Maverickb7 Posted February 28, 2006 Share Posted February 28, 2006 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 Link to comment Share on other sites More sharing options...
klitscher Posted February 28, 2006 Share Posted February 28, 2006 [!--quoteo(post=350165:date=Feb 28 2006, 12:52 AM:name=Maverickb7)--][div class=\'quotetop\']QUOTE(Maverickb7 @ Feb 28 2006, 12:52 AM) [snapback]350165[/snapback][/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: [code]SELECT *FROM `your_table`WHERE `name`LIKE 'a%'ORDER BY `name` ASC [/code]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: [code]$letter = $_GET['letter'];$sql = 'SELECT * ' . ' FROM `your_table` ' . ' WHERE `name` LIKE \'' . $letter . '%\'' . ' ORDER BY `name` ASC'; $result = mysql_query($sql);[/code]hope that helps Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted February 28, 2006 Author Share Posted February 28, 2006 Thanks for the help. I got it working great. :) Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted February 28, 2006 Author Share Posted February 28, 2006 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 #? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted March 1, 2006 Author Share Posted March 1, 2006 Anyone? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted March 3, 2006 Author Share Posted March 3, 2006 Still looking for help. Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted March 5, 2006 Author Share Posted March 5, 2006 Still no help? c'mon people :( lol I've been looking everywhere for a solution with no luck. :( Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 5, 2006 Share Posted March 5, 2006 [code] $string = "whatever your string is ....";$first = {$string[0]};if ($first>0) { echo "#";}echo $string;?>[/code] Quote Link to comment Share on other sites More sharing options...
Sasuun Posted March 5, 2006 Share Posted March 5, 2006 perhaps you could use the function is_numeric()? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 7, 2007 Author Share Posted April 7, 2007 How would I do that? Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 7, 2007 Share Posted April 7, 2007 [quote author=Maverickb7 link=topic=87031.msg568630#msg568630 date=1175961957]How would I do that?[/quote]I bet the manual explains ... http://ca.php.net/manual/en/function.is-numeric.php Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 7, 2007 Author Share Posted April 7, 2007 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? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 7, 2007 Author Share Posted April 7, 2007 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? Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 7, 2007 Author Share Posted April 7, 2007 anyone? =( Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 7, 2007 Author Share Posted April 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 8, 2007 Author Share Posted April 8, 2007 It's been a few hours with no response. Does anyone know of another way to do what I want perhaps? Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 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:[code]<?php$string = "7teen";$first = substr($string, 0, 1);if(is_numeric($first)) { $first = "#" . $first; $string = $first . substr($string, 1, strlen($string)); }echo $string;?>[/code]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):[code]<?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 />"; }?>[/code]This will output:#123teen#7teenHaLo2FrEeEkAlex#4lexSince your query results are sorted already, the order won't be a problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.