Basic foreach help

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
lazystudent
Forum Newbie
Posts: 5
Joined: Mon Feb 02, 2009 7:21 am

Basic foreach help

Post by lazystudent »

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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Basic foreach help

Post by papa »

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>
 
What you need to do now is to figure out how to display 1-26 instead 0-25.
lazystudent
Forum Newbie
Posts: 5
Joined: Mon Feb 02, 2009 7:21 am

Re: Basic foreach help

Post by lazystudent »

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

Post by Mark Baker »

lazystudent wrote:So that'll do a to z but how do you create a column going 1 to 27 next to it
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.

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

Post by lazystudent »

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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Basic foreach help

Post by papa »

You are really a lazy student arent you ? :)

Have you installed php on your computer?
lazystudent
Forum Newbie
Posts: 5
Joined: Mon Feb 02, 2009 7:21 am

Re: Basic foreach help

Post by lazystudent »

LOL i had no idea i had to install PHP i just downloaded and installed it

should that make a difference
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Basic foreach help

Post by papa »

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

Post by lazystudent »

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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Basic foreach help

Post by papa »

Ok, whatever works for you :)

So your alphabet is 30 characters long ?
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Basic foreach help

Post by Skoalbasher »

lol.

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>
 
Post Reply