SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)

    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:
    Code:
    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!):
    Code:
    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']) ) { ):
    Code:
    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
    PHP Code:
    $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!

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    724
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I'm sure someone will have a more elegant way of doing this, but this should work.

    PHP Code:
    <?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 />"
             } 
         } 

    ?>

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thanks Drummin I have just tried your code quickly and it looks like it works, but will give it more of a test tomorrow.

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:

    PHP Code:
    $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;

    Last edited by fretburner; Feb 5, 2014 at 17:34. Reason: Corrected mistake

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    724
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    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:

    PHP Code:
    $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.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thanks fretburner I will give it a go later.

    I did not know about the IN option in MySQL.

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Both methods worked well and I used fretburners method after removing an excess ;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •