Jump to content

while and for loop not working plz help.


McBryver

Recommended Posts

Okay I am trying to do a for loop within a while loop and its so far not working.

Here is what I currently have. It echo's nothing out. Also If I remove the Code that generates information from the for loop It echo's out perfectly so It has something to do with the for loop.

PHP Code so Far:

<?php
if(isset($_POST['system'])){
    
    $system = $_GET['system'];
    
    
    
}else{
    $system = 1;
}
$q = "SELECT * FROM `planets` WHERE `system` = '$system' ORDER BY `id` ASC LIMIT 9";
$res = mysql_query($q) or die("Error:<br />".mysql_error());
$couny = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){ 
    
    for($i=1; $i>=$couny; $i++){    ?>
    <li>
            <span <?php if($i=1){echo "id='mercury'";}
            if($i=2){echo "id='venus'";}
            else if($i=3){echo "id='earth'";}
            else if($i=4){echo "id='mars'";}
            else if($i=5){echo "id='jupiter'";}
            else if($i=6){echo "id='saturn'";}
            else if($i=7){echo "id='uranus'";}
            else if($i={echo "id='neptune'";}
            else if($i=9){echo "id='pluto'";}?>id="mercury"><?php echo $row['name']; ?></span>
            <p><?php if($colony['building11'] >= $row['building11']){?>
            This Colony Currently Has the Following:<br />
            Hydrogen: <?php echo num($row['recource1']); ?><br />
            Gold: <?php echo num($row['recource1']); ?><br />
            Platinum: <?php echo num($row['recource1']); ?><br />
            Silver: <?php echo num($row['recource1']); ?><br />
            Bio-Waiste: <?php echo num($row['recource1']); ?><br />
            Plasma: <?php echo num($row['recource1']); ?><br />
            Metal:  <?php echo num($row['recource1']); ?><br />
            <?php }else{?>       <font color="red">Insufficiant Observatory Level!</font><br />
            Hydrogen: ???????????????<br />
            Gold: ???????????????<br />
            Platinum: ???????????????<br />
            Silver: ???????????????<br />
            Bio-Waiste: ???????????????<br />
            Plasma: ???????????????<br />
            Metal:  ???????????????<br /> <?php } ?></p>

            </li>
    
       <?php } }
?> 

Thanks Brian.

 

PS. The Attached Image is what it looks on my end the for and while loops displays the planets in the system right under the red Solar System Star Text.

 

Thank you again for any help.

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok.. The 2 issues I can see are your conditions in the for loop..

You have "for $i greater than or equal to $couny..

change it to less than

for ($i=0;$i<=$couny;$i++)

The other thing I have noticed is your

if ($i=1)

This will always return to true as you are assigning 1 to $i.. you need to change all your code to reflect equality

if ($i == 1)

 

Link to comment
Share on other sites

Okay I did what you said to do and It does now display something but not what I want, it shows the first row name like 20,000 times on the page.

 

I currently have:

<?php
if(isset($_POST['system'])){
    
    $system = $_GET['system'];
    
    
    
}else{
    $system = 1;
}
$q = "SELECT * FROM `planets` WHERE `system` = '$system' ORDER BY `id` ASC LIMIT 9";
$res = mysql_query($q) or die("Error:<br />".mysql_error());
$couny = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){ 
    
    for ($i=0;$i<=$couny;$i++){    ?>
    <li>
            <span <?php if($i == 1){echo "id='mercury'";}
            if($i=2){echo "id='venus'";}
            else if($i == 3){echo "id='earth'";}
            else if($i == 4){echo "id='mars'";}
            else if($i == 5){echo "id='jupiter'";}
            else if($i == 6){echo "id='saturn'";}
            else if($i == 7){echo "id='uranus'";}
            else if($i == {echo "id='neptune'";}
            else if($i == 9){echo "id='pluto'";}?>><?php echo $row['name']; ?></span>
            <p><?php if($colony['building11'] >= $row['building11']){?>
            This Colony Currently Has the Following:<br />
            Hydrogen: <?php echo num($row['recource1']); ?><br />
            Gold: <?php echo num($row['recource1']); ?><br />
            Platinum: <?php echo num($row['recource1']); ?><br />
            Silver: <?php echo num($row['recource1']); ?><br />
            Bio-Waiste: <?php echo num($row['recource1']); ?><br />
            Plasma: <?php echo num($row['recource1']); ?><br />
            Metal:  <?php echo num($row['recource1']); ?><br />
            <?php }else{?>       <font color="red">Insufficiant Observatory Level!</font><br />
            Hydrogen: ???????????????<br />
            Gold: ???????????????<br />
            Platinum: ???????????????<br />
            Silver: ???????????????<br />
            Bio-Waiste: ???????????????<br />
            Plasma: ???????????????<br />
            Metal:  ???????????????<br /> <?php } ?></p>

            </li>
    
       <?php } }
?> 

 

 

Thank you thus far Brian.

Link to comment
Share on other sites

Figured it outs myself  :D

 

<?php
if(isset($_POST['system'])){
    
    $system = $_GET['system'];
    
    
    
}else{
    $system = 1;
}
$q = "SELECT * FROM `planets` WHERE `system` = '$system' ORDER BY `id` ASC LIMIT 9";
$res = mysql_query($q) or die("Error:<br />".mysql_error());
$couny = mysql_num_rows($res);
for ($i=0;$i<=$couny;$i++){    
while( $row = mysql_fetch_array($res) ){ ?>
   
    
    <li>
            <span <?php if($i == 1){echo "id='mercury'";}
            else if($i=2){echo "id='venus'";}
            else if($i == 3){echo 'id="earth"';}
            else if($i == 4){echo 'id="mars"';}
            else if($i == 5){echo 'id="jupiter"';}
            else if($i == 6){echo 'id="saturn"';}
            else if($i == 7){echo 'id="uranus"';}
            else if($i == {echo 'id="neptune"';}
            else if($i == 9){echo 'id="pluto"';}?>><?php echo $row['name']; ?></span>
            <p><?php if($colony['building11'] >= $row['building11']){?>
            This Colony Currently Has the Following:<br />
            Hydrogen: <?php echo num($row['recource1']); ?><br />
            Gold: <?php echo num($row['recource1']); ?><br />
            Platinum: <?php echo num($row['recource1']); ?><br />
            Silver: <?php echo num($row['recource1']); ?><br />
            Bio-Waiste: <?php echo num($row['recource1']); ?><br />
            Plasma: <?php echo num($row['recource1']); ?><br />
            Metal:  <?php echo num($row['recource1']); ?><br />
            <?php }else{?>       <font color="red">Insufficiant Observatory Level!</font><br />
            Hydrogen: ???????????????<br />
            Gold: ???????????????<br />
            Platinum: ???????????????<br />
            Silver: ???????????????<br />
            Bio-Waiste: ???????????????<br />
            Plasma: ???????????????<br />
            Metal:  ???????????????<br /> <?php } ?></p>

            </li>
    
       <?php } }
?> 

 

I simply had to place the for loop outside of the while loop  8)

 

Thanks for leading me in the right direction!

Brian.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.