I am passing a list of part id’s from a form to a conformation page; I want to display the list of items the user has ordered along with the price etc. using the ID number.
This is what I have in the array from the form using var_dump:
This is what I am trying but I am getting a couple of errors ( line 64 is if ( in_array($needle, $result[‘id’]) ) { ):
Notice: Undefined index: id in C:\\xampp\\htdocs\\online_prices\\validate.php on line 64
Warning: in_array() expects parameter 2 to be array, null given in C:\\xampp\\htdocs\\online_prices\\validate.php on line 64
$stmt = $PDO->prepare("SELECT id, item, description, price FROM price");
if ($stmt->execute()) {
$result =$stmt->fetchAll();
echo "<pre>".print_r($result)."</pre>";
foreach ($_POST['part'] as $needle) {
if ( in_array($needle, $result['id']) ) {
echo $row->item;
}
}
}
I would guess I need to use a foreach on $result somehow but I am not sure how to do it.
I am looking to display the item, description, price for every part in the $_post array.
Please can somebody give me some pointers or possibly a better method; I did consider doing a database call for every part on the form but that seemed a bit excessive!
You’re returning all rows from your table with that query, which could be pulling in a lot of unnecessary data. You could return only the relevant rows like this:
$part_ids = array_filter($_POST['part'], "is_numeric");
$part_ids = implode(',', $part_ids);
$stmt = $PDO->query("SELECT id, item, description, price FROM price WHERE id IN ($part_ids)";);
while($row = $stmt->fetchObject()) {
echo $row->item;
}