TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-06-2008, 06:06 AM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 20
Thanks: 1
Jako is on a distinguished road
Default Nested While Loop Issue

I'm having trouble with nested while loops.

Here's what I am currently doing.

PHP Code:
// sql query
$sql 'SELECT * FROM rank LEFT JOIN national on national.n_id = rank.rank_nation ORDER BY rank ASC';
$result mysql_query($sql) or dies(mysql_error());

// get details of all nations
$sql_nation "SELECT * FROM national ORDER BY national_name ASC";
$nation_list mysql_query($sql_nation) or die (mysql_error()); 
any my tested loops look something like this.

PHP Code:
    <? while($row mysql_fetch_assoc($result)) { ?>
    <tr>
        <td><? echo $row['rank']; ?></td>
        <td><input name="rank" type="text" value="<? echo $row['rank']; ?>" size="20" maxsize="100" /></td>
        <td><select name="national_id"><option selected value="<? echo $row['national_id']; ?>"><? echo $row['national_name']; ?></option>
<option>-------</option>
 <? 
 
while($r mysql_fetch_assoc($nation_list)) { ?><option value="<? echo $r['n_id']; ?>"><? echo $r['national_name']; ?></option>
<? ?> </select></td>
        <td><input name="points" type="text" value="<? echo $row['rank_points']; ?>" size="20" maxsize="100" /></td>
    </tr>
    <? ?>
How can I fix this? Right now the first loop is working alright, but the nested while loop only works once.
Jako is offline  
Reply With Quote
Old 08-06-2008, 06:31 AM   #2 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Because it only has one result set to work with. If you need it to run the loop on that second query each time the first loop runs, you need to nest the query inside the first loop so that $nation_list will repopulate itself each time - alternatively, and better yet, as it only runs the query once, you could run the nested loop outside of the first loop and save the results to a variable, then just echo that variable inside the first loop each time. Ala;

PHP Code:
<?php
    $sql_nation 
"SELECT * FROM national ORDER BY national_name ASC";
    
$nation_list mysql_query($sql_nation) or die (mysql_error());  

    
$nation_list '';

    while(
$row mysql_fetch_assoc($nation_list))
    {
        
$nation_list .= '<option value="'.$row['n_id'].'">'.$row['national_name'].'</option>'."\n";
    }

    
$sql 'SELECT * FROM rank LEFT JOIN national on national.n_id = rank.rank_nation ORDER BY rank ASC';
    
$result mysql_query($sql) or die (mysql_error()); 

    while(
$row mysql_fetch_assoc($result))
    {
?>
    <tr>
        <td><?php echo $row['rank']; ?></td>
        <td><input name="rank" type="text" value="<?php echo $row['rank']; ?>" size="20" maxsize="100" /></td>
        <td><select name="national_id"><option selected value="<?php echo $row['national_id']; ?>"><?php echo $row['national_name']; ?></option>
            <option>-------</option>
            <?php echo $nation_list?>
            </select></td>
        <td><input name="points" type="text" value="<?php echo $row['rank_points']; ?>" size="20" maxsize="100" /></td>
    </tr>
<?php ?>
-m
delayedinsanity is offline  
Reply With Quote
Old 08-06-2008, 06:51 AM   #3 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 20
Thanks: 1
Jako is on a distinguished road
Default

Thanks m,

I'm getting this error now.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 31

Which is this line,

PHP Code:
while($row mysql_fetch_assoc($nation_list)) 
// EDIT

Actually got it fixed by changing it to

PHP Code:
$nation .= '<option value="'.$row['n_id'].'">'.$row['national_name'].'</option>'."\n"
Guess since there was already a variable $nation_list it was getting screwy.

It's working great now. Thanks!
Jako is offline  
Reply With Quote
Old 08-06-2008, 07:13 AM   #4 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

ahahah oops, I didn't realize I reused the same variable there. That's why I use hungarian notation, but I try to omit it in examples for people who don't use it.
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:05 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0