Jump to content

problem with loop....


nathanmaxsonadil

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/69488-problem-with-loop/
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/69488-problem-with-loop/#findComment-349253
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.