Author Topic: problem with loop....  (Read 438 times)

0 Members and 1 Guest are viewing this topic.

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
problem with loop....
« on: September 15, 2007, 02:55:01 PM »
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
Code: [Select]
<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
Code: [Select]
<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
<?php

echo 'Hello World!';

?>

Online darkfreaks

  • Fanatic
  • Posts: 4,306
  • Gender: Male
    • View Profile
    • Site Security
Re: problem with loop....
« Reply #1 on: September 15, 2007, 03:17:26 PM »
the num_row function wont work if you dont select a database


Code: [Select]
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
« Last Edit: September 15, 2007, 03:19:16 PM by darkfreaks »

Offline rarebit

  • Devotee
  • Posts: 996
    • View Profile
Re: problem with loop....
« Reply #2 on: September 15, 2007, 03:22:02 PM »
$i will only ever equal $numrows once!

Offline rarebit

  • Devotee
  • Posts: 996
    • View Profile
Re: problem with loop....
« Reply #3 on: September 15, 2007, 03:32:31 PM »
Here's food for thought!
Code: [Select]
for($i=0;$i<10;$i++)
{
echo (($i % 2) == 0) ? 'foo' : 'bar';
}

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
Re: problem with loop....
« Reply #4 on: September 15, 2007, 05:55:58 PM »
the num_row function wont work if you dont select a database


Code: [Select]
$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....
<?php

echo 'Hello World!';

?>

Offline nathanmaxsonadilTopic starter

  • Enthusiast
  • Posts: 305
    • View Profile
Re: problem with loop....
« Reply #5 on: September 15, 2007, 06:01:30 PM »
I did a quick and dirty fix by making $i -1 but is there a better fix than that?
<?php

echo 'Hello World!';

?>