Please login or register.

Login with username, password and session length
Advanced search  

News:

We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!

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

0 Members and 1 Guest are viewing this topic.

nathanmaxsonadil

  • Enthusiast
  • Offline Offline
  • Posts: 305
    • View Profile
problem with loop....
« on: September 15, 2007, 01: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
Logged
<?php

echo 'Hello World!';

?>

darkfreaks

  • Fanatic
  • Offline Offline
  • Gender: Male
  • Posts: 3,570
    • View Profile
    • WWW
Re: problem with loop....
« Reply #1 on: September 15, 2007, 02: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, 02:19:16 PM by darkfreaks »
Logged

rarebit

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

rarebit

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

nathanmaxsonadil

  • Enthusiast
  • Offline Offline
  • Posts: 305
    • View Profile
Re: problem with loop....
« Reply #4 on: September 15, 2007, 04: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....
Logged
<?php

echo 'Hello World!';

?>

nathanmaxsonadil

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

echo 'Hello World!';

?>

PHP Freaks Forums

 
 
 

Page created in 0.047 seconds with 20 queries.