McBryver Posted November 9, 2010 Share Posted November 9, 2010 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] Quote Link to comment Share on other sites More sharing options...
Buddski Posted November 9, 2010 Share Posted November 9, 2010 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) Quote Link to comment Share on other sites More sharing options...
McBryver Posted November 9, 2010 Author Share Posted November 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
McBryver Posted November 9, 2010 Author Share Posted November 9, 2010 Figured it outs myself <?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 Thanks for leading me in the right direction! Brian. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.