The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Loop question
Discuss Loop question in the PHP Development forum on Dev Shed. Loop question PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 29th, 2012, 11:53 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
Loop question
I need this loop to display the $Year 12 across the page instead of straight down the page as it does now. What am I missing? Thanks in advance for any assistance.
PHP Code:
echo "<tr>";
$space = 1;
$Count = 1;
$Year = 1912;
while ($Year<=2019) {
echo '<th>';
echo "$Year";
echo '</th>';
if ($Count>11) { /*Evaluates the number of years/columns for each row*/
echo '</tr>';
$Count=1;
echo '<tr>';
} // End of if conditional
else ++$Count;
++$Year;
echo "</tr>";
|

September 29th, 2012, 11:58 AM
|
|
|
|
you should have a brace } before the final echo </tr>;
|

September 29th, 2012, 12:04 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
I tried the below and get a syntax error:
PHP Code:
echo "<tr>";
$space = 1;
$Count = 1;
$Year = 1912;
while ($Year<=2019) {
echo '<th>';
echo "$Year";
echo '</th>';
if ($Count>11) { /*Evaluates the number of years/columns for each row*/
echo '</tr>';
$Count=1;
echo '<tr>';
} // End of if conditional
else ++$Count;
++$Year;}
echo "</tr>";
|

September 29th, 2012, 01:53 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
HI your code has no syntax errors. Just make sure you surround it with the php tags. (also if you get errors it tells you in a more precise manner on what line)
PHP Code:
<?php
echo "<tr>";
$space = 1;
$Count = 1;
$Year = 1912;
while ($Year <= 2019) {
echo '<th>';
echo "$Year"; // <--- these quotes are not needed, but do no harm
echo '</th>';
if ($Count > 11) { /* Evaluates the number of years/columns for each row */
echo '</tr>';
$Count = 1;
echo '<tr>';
} // End of if conditional
else
++$Count;
++$Year;
}
echo "</tr>";
?>
|

September 29th, 2012, 05:06 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
ok i've got this. its still a loop question so i didnt make a new post.
in the following when I execute it, i get this:
Rat /> Ox /> Tiger /> Rabbit />
When I just wnt the name from the array. whats missing?
PHP Code:
// Begin loading zodiac characters names
$zodiacNames = array(
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Ram", "Monkey", "Rooster", "Dog", "Pig");
// End loading the names array
echo "<table>";
for ($names = 0; $names < 12; ++$names) {
echo "<td>$zodiacNames[$names] /></td>";
}
Thanks for your support. BTW, this is just a snippet of the whole code. 
|

September 29th, 2012, 05:09 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
look closely at your code mate:
what is the difference between:
Code:
echo "<td>$zodiacNames[$names] /></td>";
and
Code:
echo "<td>$zodiacNames[$names]</td>";
Careful with those rabbits and tigers btw, they tend to not mix very well
|

September 29th, 2012, 05:21 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
thanks got it
|

September 29th, 2012, 05:22 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by maineman thanks got it |
NIce!
|

September 29th, 2012, 07:00 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
Im getting an undefined variable in my echo statement
PHP Code:
// load the zodiac names
$zodiacNames = array(
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Ram", "Monkey", "Rooster", "Dog", "Pig");
// end loading the names array
echo "<table>";
$names = "";
while ($names < 12) {
[COLOR=Red]echo "<td><$zodiacNames[$names]></td>";[/COLOR]
$names++;
}
i dont see it.
|

September 29th, 2012, 07:16 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Hi mate,
this undefined variable-error, is probably more specific. Next time post the complete error.
I can also really recommend that you use a decent editor to write your php in. There are some errors in it that would show up directly in pretty much all decent editors out there.
Install (for instance) Netbeans and use that.
The color tags ([COLOR=Red]) are html elements and should be echoed.
All HTML should be echoed.
But there is something very wrong with those [color] tags
Read this: http://webdesign.about.com/od/htmltags/a/bltags_deprctag.htm
Burn the book that thought you this stuff, or never visit the website that thought you this. You are learning it the wrong way. Separate functionality, content and styling
|

September 29th, 2012, 07:21 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
was trying to highlight the line in question in red to make it stand out, so i used the forums color changer and that is what it inserted, not me.
the error $number not being defined as 0. Now the loop runs just doesn't output the names.
|

September 29th, 2012, 07:30 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by maineman was trying to highlight the line in question in red to make it stand out, so i used the forums color changer and that is what it inserted, not me.
the error $number not being defined as 0. Now the loop runs just doesn't output the names. |
AH that explains the use of that stuff, but that is not needed it can only confuse.
Well what is wrong is the index of names.instead of
make it
PHP Code:
$names = 0; // index would be a better name
Just keep in mind that code wont make anything visible because a <td></td> should be inside of a <tr></tr>
Last edited by aeternus : September 29th, 2012 at 07:32 PM.
|

September 29th, 2012, 07:33 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
i did that and it outputs nothing. here is the currect code.
PHP Code:
// load the zodiac names
$zodiacNames = array(
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Ram", "Monkey", "Rooster", "Dog", "Pig");
// end loading the names array
echo "<table>";
$names = 0;
echo "<tr>";
while ($names < 12) {
echo "<td><$zodiacNames[$names]></td>";
$names++;
}
|

September 29th, 2012, 07:35 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by maineman i did that and it outputs nothing. here is the currect code.
PHP Code:
// load the zodiac names
$zodiacNames = array(
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Ram", "Monkey", "Rooster", "Dog", "Pig");
// end loading the names array
echo "<table>";
$names = 0;
echo "<tr>";
while ($names < 12) {
echo "<td><$zodiacNames[$names]></td>";
$names++;
}
|
Mate do you look at your own code or are you just posting stuff and hoping for someone else to think. Look WHat is missing.! (hint 2 endtags which ones are they)
P.s. if you dont see stuf in your browser, right click, view source and have a look.
Last edited by aeternus : September 29th, 2012 at 07:39 PM.
|

September 29th, 2012, 07:46 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
honestly i do not see it. just started trying to learn and havent much experience lately with tags. I know but i am not just throwing code out there and asking for someone to fix it.
if its 2 end tags I am at a loss. Sorry.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|