Search an for items in an array with another array and using pdo

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:


array(3) {
  ["part"]=>
  array(2) {
    [0]=>
    string(2) "38"
    [1]=>
    string(2) "39"
  }
  ["model"]=>
  string(7) "mayfair"
  ["formSubmit"]=>
  string(6) "Submit"
}

This is part of the array from my pdo query ( I have noticed something a bit strange as I have some items twice in this array!):

Array ( [0] =>
Array ( [id] => 1 [0] => 1 [item] => Plans [1] => Plans [description] => Paper Patterns and Build Manual [No VAT] [2] => Paper Patterns and Build Manual [No VAT] [price] => 25.00 [3] => 25.00 ) [1] =>
Array ( [id] => 2 [0] => 2 [item] => Front axle [1] => Front axle [description] => [2] => [price] => 15.00 [3] => 15.00 ) [2] => 

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!

I’m sure someone will have a more elegant way of doing this, but this should work.

<?php
$stmt = $PDO->prepare("SELECT id, item, description, price FROM price");

if ($stmt->execute()) {
	$result = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		$result[$row['id']]['item']        = $row['item'];
		$result[$row['id']]['description'] = $row['description'];
		$result[$row['id']]['price']       = $row['price'];
	}
    echo "<pre>";
	print_r($result);
	echo "</pre>";
     foreach ($_POST['part'] as $needle) {
         if (array_key_exists($needle, $result)){
		
		  	 $item        = $result[$needle]['item'];
		  	 $description = $result[$needle]['description'];
		  	 $price       = $result[$needle]['price'];
			
             echo $item."<br />".$description."<br />".$price."<br /><br />";
         }
     }
}
?>

Thanks Drummin I have just tried your code quickly and it looks like it works, but will give it more of a test tomorrow.

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;
} 

I agree with fretburner. Although searching through all items will work, only pulling necessary items is a better option.

Thanks fretburner I will give it a go later.

I did not know about the IN option in MySQL.

Both methods worked well and I used fretburners method after removing an excess ;