PHP SELECT Query in FOR LOOP not executing - select

I have a SELECT query in the FOR loop that will just not return a value, I have echoed the parameters and also run the SQL and they appear fine.
Any ideas as to where I am going wrong?
I am posting values from a multi row form that has the following text fields:
F1,
Grade_system_id
Grade
Grade_System_id
I want to look up the Grade_Points using Grade and Grade_System_id, but not able to.
The error checks dont suggest any errors.
foreach ($_POST['F1'] as $Lesson_Class_id => $Response) {
$Grade_System_id = $_POST['Grade_System_id'][$Lesson_Class_id];
$G1 = $_POST['G1'][$Lesson_Class_id];
$Lesson_Class_id=trim($_POST['Lesson_Class_id']);
$I1 = $_POST['I1'][$Lesson_Class_id];
$stmt = $conn -> prepare('SELECT Grade_Points FROM Grades WHERE Grade=? AND Grade_System_id=?');
$stmt -> bind_param('si', $G1, $Grade_System_id);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($Grade_Points);
$stmt -> fetch();
echo $G1;
echo $Grade_System_id;
echo $Response;
echo $Grade_Points;
if ($Response=="Completed") {
$T_Points=$Grade_Points;
}
if ($Response!="Completed") {
$T_Points="";
}
$stmt = $conn -> prepare('UPDATE Lesson_CLass SET I1 = ?, F1=?, T_Points=? WHERE Lesson_Class_id= ?');
//echo 'Student ID: ' . $Lesson_Class_id . ' Outcome: ' . $Response . "<br>";
$stmt -> bind_param('ssss', $I1,$Response, $T_Points, $Lesson_Class_id);
$stmt -> execute();
}
?>
The query is unable to return 'Grade Points'

Related

Moodle Filters - GROUP BY

How can we get to work the moodle filters when GROUP BY - select form is used as filter mform field. I wasn't getting the desired output when I select the course. GROUP BY is not consider when sql_like function is called:
require_once($CFG->dirroot.'/filter_form.php');
$mform = new filter_form();
$coursefilter = '';
if ($formdata = $mform->get_data()) {
$coursefilter = $formdata->course;
}
$mform->set_data($formdata);
$mform->display();
$reporttable = new html_table();
$reporttable->head = array('course', 'users');
$reporttable->attributes['class'] = 'table';
$sql = "SELECT c.fullname, count(cc.userid) AS 'completed'
FROM {course_completions} cc JOIN {course} ON c.id = cc.course WHERE cc.timestarted > 0
GROUP BY c.fullname ";
$params = array();
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$mds = $DB->get_records_sql($sql, $params);
foreach ($mds as $m) {
$reporttable->data[] = new html_table_row(array(implode(array($m->fullname. $m->lastname)),
$m->completed ));
}
echo html_writer::table($reporttable);
Add the GROUP BY after the filter.
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$sql .= " GROUP BY c.fullname ";

syntax for selecting the last recordset in my model

I tried different possibilities but nothing worked, in the tutorial I couldn't find an example either.
I have a method in my modelclass:
public function getlastImport($filename)
{
//$id = (int) $id;
$rowset = $this->tableGateway->select(['Path' => $filename]);
$row = $rowset->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
I want to retrieve the last import of a given filename, so ist must be like in sql:
select max(ID) from table where filename = $filename;
But how would be the right syntax in this case?
The sql query should be
"SELECT * FROM table_name WHERE filename={$filename} ORDER BY id DESC LIMIT 1"
Use as the following in your model
public function getlastImport($filename)
{
$select = $this->tableGateway->getSql()->select();
$select->columns(array('id', 'filename', 'label'));
$select->where(array('filename' => $filename));
$select->order("id DESC");
$select->limit(1);
$result = $this->tableGateway->selectWith($select);
$row = $result->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
Hope this would help you!

Returning an array using PDO

I'm trying to use PDO using the following code, I want to select the array of values for the context_list, but it returns one record, any ideas where I'm going wrong?
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list = $row['area_easting'];
$northing_list = $row['area_northing'];
$context_list = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo $context_list;
A partial solution:
This worked:
$query = $conn->prepare("SELECT area_easting, area_northing, context_number FROM excavation.contexts");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo $r->area_easting, '|';
echo $r->area_northing;
echo '<br/>';
}
But Now I need to make the $r->area_easting accessible to the session, but that's another question.
Your query may return several records (provided they actually exist in the table).
Your code loops through all the records (with the while loop) but the values ($easting_list, $northing_list and $context_list) are overwritten in the loop.
I suggest the following changes to your code:
$easting_list = array();
$northing_list = array();
$context_list = array();
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list[] = $row['area_easting'];
$northing_list[] = $row['area_northing'];
$context_list[] = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo implode(', ', $context_list);
Now all the values are stored in 3 arrays. explode is used to print a comma-separated list of all values in $context_list array.

Prepare content does not work for Sourcerer and DirectPHP

In my Joomla website, I need to execute some custom SQL queries, that have to select different titles from related categories.
Problem I have it works like option Prepare Content is turned off, so all of my content is outside HTML tags.
Module content looks like this:
{source}
<?php
$var_result = '';
$var_categories = array();
$var_category_list = array();
$db =& JFactory::getDBO();
$query = 'select * from jneg_categories where parent_id = 9';
$db->setQuery($query,0,300);
$res = $db->loadObjectList();
foreach ( $res as $row ) {
$var_categories[($row->id)] = $row->title;
$var_category_list[] = $row->id;
}
$var_category_list = implode($var_category_list, ', ');
$sql = "select * from jneg_content where catid IN (".$var_category_list.") order by `catid`";
$db->setQuery($sql,0,30000);
$res = $db->loadObjectList();
$var_current_cat = 0;
foreach ( $res as $row ) {
if ($current_cat != $row->catid) {
$current_cat = $row->catid;
echo '<h2>'.$categories[($row->catid)] . '</h2>';
echo '<br>';
}
echo $row->title;
echo '<br>';
}
?>
{/source}
Can you help me how to get proper HTML as a result of this code please.
Sourcer or other php rendering plugins don't run in html modules unless you go under the module 'options' and select 'prepare content'...
...or you can use this module and just include your php file directly:
https://github.com/idea34/mod_show
Ok, I did it with Jumi plugin - http://2glux.com/projects/jumi/usage-for-j15
Thank you anyway.

Incorrect `update statement` using IN operator with Zend

I have a function which is wanted to execute a statement like below:
UPDATE coupon_users SET status = status | '1' WHERE id IN ('3','4')
And in coupon_users model, I wrote a method like below do to:
/**
* #param array $ids #array(3,4)
* #param array $status #1
*/
public function updateStatus(array $ids, $status)
{
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
array(
$status,
$ids
)
)->execute();
return $result;
}
But the query is always:
UPDATE coupon_users SET status = status | '1' WHERE id IN ('Array')
I don't know what am I wrong here, please help me, many thanks.
According to the PDO documentation (Zend_Db uses PDO as its DB access backend):
You cannot bind multiple values to a single named parameter in, for
example, the IN() clause of an SQL statement.
So, you'll probably need to prepare a bit further your query, so that it contains as many markers as elements in the array. A possible solution could be the following:
// Compose the query
$queryToExecute = "UPDATE {$this->_name} SET status = status | ? WHERE id IN (";
$questionMarks = array();
for ($id in $ids) {
$questionMarks[] = '?';
}
$queryToExecute .= implode(',', $questionMarks);
$queryToExecute .= ')';
// $queryToExecute should have the format "UPDATE ... WHERE id IN (?,?,?,...?)"
// Execute it
$result = $this->_db->query(
$queryToExecute,
array($status, $ids)
)->execute();
Hope that helps,
try:
public function updateStatus(array $ids, $status)
{
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = ? WHERE id IN (?)",
array(
$status,
implode(',',$ids)
)
)->execute();
return $result;
}
Update:
Have you tried?:
$this->_db->update($this->_name, array('status'=>$status), array('id IN (?)'=>$ids));
I haven't tested it, it also depends on what $this->_db is an instance of
http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update
Try this..
public function updateStatus(array $ids, $status)
{
$inarray= implode(',',$ids);
$result = $this->_db->query(
"UPDATE {$this->_name} SET status = status | ? WHERE id IN (?)",
array(
$status,
$inarray
)
)->execute();
return $result;
}
Its working fine for me.
$existingImagesIds = array(1, 2, 3, 7);
$where = $pImgModel->getAdapter()->quoteInto("id in (?) ", $existingImagesIds);
$pImgModel->update(array('status' => '0'), $where);