Jump to content


Photo

alphabetical mysql results?


  • Please log in to reply
16 replies to this topic

#1 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 28 February 2006 - 07:52 AM

Hi, I was wondering if someone could point me in the right direction. I'm trying to have my mysql content listed on a single page in alphabetical with "A B C D E F ect.." at the top of the page. You click A and you'll get all items starting with A. Does anyone know of a tutorial I could use to learn how to do this?

#2 klitscher

klitscher
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 February 2006 - 08:25 AM

[!--quoteo(post=350165:date=Feb 28 2006, 12:52 AM:name=Maverickb7)--][div class=\'quotetop\']QUOTE(Maverickb7 @ Feb 28 2006, 12:52 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi, I was wondering if someone could point me in the right direction. I'm trying to have my mysql content listed on a single page in alphabetical with "A B C D E F ect.." at the top of the page. You click A and you'll get all items starting with A. Does anyone know of a tutorial I could use to learn how to do this?
[/quote]

Try using the sql code:
SELECT *
FROM `your_table`
WHERE `name`
LIKE 'a%'
ORDER BY `name` ASC

you could pass in a variable for the letter you would like to sort by from a link.

so clicking on sort.php?letter=a would lead to:

$letter = $_GET['letter'];
$sql = 'SELECT * '
        . ' FROM `your_table` '
        . ' WHERE `name` LIKE  \'' . $letter . '%\''
        . ' ORDER BY `name` ASC'; 

$result = mysql_query($sql);


hope that helps

#3 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 28 February 2006 - 09:03 AM

Thanks for the help. I got it working great. :)

#4 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 28 February 2006 - 09:56 AM

I have one follow-up question if anyone could be so kind to answer it. Does anyone know what code I would use to have all items starting with numbers to be listed as #?

#5 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 01 March 2006 - 07:52 PM

Anyone?

#6 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 03 March 2006 - 08:59 PM

Still looking for help.

#7 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 05 March 2006 - 07:15 PM

Still no help? c'mon people :( lol I've been looking everywhere for a solution with no luck. :(

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 05 March 2006 - 07:28 PM

$string = "whatever your string is ....";
$first = {$string[0]};
if ($first>0) {
    echo "#";
}
echo $string;
?>

Legend has it that reading the manual never killed anyone.
My site

#9 Sasuun

Sasuun
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 05 March 2006 - 07:47 PM

perhaps you could use the function is_numeric()?

#10 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 April 2007 - 04:05 PM

How would I do that?

#11 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 07 April 2007 - 04:11 PM

How would I do that?


I bet the manual explains ... http://ca.php.net/ma....is-numeric.php
Legend has it that reading the manual never killed anyone.
My site

#12 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 April 2007 - 04:46 PM

Alright, I understand how it works and what it does. But could you point me in the right direction. I'm having trouble using # to display all numeric results. =( how would I use that with the sql line?

#13 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 April 2007 - 05:36 PM

let's say I do what klitscher suggested and used his code. How would I tell the sql statement to grab all rows that start with a numeric vale? I want to type sort.php?letter=# and have it display all numeric results. Can anyone show me how to do this?

#14 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 April 2007 - 06:58 PM

anyone? =(

#15 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 April 2007 - 10:31 PM

Well, I have to leave for a couple of hours so hopefully when I return someone was able to offer their help. I'm kinda stuck on this even though its a pretty simple task. If you could help I would be really grateful.

#16 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 08 April 2007 - 07:52 AM

It's been a few hours with no response. Does anyone know of another way to do what I want perhaps?

#17 HaLo2FrEeEk

HaLo2FrEeEk
  • Members
  • PipPipPip
  • Advanced Member
  • 724 posts

Posted 08 April 2007 - 08:41 AM

I think you meant that you want all values with a number at the beginning to start with a # sign, so basically, 7teen would be listed as #7teen?  Hmm...try this:

<?php
$string = "7teen";
$first = substr($string, 0, 1);
if(is_numeric($first)) {
  $first = "#" . $first;
  $string = $first . substr($string, 1, strlen($string));
  }
echo $string;
?>

Hope this helps you out.

edit: all you have to do is read in your array in a foreach loop like this (I use an example array):

<?php
$string_arr = array("123", "teen", "7teen", "HaLo2FrEeEk", "Alex", "4lex");
foreach($string_arr as $string) {
  $first = substr($string, 0, 1); // Gets the first character of the string
  if(is_numeric($first)) { // Checks to see if $first is a number
    $first = "#" . $first; // This would set $first to #7
    $string = $first . substr($string, 1, strlen($string)); // This appends the last characters to the new $first;
    }
  echo $string . "<br />";
  }
?>

This will output:

#123
teen
#7teen
HaLo2FrEeEk
Alex
#4lex

Since your query results are sorted already, the order won't be a problem.
http://infectionist....HaLo2FrEeEk.png

^ I wrote the code for this myself, with the help of fert: coding, and KJdog23: for making the template.  These are live stats from my forum, instantly updated onload.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users