getting data from database in alphabetically

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
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

getting data from database in alphabetically

Post by Smackie »

Alright im looking for away to search the database for users first letter and put them on a webpage ... like say you got 5 different people posted something but you dont want it all being right there.. but i know you guys are going to get confused so im going to try and break it down for you all.

i want a script or something that when someone posts a poem it will create a username on the letter of the first name
(example: if someone uses Smackie there user name will go under S and will make a link using there username and it will post there poem in that link and every time he makes a new poem it will insert it in the same link but right under the the old poem....) if you can help me please post in here or hit me up on Aim, msn, or yahoo.......... Thank you in advanced

Smackie
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

so all you need is a table poems and a table poets..

and then everytime a poem is posted you also store the poet_id...

and then you generate a page that accepts the first letter.... and then you search like:

select *
from poets inner join poems using (poet_id)
where poets.name like '$firstletter%'

and you're done.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

easier said then done lol see i already got tabes set up but i guess i can redo them thats not hard but making the script is the hard part :oops: lol here is the page i would like this script to work on/with
http://www.hauntedgraveyard.net/Poetry/ ... /index.php
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

As Tim said, just set up two tables that link to each other by the poet's id. The only thing I might add to his query to obtain the results you're after is to add an order by clause to sort the poet's names alphabetically.

feel free to add me up on MSN if you want some help.

Burr
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Google is great for this type of learning..

http://www.google.com/search?hl=en&q=ph ... abetically
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

i already got that done :P lol im now trying to find a way to add a link to it where when a user wants to look at someones poem or poems they just click on a name... but for some reason i cant find away to do that.....
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

here is a link to show you the code so someone can help me

http://www.hauntedgraveyard.net/Poetry/script
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rough example

Code: Select all

<?php

$user = (isset($_GET&#1111;'user']) ? mysql_escape_string(trim($_GET&#1111;'user'])) : '');

$query = mysql_query('SELECT * FROM `users`, `poems` WHERE `poems`.`userid` = `user`.`userid`') or die(mysql_error());

if(!$query || mysql_num_rows($query) < 1)
&#123;
  die('Unknown user or no poems in database.');
&#125;

// display loop
?>
Last edited by feyd on Mon Mar 07, 2005 1:45 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

<? $sql = "SELECT poetname FROM poets WHERE poetid = '$poetid'";
$query = mysql_query($sql);
while($array = mysql_fetch_array($query)) &#123; ?>
<a href="showpoems.php?poetid=<? echo $poetid; ?>"><? echo $array&#1111;'poetname']; ?></a><BR><? &#125; ?>
With poetid coming from the URL.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

neither one of them works :( :?: :!: here are the errors i get

from feyd script
Table 'haunted_poetry.users' doesn't exist
from scrotaye script
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/haunted/public_html/Poetry/poems.php on line 49
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Didn't I mention "rough example" in my post? :? That means you have to change the code to fit your table/database whatever..
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

and thats what i did and still came out looking like that... or something like that....
Post Reply