IT.COM

Using Arrays & Loops for name lists.

Spaceship Spaceship
Watch
Impact
0
Ok It's pretty easy to make a name list in PHP as long as you know how to use arrays and loops. Here's an example:

PHP:
<?
$names[0] = 'John';
$names[1] = 'Sarah';
$names[2] = 'Steven';
$names[3] = 'Brittany';
$names[4] = 'David';
$number = 5;
$x = 0;
while ($x < $number) {
$namenumber = $x + 1;
echo "Name $namenumber is $names[$x]<br>";
++$x
}
?>

This displays:

Name 1 is John
Name 2 is Sarah
Name 3 is Steven
Name 4 is Brittany
Name 5 is David

On your page. You can always replace the
PHP:
"Name $namenumber is $names[$x]<br>";
with what you want but leave the $namenumber and the $names[$x] alone unless you changed them to begin with. You can also replace or add more names. Just don't forget to add more to the $number. Also since we want it to be set up counting up from one, the $number will always be one greater than the amount of names shown.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
The same array and result with a little less code:
PHP:
<?
$names = array("John", "Sarah", "Steven", "Brittany", "David");
foreach ($names as $index => $value) {
$index++;
echo "Name $index is $value<br>";
}
?>
:webdev:
 
0
•••
Cool. :) I just finished chapter 5 of my book and I am done with beginner's stuff :) I just wrote this funny news flash where you type your name, a city, your favorite ice cream flavor, your favorite pop music star, and the name of a famous river into boxes. Then the PHP script puts it into a story such as:

Los Angeles:
Last evening, pop star Britney Spears was spotted with an unfarmiliar companion. The two were dining on Chocolate at the Amazon Club, popular Los Angeles hangout for the rich and famous. Confidential sources tell us the mystery companion is Alpha, former chauffeur of last year's Grammy Award winner ....
:D

Plus it says Celebrity News Central at the top header and then the date and then Flash Bulletin. :) PHP is cool.
 
0
•••
lol, classic php jokes use the same style of code.

A friend of mine had a php script that basically grabbed your name from the url and says Hey, <name> and below had an image that has a guy pointing at you and below it said "You suck at the internet".
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back