while loop difficulties

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
randolph_carter
Forum Newbie
Posts: 3
Joined: Wed Dec 17, 2003 8:15 pm

while loop difficulties

Post by randolph_carter »

I'm doing an example out of the book 'php and mysql for web development' and for the life of me I can't seem to pick out the error in this code. The gist of this loop is, it's supposed to fill in the first two table cells with the $distance variable for the first cell and that number diviided by ten for the second.
Here is the code:

Code: Select all

<?php
//the first distance cell will be 50
$distance=50;
//while the variable 'distance' is less than 50.
//create table cells and populate them with numbers 
//until the number is equal to distance
while ($distance <= 250 )	
{
  	  echo "<tr><td bgcolor='#ff4433'>"$distance"</td>";
  	  echo "<td bgcolor='#ff4433'>".$distance/10."</td></tr>";
  //distance = distance + 50
  $distance+=50;
}
?>
It's only filling the first cells with the actual 'variable name', and the 'variable name/10'. I picked out some errors that were in the code originally, but these problems remain. I am completely befuddled. I have spent some time trting to find a way to get it to function. I would greatly appreciate any help I could get. Thanks.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

so.. what exactly is the error?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Instead of exiting and entering the string you could just seperate it out and do something like this:

Code: Select all

<?php
//the first distance cell will be 50
$distance=50;
//while the variable 'distance' is less than 50.
//create table cells and populate them with numbers
//until the number is equal to distance
while ($distance <= 250 )
{
       echo "<tr><td bgcolor='#ff4433'>";
       echo $distance;
       echo "</td>";
       echo "<td bgcolor='#ff4433'>";
       echo $distance/10;
       echo "</td></tr>";
       //distance = distance + 50
       $distance+=50;
}
?>
Post Reply