nathanmaxsonadil Posted September 15, 2007 Share Posted September 15, 2007 my loop is still causing problems... it supposed to find the half mark and then put half on one side and half on the other but it just puts 1 on one side and 3 on the other. here's my code. $sql1 = mysql_query('SELECT * FROM table') or die('Could not connect: ' . mysql_error()); $numrows = mysql_num_rows($sql1); $numrows = $numrows / 2; $numrows = round($numrows); echo '<div id="div1">'; $i = 0; while ($row = mysql_fetch_assoc($sql1)) { $i++; if($i == $numrows){ echo"</div> <div id='div2'><br/><a href='{$row['href']}'> <img style='border:0;' src='siteimg/{$row['img']}' alt='{$row['alt']}'/> {$row['url']} </a>"; }else if($i == 1){ echo "<br/><a href='{$row['href']}'> <img style='border:0;' src='siteimg/{$row['img']}' alt='{$row['alt']}'/> {$row['url']} </a>"; }else { echo "<br/><br/><a href='{$row['href']}'> <img style='border:0;' src='siteimg/{$row['img']}' alt='{$row['alt']}'/> {$row['url']} </a>"; } } echo'</div>'; so it should do something like <div id='1'> <br/><a href='site1.php'> <img style='border:0;' src='siteimg/site1.jpg' alt='site1'/> site1.com </a></div> <br/><br/><a href='site2.php'> <img style='border:0;' src='siteimg/site2.jpg' alt='site2'/> site2.com </a><div id='div2'><br/><a href='site3.php'> <img style='border:0;' src='siteimg/site3.jpg' alt='site3'/> site3.com </a><br/><br/><a href='site4.php'> <img style='border:0;' src='siteimg/site4.jpg' alt='site4'/> site4.com </a></div> however it does something like this <div id='1'> <br/><a href='site1.php'> <img style='border:0;' src='siteimg/site1.jpg' alt='site1'/> site1.com </a></div> <div id='div2'><br/><a href='site2.php'> <img style='border:0;' src='siteimg/site2.jpg' alt='site2'/> site2.com </a><br/><br/><a href='site3.php'> <img style='border:0;' src='siteimg/site3.jpg' alt='site3'/> site3.com </a><br/><br/><a href='site4.php'> <img style='border:0;' src='siteimg/site4.jpg' alt='site4'/> site4.com </a></div> what is wrong with my loop Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/ Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 the num_row function wont work if you dont select a database $link = mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("database", $link); Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349166 Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 $i will only ever equal $numrows once! Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349168 Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Here's food for thought! for($i=0;$i<10;$i++) { echo (($i % 2) == 0) ? 'foo' : 'bar'; } Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349173 Share on other sites More sharing options...
nathanmaxsonadil Posted September 15, 2007 Author Share Posted September 15, 2007 the num_row function wont work if you dont select a database $link = mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("database", $link); I have connected to the database. $i will only ever equal $numrows once! that's what I want.... Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349253 Share on other sites More sharing options...
nathanmaxsonadil Posted September 15, 2007 Author Share Posted September 15, 2007 I did a quick and dirty fix by making $i -1 but is there a better fix than that? Quote Link to comment https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349258 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.