Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts

    Question Selecting from a position in an array...

    Hi!

    I have a mysql database with 4 rows containing arrays, including an unique ID field.

    I want to run a SELECT query to pull out the data from each array based on its position.

    E.g. I may want to select from the 4 arrays in my row the 3rd results:

    | ID | Name | Description | Price |
    3 Car Red $100

    My code so far looks like this: ($furnaccept is POST'd from the previous page)

    PHP Code:

    $furnaccept 
    serialize($_POST['furnaccept']);

    $mydata unserialize($furnaccept);

    $itemcount count($mydata);

    for (
    $i=0;$i<$itemcount;$i++) {
    echo 
    '<br><br><b> SHOW ME ROW ' $mydata[$i] . ' OF THE ARRAY:</b><br>'
    echo 
    '<br><br>';

    $query "SELECT furn_item,furn_desc,furn_qty,furn_cost 
    FROM dbQuotes 
    WHERE 
    furn_ID='$i'"
    ;

    $result mysql_query($query);

    while(list(
    $furn_item,
    $furn_desc,    
    $furn_qty,
    $furn_cost) = mysql_fetch_row($result))

    {

    echo 
    $furn_item ' <br />' $furn_desc ' <br />' .    $furn_qty ' <br />' .    $furn_cost
         
    }




    Am I on the right lines?

    Help!

  • #2
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by thoford75 View Post
    PHP Code:

    $query 
    "SELECT furn_item,furn_desc,furn_qty,furn_cost 
    FROM dbQuotes 
    WHERE 
    furn_ID='$i'"

    This doesn't work because $i will equal an id e.g.
    0,1,2 ...

    but my furn_ID row will look something like this:
    a:3:{i:0;s:1:"0";i:1;s:1:"2";i:2;s:1:"3";}

    Guess it's a case of converting the furn_ID data first??
    Last edited by thoford75; 01-25-2012 at 12:26 PM. Reason: correction

  • #3
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    you should go to the database only once not for every iteration of a loop...

    write all your id's into a comma separated string

    PHP Code:
    $myIdStr implode(",",$mydata); 
    this is assuming you are ONLY sending the ids

    get all the records

    PHP Code:
    $query "SELECT furn_item,furn_desc,furn_qty,furn_cost 
    FROM dbQuotes 
    WHERE 
    furn_ID IN ("
    .$myIdStr.")" 
    then loop through the returned results and display...
    Last edited by jmj001; 01-26-2012 at 05:35 AM.

  • Users who have thanked jmj001 for this post:

    thoford75 (01-26-2012)

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Hi, thanks for the reply. Using this returns this:

    PHP Code:
    SELECT furn_itemfurn_descfurn_qtyfurn_cost FROM dbQuotes WHERE furn_ID IN (0,1
    a:2:{i:0;s:5:"Chair";i:1;s:5:"Table";}
    a:2:{i:0;s:17:"Brown with 4 legs";i:1;s:14:"Brown circular";}
    a:2:{i:0;s:1:"4";i:1;s:1:"1";}
    a:2:{i:0;s:3:"200";i:1;s:3:"400";}


    PHP Code:
    SELECT furn_itemfurn_descfurn_qtyfurn_cost FROM dbQuotes WHERE furn_ID IN (0,1
    a:2:{i:0;s:5:"Chair";i:1;s:5:"Table";}
    a:2:{i:0;s:17:"Brown with 4 legs";i:1;s:14:"Brown circular";}
    a:2:{i:0;s:1:"4";i:1;s:1:"1";}
    a:2:{i:0;s:3:"200";i:1;s:3:"400";}

    What I want to show is:

    Chair
    Brown with 4 legs
    4
    200

    Table
    Brown circular
    1
    400

    So basically I want the SELECT command to look up an array's id then go to the database and pull out the value for that array position...
    Last edited by thoford75; 01-26-2012 at 09:07 AM.

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    is that the serialized data stored in the database?

    you should be able to loop through the results and unserialize it

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Yes the data is serialized. The entire code currently is:

    PHP Code:
     $mydata unserialize($furnaccept);


    $itemcount count($mydata);

    $myIdStr implode(",",$mydata);  
    for (
    $i=0;$i<$itemcount;$i++) {
    echo 
    '<br><br><b> SHOW ME ROW ' $mydata[$i] . ' OF THE ARRAY:</b><br>'
      echo 
    '<br><br>';
    echo 
    $query "SELECT furn_item,    furn_desc,    furn_qty,    furn_cost FROM dbQuotes WHERE id='$quote_id' and furn_ID IN (".$myIdStr.")" ;
    $result mysql_query($query);
    while(list(
    $furn_item,    $furn_desc,    $furn_qty,    $furn_cost) = mysql_fetch_row($result))
    {

    echo 
    '<br />' $furn_item ' <br />' $furn_desc ' <br />' .    $furn_qty ' <br />' .    $furn_cost;      
    }




  • #7
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    prob easiest to get all the records and unserialize them and search for whatever record you are looking for

  • Users who have thanked jmj001 for this post:

    thoford75 (01-26-2012)

  • #8
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    As a test I tried the following code:
    PHP Code:
    echo '<br />' unserialize($furn_item) . ' <br />' $furn_desc[$i] . ' <br />' .    $furn_qty[$i] . ' <br />' .    $furn_cost
    and output was:

    SHOW ME ROW 0 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)
    Array
    a
    a
    a:2:{i:0;s:3:"200";i:1;s:3:"400";}

    SHOW ME ROW 1 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)
    Array
    :
    :
    a:2:{i:0;s:3:"200";i:1;s:3:"400";}

  • #9
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Had a thought... essentially I am trying to find the position of a value in an array. If i have the position (i.e. 0,1 or 0,3,4 etc) is there a way of saying

    SELECT x from table where myID = $myID

    THEN...

    get position 0->
    then position 1->

    or

    get position 0->
    then position 3->
    then position 4->


    Help! ?

  • #10
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    How do I got from getting data like this:

    a:2:{i:0;s:5:"Chair";i:1;s:5:"Table";}

    into an array like this:

    [0]-> Chair
    [1]-> Table


    ?

  • #11
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    PHP Code:
    $someVar 'a:2:{i:0;s:5:"Chair";i:1;s:5:"Table";} ';
    print_r(unserialize($someVar)); 

  • Users who have thanked jmj001 for this post:

    thoford75 (01-27-2012)

  • #12
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Almost there! My entire PHP code:
    PHP Code:
    <?PHP 
    include "../includes/connect.php";
    $quote_id $_POST['quote_id'];

    $furnaccept serialize($_POST['furnaccept']);

    $mydata unserialize($furnaccept);

    $itemcount count($mydata);

    $myIdStr implode(",",$mydata);
      
    for (
    $i=0;$i<$itemcount;$i++) {
    echo 
    '<br><br><b> SHOW ME ROW ' $mydata[$i] . ' OF THE ARRAY:</b><br>'
      echo 
    '<br><br>';
    echo 
    $query "SELECT furn_item,    furn_desc,    furn_qty,    furn_cost FROM dbQuotes WHERE id='$quote_id' and furn_ID IN (".$myIdStr.")" ;
    $result mysql_query($query);
    while(list(
    $furn_item,    $furn_desc,    $furn_qty,    $furn_cost) = mysql_fetch_row($result))
    {

    echo 
    '<br />';
    print_r(unserialize($furn_item)); 
    echo 
    '<br />';
    print_r(unserialize($furn_desc)); 
    echo 
    '<br />';
    print_r(unserialize($furn_qty)); 
    echo 
    '<br />';
    print_r(unserialize($furn_cost)); 
    echo 
    '<br />';
     
    }


    }

    ?>
    The result:


    SHOW ME ROW 0 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)
    Array ( [0] => Chair [1] => Table )
    Array ( [0] => Brown with 4 legs [1] => Brown circular )
    Array ( [0] => 4 [1] => 1 )
    Array ( [0] => 200 [1] => 400 )


    SHOW ME ROW 1 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)
    Array ( [0] => Chair [1] => Table )
    Array ( [0] => Brown with 4 legs [1] => Brown circular )
    Array ( [0] => 4 [1] => 1 )
    Array ( [0] => 200 [1] => 400 )


    ***

    What I want...

    SHOW ME ROW 0 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)
    Chair
    Brown with 4 legs
    4
    200


    SHOW ME ROW 1 OF THE ARRAY:


    SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1)

    Table
    Brown circular
    1
    400

  • #13
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    do you know how to loop through an array and echo the results?

  • Users who have thanked jmj001 for this post:

    thoford75 (01-27-2012)

  • #14
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    why are you storing this information in a serialized format, it doesn't make any sense to me... just put it i there the way you want it to come out

  • Users who have thanked jmj001 for this post:

    thoford75 (01-27-2012)

  • #15
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmj001 View Post
    do you know how to loop through an array and echo the results?
    No, not for an array. Also the data is serialised as an array from a previous form.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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