Basic foreach help
Moderator: General Moderators
-
lazystudent
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 02, 2009 7:21 am
Basic foreach help
I would like so help with creating a basic table that has numbers 1 - 27 on one column and letters a - z on the other, but i want it created by foreach or a loop,
can anyone supply me with code for this
can anyone supply me with code for this
Re: Basic foreach help
Code: Select all
<table border="1">
<?php
$letters = range('a', 'z');
for($i=0; $i<count($letters); $i++) {
echo "<tr>\n";
echo "<td>".$i."</td> ";
echo "<td>".$letters[$i]."</td>\n";
echo "</tr>\n";
}
?>
</table>
-
lazystudent
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 02, 2009 7:21 am
Re: Basic foreach help
So that'll do a to z but how do you create a column going 1 to 27 next to it
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Basic foreach help
If you used the code that was posted, you should get 0..25 in a column. It's up to you to modify it to get 1..26 instead.lazystudent wrote:So that'll do a to z but how do you create a column going 1 to 27 next to it
But why to 27? There isn't a 27th letter of the alphabet in English.
-
lazystudent
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 02, 2009 7:21 am
Re: Basic foreach help
I have pasted this into notepad ++ but it has come up with
"; echo " ".$letters[$i]." \n"; echo "
\n"; } ?>
This isn't a table with letters down 1 side and numbers on another, am i doing something wrong
"; echo " ".$letters[$i]." \n"; echo "
\n"; } ?>
This isn't a table with letters down 1 side and numbers on another, am i doing something wrong
Re: Basic foreach help
You are really a lazy student arent you ? 
Have you installed php on your computer?
Have you installed php on your computer?
-
lazystudent
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 02, 2009 7:21 am
Re: Basic foreach help
LOL i had no idea i had to install PHP i just downloaded and installed it
should that make a difference
should that make a difference
Re: Basic foreach help
What happens when you type "localhost" in your browser's address field?
-
lazystudent
- Forum Newbie
- Posts: 5
- Joined: Mon Feb 02, 2009 7:21 am
Re: Basic foreach help
i got it working!!
but now i've been told you use
<?foreach (range(1, 30) as $temp_number) {
echo "$temp_number<br>";
}?>
this in the code
but now i've been told you use
<?foreach (range(1, 30) as $temp_number) {
echo "$temp_number<br>";
}?>
this in the code
Re: Basic foreach help
Ok, whatever works for you 
So your alphabet is 30 characters long ?
So your alphabet is 30 characters long ?
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: Basic foreach help
lol.
Try this.
Try this.
Code: Select all
<table border="1">
<?php
$letters = range('a', 'z');
$j = 1;
for($i=0; $i<count($letters); $i++) {
echo "<tr>\n";
echo "<td>".$j."</td> "; // here should work?
echo "<td>".$letters[$i]."</td>\n";
echo "</tr>\n";
$j++;
}
?>
</table>