Alphabetical Navigation Bar

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
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Alphabetical Navigation Bar

Post by Stelios »

Hi everybody,

I would like to add to the web site am constructing an aphabetic navigation bar so that users can browse the database according to the song name. What I actually want to do is to redirect a user when they click on, say letter B, a pgae which is going to display all the songs starting with the letter B. Do I need to create a separate database field in my database stating which letter the song begins with or there is a way I can do it by just extracting the first letter of each song from the field that already exists about the song names. I hope I made myself clear and didnt confuse you.

Thanks a lot!
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

In your select statement do something like

'WHERE title= 'a%'

and you'll get every title that starts with an a
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Tried to do what you say by performing a direct query to my database in the form:

Code: Select all

select song_name from uploads where song_name='f%';
and although there is a song on my database starting with f I get an empty set back as a return of my query....
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

Try

WHERE song_name LIKE 'f%'
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

found the correct statement...it should be

WHERE title LIKE '%b%';
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

if you do '%f%' you get all songs containing and f.

%f will give you ending with an f
f% starting with an f
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Ok, what Ive done is I have created a small script to display all the alphabet letters which is:

Code: Select all

<?php

echo 'This is a test';
echo '<br>';

for ($i = 'A'; $i != 'AA'; $i++) &#123; 
echo '<a href="test_t.php?i='.$i.'">&#1111;'.$i.']</a>'; 
&#125; 

?>
and then another one to display the database entries which is

Code: Select all

<?php

require_once('mysql_connect.php');


$query = "SELECT * FROM uploads WHERE song_name LIKE '$i%' ";
$result = mysql_query ($query);


while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) 
&#123;
	
	echo "<a href=&#123;$row&#1111;'song_name']&#125;>" . ($row&#1111;'song_name']) . "</a>";
&#125;

?>
The script is running but is returning me all the entries of the database. Any idea why? :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$i doesn't exist in your script. print the query..

and using the url variable straight is dangerous.
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

what do you mean it doesnt exist? It does in the previous script...Whats wrong?
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

You have to transfer the data in between your scripts. Knowing a variable in one script doesn't mean you know it in another.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're assuming register_globals is on, when it likely isn't. Echo out the query.

also.. set error_reporting to E_ALL.
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

nevermind, I found it, missing a $_GET variable...thanks anyway...
Post Reply