SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Location
    australia
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with for loop or looping till match with array

    I have an array which has the data of books and uids. The array changes and does not have a set amount of data. Now i want to compare the uid of each array to a uid in my database and if matched then print out the book name. I thought For each may work but had no luck. here is a sample of my array with print_r function.

    Array
    (
    [data] => Array
    (
    [0] => Array
    (
    [book] => fairy
    [uid] => 11 )

    [1] => Array
    (
    [book] => Kloet
    [uid] => 46 )

    I had tryed the following but had no luck.

    <?php
    $x= $bookarray;
    foreach ($x as $value)
    {
    //check that uid matchs
    $query ="SELECT uid FROM books WHERE uid='".$x."'";
    $result = mysql_query($query);
    $affected_rows = mysql_num_rows($result);
    //If match found.
    if ($affected_rows==1)
    {
    echo $value . "<br />";
    }
    }
    ?>

    thank you for any help

  2. #2
    SitePoint Enthusiast LSC-Rob's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, NSW Australia
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First thing's first - you're stating in your foreach "$x as $value", so you'll need to use $value not $x inside the loop to access that data.

    Secondly, if your data is an array inside an array, you'll need to be calling the array that includes your data (i.e. $x['data'] not $x) (that may not have made as much sense as I hoped it would - let me know if you'd like it explained further).
    Rob G.
    ShopManager - Sales & Repair Business Management Software

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Location
    australia
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh i get it now,
    $x= $bookarray['data'];
    and
    $value['uid']
    worked a treat thanks.


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
  •