FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Help





assex
Hi,
I'm not exactly a beginner neither am I an expert at PHP.
I needed help with something:
I want to make one page called 'animals.php' in my Web Site and I want to show different content on the page, depending on the letter I choose. Suppose I choose 'a', all the animals with 'a' should appear. The same should happen for 'b' and 'c' and so on all the way till 'z'. How do I do it?
Help would be appreciated.
Stubru Freak
How will you store your animals, how will you choose the letter?
BlackSkad
The easiest would be to store everything in a database. I should say at least 3 columns: id, mark, name. Let mark be the first letter of the name of the animal. This should be faster then only 2 columns and performing a mysql-LIKE on each name to check the first letter.
You then select all animals with an a like this: ( using mysql)
Code:
SELECT * FROM animals WHERE mark = 'a'


In a php-file, it might look like this:
Code:
<?php

//maybe more controle here...
$mark = (isset($_GET['mark'])) ? $_GET['mark'] : 'a';

//query from the database
$sql = "SELECT * FROM animals WHERE mark = {$mark}";
$query = mysql_query($sql);

while ($result = mysql_fetch_assoc ($query)) {
   //do some stuff with each line...
}
?>


If you have questions, ask them. But first, think deeply, and try it yourself, that's the way you learn more...

BlackSkad
n0obie4life
BlackSkad's code is one way, but here's my way.

Code:
<?php

$animals_a = {'ape','apple'}
$animals_b = {'blah'}
.
.
.
.
.
.
$animals_z = {'zebra'}

$letter = $_GET['letter'];

switch ($letter)
{
case 'a':
for ( $x = 0; $x < count($animals_a)-1; $x++ )
{
echo $animals_a[$x];
}
break;
case 'b':
for ( $x = 0; $x < count($animals_b)-1; $x++ )
{
echo $animals_b[$x];
}
break;
.
.
.
.
case 'z':
for ( $x = 0; $x < count($animals_z)-1; $x++ )
{
echo $animals_z[$x];
}
break;
default:
echo "Hacking Attempt";
break;
}


Yes. A little unindented..
Related topics
mysql/php help please
Need some php help.
How to start using PHP? Help!
php HELP!!!
PHP Help Forum
PHP Help please
Php help
php help
PHP HELP
HTML/PHP Help (Contact Box)
Mysql And PHP HELP PLZ
Great php editor
PHP Help Needed
need php help save command
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.