Alphabetical sorting 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

Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Alphabetical sorting help....

Post by Smackie »

alright i cant seem to get anywhere on this little alphabetical thing..
im looking for someone to help me code or even has a code that will work on what i need it for.... alright here is what i would like this code to do...

i would like for it to have like a place where users(already signed in) can post there poems on my site.. but the thing is i would like for it to have at the top of the pages
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z
and for where when users submit there poems it will look at there names and have it alphabetical there first name (example: Adam would go to a and eve would go to e...... and Sam would go before Smackie) but thats what is stoping me :? :?: i been trying to do this for 2 weeks straight and cant seem to come up with anything i even look at the php manual to try and help me but i couldnt get anything done... please someone help me.....

Thank you in advance
Smackie
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

and you have what done so far?
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Are you trying to do something similar to an address book that you see on some sites? You click a letter and then the screen refreshes with the names that start with that letter in alpabetical order.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

nothing really :oops: because i get a blank in the head and so i go to read the manual and still cant find anything about sorting alphabetical that would help me out or anything unless someone is just hiding it on me.....
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

yeah something like that yeah...
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Can we assume the user list is in a database? If so, what fields and tables are related to it? Have you written any code so far? Post a fragment, it will make it easier.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

typically it'd be like

Code: Select all

SELECT * FROM `users` WHERE `username` REGEXP '^$letter.*' ORDER BY `username`
I believe we've discussed this before.. with you.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

all i got really is the login script really thats it and yes they will be in a db as well..
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

no all you discussed about this with me was just to read the php manual which didnt help one bit
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Let me ask, and I am not trying to be rude, how much experience do you have with PHP and MySQL? I want to know so I can figure out how to answer your questions.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

well not much i mean i learned from a friend well he tought me some of the basics before he got into a car wreck that ended his life but every since then i been trying to learn up on it and well only thing i really made by myself was a login script and a few other things.. so im somewhat of a newbie at it
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Sorry to hear about your friend. Thanks for the honest answer. Some people are afraid to say they are new, always have the courage to be honest. Let me think about it for a little bit and I will post some code and walk you through it.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

well im not other people im straight up forward with my stuff lol i dont hide anything.. but alright cool and thank you very much... i started out with html i really good with html i been working with html for about 2 years now..... and i know alil bit of php and javascript... thats about it...
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

I wrote this quickly but it ought to work enough to give you a place to start. Make sure to replace the DB specific stuff with your own.

Code: Select all

<?php

// This function makes the alphabetic list which the links.
function drawAddressList() &#123;
  $seperator = "-";

  // Loop through all 26 characters.
  for ($i = 1; $i <= 26; $i++) &#123;
    $letter = chr($i + 64);
    echo $seperator . "<a href='&#123;$_SERVER&#1111;'PHP_SELF']&#125;?letter=&#123;$letter&#125;'>&#123;$letter&#125;</a>";
  &#125;  
  echo $seperator . '<br />';
&#125;

// This function gets a parameter or the default.
function getParm($VariableName, $Default = '') &#123;
  return (isset($_GET&#1111;$VariableName])) ? $_GET&#1111;$VariableName] : $Default;	
&#125;

drawAddressList();

// Connect to MySQL - use your user name and password.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) &#123;
   die('Could not connect: ' . mysql_error());
&#125;

// Connects to the database that has the user table.
$db_selected = mysql_select_db('mysql_db', $link);
if (!$db_selected) &#123;
   die ('Can''t use table : ' . mysql_error());
&#125; else &#123;
  $letter = getParm('letter');

  if ($letter != '') &#123;
    $letter_query = "SELECT * FROM `users` WHERE `username` REGEXP '^$letter.*' ORDER BY `username`";
    $letter_result = mysql_query($letter_query);
    if ($letter_result != false) &#123;
    	while ($letter_row = mysql_fetch_assoc($letter_result)) &#123;
    	  // Do something in here with the rows you get.
  		  // Maybe use the information from the user table to fill another recordset with the poems.
    	&#125;
    &#125;
  &#125;  
&#125;

?>
Last edited by smpdawg on Fri Mar 04, 2005 8:38 pm, edited 1 time in total.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

Thank you so much you have been helpful i would have probably pulled out my hair by the time i got half of that down lol but thank you again....


Smackie
Post Reply