echo '<td><input type="checkbox" name="items[]" value="' . $row['0'] . '" /></td>';
Hi, I’m trying to get a reference number which comes from a an array called items from another page as shown above, and to find it in the table and print out the reference row, like “Title,Platform….” into another table, but I can’t seem to get it working…any help be appreciated
if (isset($_POST['items'])) {
$n = count($_POST['items']);
for($i=0; $i < $n; $i++){
// echo $_POST['items'][$i];
}
$items = array();
foreach ($_POST['items'] as $item) {
$items[] = pg_escape_string($con, $item);
}
if (!$_SESSION["selectingrows"]) {
$item_string = "'" . implode("','", $items) . "'";
$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames WHERE 'refnumber' IN ($item_string)");
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
}
}
One thing, you need to put
{}braces after yourwhileloop.Here is what you are doing:
By not putting braces around the code after the
whilestatement, here is what your code really does:You should always put braces in to define what code is in the while loop.
You want your code to be something like this:
Format your code neatly and properly. By doing this your code is clearer and it is much easier to notice possible mistakes like the above. Always use braces for
if,while,forstatements. When putting an end line semicolon;put in a new line break. Indent your code correctly. It’s little things like formatting that make coding easier.Now the next problem I can see is the values you are getting from the
$rowsarray:This is trying to get something from the
$rowsarray which has the key of string'1'.Usually you access array values by index, which uses an integer beggining from
0. Or you access it by a key.Either you can try this:
Or this: