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.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nested while loop problem

    My problem is that in the <em>Also found in:</em> section it is only displaying the results for the first entry and not the rest
    any idea what would cause this to happen?
    PHP Code:
    <?
    $result 
    $db->query("SELECT * FROM business_detail");
    $result2 $db->query("SELECT * FROM business_type");

    while(
    $row $db->fetchArray($result)) {
            
    ?>
            <? if($row['b_image']) { ?><img src="../images/thumb_b/<? echo $row['b_image']; ?>" /><br /><? ?>
            <strong>Title:</strong> <? echo $row['b_title']; ?> <br />
            <strong>Address:</strong> <? echo $row['b_address1']; ?><br /><? if($row['b_address2']) { echo $row['b_address2']."<br />"; } ?>
            <strong>Phone:</strong> <? echo $row['b_phone']; ?><br />
            <strong>Fax:</strong> <? if($row['b_fax'] == "") { echo "n/a"; } else { echo $row['b_fax']; } ?><br />
            <strong>Email:</strong> <? echo $row['b_email']; ?><br />
            <strong>Website:</strong> <? if($row['b_weburl'] == "n/a") { echo $row['b_weburl']; } else {?><a href="<? echo $row['b_weburl']; ?>"><? echo $row['b_weburl']; ?><a><?  ?><br />
            <strong>Description:</strong><br /> <? echo $row['b_desc']; ?><br />
            <em>Also found in:</em> <br />
            <?
            $types 
    preg_split("/[\-,]+/"$row['b_types']);
            while(
    $row2 $db->fetchArray($result2)) {
                    
    //$types = str_replace("-", " ", $row['b_types']);
        
                    
    if(in_array($row2['t_short'], $types)) { echo $row2['t_name']."<br /> "; }
                }

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    fetch_array() is like a file read, it fetches one row at a time, repositioning the "pointer" at the next row. You rip through all the rows the first time, until the pointer gets to the end of the results set, then bails out of the loop. You don't ever reset the "pointer", so the pointer remains at the end of the results and that loop never gets executed again.

    Since (apparently) you want the same data to be displayed for every business detail row, you can simply fetch the query results into an array once and then use the array with a foreach() to loop the echo statement.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok i got the foreach to spit out the values of the array but i still need to do the while($row2 = $db->fetchArray($result2)) because i need to see if $value matches any of the entries in the database.
    any ideas?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yeah you do need to still do the fetch loop, just not inside the first loop. Do it outside the first loop. Within the first loop, loop through the array that you build.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    You can also use the mysql_data_seek( resource result, int row_number ) function to reset the pointer back to the first row of the result set.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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