hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : alphabetical list separating...
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

alphabetical list separating...

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 04-15-2006, 01:25 AM
Steven Steven is offline
I like ice cream
 
Join Date: Mar 2003
Location: California USA
Posts: 11,636

alphabetical list separating...


I have a database with a listing of campground names... I want to separate them with a letter on the outputing html..

for example i have:

Quote:
Acorn Camp East
Acorn Camp West
Agnew Horse Camp
Agnew Meadow
Almanor
Angel Island SP
Antlers
Anza-Borrego Desert SP
Arroyo Seco
Aspen Group
Aspen Groupyo
Aspen Hollow
Auburn Sra Boat
Badger Flats
Bailey Cove
Barton Flats
Belknap
Benbow Lake SRA
Big Basin Redwoods SP
Big Cove
i want it to look like this

Quote:
A

Acorn Camp East
Acorn Camp West
Agnew Horse Camp
Agnew Meadow
Almanor
Angel Island SP
Antlers
Anza-Borrego Desert SP
Arroyo Seco
Aspen Group
Aspen Groupyo
Aspen Hollow
Auburn Sra Boat

B

Badger Flats
Bailey Cove
Barton Flats
Belknap
Benbow Lake SRA
Big Basin Redwoods SP
Big Cove
i am currently just using a while loop and echo. I thought about using substr to get the first letter and then using a for loop or something, but I can not formulate how to do this exactly.

any suggestions would be great

__________________
Steven Ciaburri | Proactive Linux Server Management - Rack911.com | 1.855.RACK911
System Administration Extraordinaire

Managed Dedicated Servers, Linux Server Management, Disaster Recovery, Server Security Audits

Reply With Quote


Sponsored Links
  #2  
Old 04-15-2006, 01:33 AM
arkin arkin is offline
Web Hosting Guru
 
Join Date: Feb 2003
Location: L.A. C.A.
Posts: 334
I'd do it using order by `name` ASC.

Then i'd use,

if ($name{1}!=$cache_name) { echo "<h1>$name{1}</h1>";$cache_name=$name{1}; }

$name{1} being the substr, $name being the returned name row.

Its late, this is just an outline.

Reply With Quote
  #3  
Old 04-15-2006, 02:05 AM
Vdevelopers Vdevelopers is offline
Junior Guru Wannabe
 
Join Date: Sep 2004
Location: Rohnert Park, CA
Posts: 97
PHP Code:
echo "<h1>A</h1><br>\n\r";
for ( 
$i=0$i<count($sortedarray); $i++ ) {
  if ( 
$i && substr($sortedarray[$i], 01) != substr($sortedarray[$i-1], 01) ) 
    echo 
"<h1>" substr($sortedarray[$i], 01) . "</h1><br>\n\r";
  echo 
$sortedarray[$i] . "<br>\n\r";

Where, of course, $sortedarray refers to your array of items that are already sorted alphabetically. There are better ways to do it, and far more elegant ways, but I haven't reached programmer nirvana yet and so the elegance factor in my code is zilch. But it works.

Reply With Quote
Sponsored Links
  #4  
Old 04-15-2006, 03:52 AM
RH Swaroop RH Swaroop is offline
WHT Addict
 
Join Date: Feb 2006
Posts: 135
Quote:
Originally Posted by Vdevelopers
PHP Code:
echo "<h1>A</h1><br>\n\r";
for ( 
$i=0$i<count($sortedarray); $i++ ) {
  if ( 
$i && substr($sortedarray[$i], 01) != substr($sortedarray[$i-1], 01) ) 
    echo 
"<h1>" substr($sortedarray[$i], 01) . "</h1><br>\n\r";
  echo 
$sortedarray[$i] . "<br>\n\r";

Where, of course, $sortedarray refers to your array of items that are already sorted alphabetically. There are better ways to do it, and far more elegant ways, but I haven't reached programmer nirvana yet and so the elegance factor in my code is zilch. But it works.
FYI, that's a pretty good piece of code!

__________________
Swaroop Hegde
Racked Hosting LLC
Top Quality SHOUTcast Hosting + Dedicated Servers @ low prices
SCPanel is now a product of Racked Hosting LLC

Reply With Quote
  #5  
Old 04-15-2006, 09:10 AM
dgeorge dgeorge is offline
Junior Guru Wannabe
 
Join Date: Jun 2005
Posts: 73
ok, you said you were currently using a while loop with an echo. I know this isnt correct syntax but you should get the idea


Code:
$strCurrentLetter = "";
$strThisLetter = "";

while($arrRecordset){
	
	$strThisLetter = strtoupper(substr($strRecord, 0,1));

	if($strThisLetter != $strCurrentLetter){
		$strCurrentLetter = $strThisLetter;
		echo '<h1>'.$strCurrentLetter.'</h1><br>';
	}

	echo $strRecord.'<br>';
}

__________________
Dorian George

Reply With Quote
  #6  
Old 04-15-2006, 11:14 AM
Steven Steven is offline
I like ice cream
 
Join Date: Mar 2003
Location: California USA
Posts: 11,636
Quote:
Originally Posted by Vdevelopers
PHP Code:
echo "<h1>A</h1><br>\n\r";
for ( 
$i=0$i<count($sortedarray); $i++ ) {
  if ( 
$i && substr($sortedarray[$i], 01) != substr($sortedarray[$i-1], 01) ) 
    echo 
"<h1>" substr($sortedarray[$i], 01) . "</h1><br>\n\r";
  echo 
$sortedarray[$i] . "<br>\n\r";

Where, of course, $sortedarray refers to your array of items that are already sorted alphabetically. There are better ways to do it, and far more elegant ways, but I haven't reached programmer nirvana yet and so the elegance factor in my code is zilch. But it works.
It will just keep repeating a over and over.

__________________
Steven Ciaburri | Proactive Linux Server Management - Rack911.com | 1.855.RACK911
System Administration Extraordinaire

Managed Dedicated Servers, Linux Server Management, Disaster Recovery, Server Security Audits

Reply With Quote
  #7  
Old 04-15-2006, 11:21 AM
RH Swaroop RH Swaroop is offline
WHT Addict
 
Join Date: Feb 2006
Posts: 135
*

Quote:
Originally Posted by Steven
It will just keep repeating a over and over.
why would it repeat over and over?

I apologise if I overlooked something there.

__________________
Swaroop Hegde
Racked Hosting LLC
Top Quality SHOUTcast Hosting + Dedicated Servers @ low prices
SCPanel is now a product of Racked Hosting LLC

Reply With Quote
  #8  
Old 04-15-2006, 05:07 PM
Steven Steven is offline
I like ice cream
 
Join Date: Mar 2003
Location: California USA
Posts: 11,636
im probably going to scrap the project getting tired of the frustration

__________________
Steven Ciaburri | Proactive Linux Server Management - Rack911.com | 1.855.RACK911
System Administration Extraordinaire

Managed Dedicated Servers, Linux Server Management, Disaster Recovery, Server Security Audits

Reply With Quote
  #9  
Old 04-15-2006, 05:12 PM
Vdevelopers Vdevelopers is offline
Junior Guru Wannabe
 
Join Date: Sep 2004
Location: Rohnert Park, CA
Posts: 97
Steven, there is no reason it would repeat over and over unless it reset $i in every iteration . Have you tried the code?

PHP Code:
echo "<h1>A</h1><br>\n\r";
for ( 
$i=0$i<count($sortedarray); $i++ ) {
  if ( 
$i && substr($sortedarray[$i], 01) != substr($sortedarray[$i-1], 01) ) {
    echo 
"<h1>" substr($sortedarray[$i], 01) . "</h1><br>\n\r";
  }
  echo 
$sortedarray[$i] . "<br>\n\r";

That may be better structured for comprehension and readability.


Last edited by Vdevelopers; 04-15-2006 at 05:17 PM.
Reply With Quote
  #10  
Old 04-15-2006, 05:51 PM
Steven Steven is offline
I like ice cream
 
Join Date: Mar 2003
Location: California USA
Posts: 11,636
Actual code:

PHP Code:
<?PHP
include('includes/globals.php');
$query "SELECT campcity,campname,campid FROM camps ORDER BY campname ASC";

$result mysql_query($query);
if (!
$result) {
   die(
'Invalid query: ' mysql_error());
}
while (
$row mysql_fetch_array($result)) {
if (
$row['0'] == '') {
$row[0] = 'NULL';
}
echo 
"<a href=\"index.php?id=$row[2]\">$row[1] - $row[0]</a><br>";
        }
mysql_free_result($result);

?>
outputs something like this:

Quote:
Acorn Camp East - New Hogan Lake
Acorn Camp West - New Hogan Lake
Agnew Horse Camp - NULL
Agnew Meadow - NULL
Almanor - Lassen
Angel Island SP - NULL
Antlers - Shasta-Trinity
Anza-Borrego Desert SP - NULL
Arroyo Seco - Los Padres
Aspen Group - Tahoe
Aspen Groupyo - NULL
Aspen Hollow - Sequoia
Auburn Sra Boat-In - NULL
Badger Flats - Sierra
Bailey Cove - Shasta-Trinity
Barton Flats - San Bernardino
Belknap - Sequoia
Benbow Lake SRA - NULL
Big Basin Redwoods SP - NULL
Big Cove - Plumas
Big Meadow - Stanislaus
Big Pine Canyon - NULL
Camp name followed by the city.

What i need is it seperated by letter like:

Quote:
A

Acorn Camp East - New Hogan Lake
Acorn Camp West - New Hogan Lake
Agnew Horse Camp - NULL
Agnew Meadow - NULL
Almanor - Lassen
Angel Island SP - NULL
Antlers - Shasta-Trinity
Anza-Borrego Desert SP - NULL
Arroyo Seco - Los Padres
Aspen Group - Tahoe
Aspen Groupyo - NULL
Aspen Hollow - Sequoia
Auburn Sra Boat-In - NULL

B

Badger Flats - Sierra
Bailey Cove - Shasta-Trinity
Barton Flats - San Bernardino
Belknap - Sequoia
Benbow Lake SRA - NULL
Big Basin Redwoods SP - NULL
Big Cove - Plumas
Big Meadow - Stanislaus
Big Pine Canyon - NULL

C
...
...
..

__________________
Steven Ciaburri | Proactive Linux Server Management - Rack911.com | 1.855.RACK911
System Administration Extraordinaire

Managed Dedicated Servers, Linux Server Management, Disaster Recovery, Server Security Audits

Reply With Quote
  #11  
Old 04-15-2006, 06:11 PM
Vdevelopers Vdevelopers is offline
Junior Guru Wannabe
 
Join Date: Sep 2004
Location: Rohnert Park, CA
Posts: 97
PHP Code:
<?PHP
include('includes/globals.php');
  
$query "SELECT campcity,campname,campid FROM camps ORDER BY campname ASC";
  
$result mysql_query($query);

  if ( 
$result && mysql_num_rows$result ) ) {
    echo 
"<h1>A</h1><br>";
    for ( 
$i=0$i<mysql_num_rows$result ); $i++ ) {
      if ( 
$i && substr(mysql_result$result$i"campname" ), 01) != substr(mysql_result$result$i 1"campname" ), 01) )
        echo 
"<h1>" substr(mysql_result$result$i"campname" ), 01) . "</h1><br>\n\r";

      echo 
"<a href=\"index.php?id=" mysql_result$result$i"campid" ) . "\">" mysql_result$result$i"campname" );
      if ( 
mysql_result$result$i"campcity" ) ) {
        echo 
" - " mysql_result$result$i"campcity" );
      }
      echo 
"</a><br>";
    }
    
mysql_free_result$result );
  } else {
    die( 
"Query failed:" mysql_error() );
  }
?>
Fin.

Reply With Quote
  #12  
Old 04-15-2006, 06:40 PM
Steven Steven is offline
I like ice cream
 
Join Date: Mar 2003
Location: California USA
Posts: 11,636
Thanks bud! it works now

__________________
Steven Ciaburri | Proactive Linux Server Management - Rack911.com | 1.855.RACK911
System Administration Extraordinaire

Managed Dedicated Servers, Linux Server Management, Disaster Recovery, Server Security Audits

Reply With Quote
  #13  
Old 04-15-2006, 06:41 PM
Vdevelopers Vdevelopers is offline
Junior Guru Wannabe
 
Join Date: Sep 2004
Location: Rohnert Park, CA
Posts: 97
Glad I could help.

Reply With Quote
  #14  
Old 04-15-2006, 07:49 PM
thartdyke thartdyke is offline
Junior Guru
 
Join Date: Jul 2004
Location: Allentown, PA
Posts: 200
That code will fail if you have no camps beginning with the letter 'A' (it always spits out the header).

I prefer to let the database do the work in cases like this. You can use one query for all the initial letters that you have:

$queryHead = "SELECT DISTINCT LEFT(campname, 1) AS head FROM camps ORDER BY head";

After that, step through each row, and look up the camps that start with that name. In full:

PHP Code:
include('includes/globals.php');
  
$queryHead "SELECT DISTINCT LEFT(campname, 1) AS head FROM camps ORDER BY head";
  
$resultHead mysql_query($queryHead);
  
  while (
$rowHead(mysql_fetch_array($resultHead) {
      echo 
"<h1>" $rowHead['head'] . "</h1><br>\n";   /* This is the single character header */
    
    
$query "SELECT campcity,campname,campid FROM camps WHERE LEFT(campname, 1) = \'" $rowHead['head'] . "\' ORDER BY campname ASC";
    
$result mysql_query($query);
    
    while (
$row(mysql_fetch_array($result) {
        echo 
"<a href=\"index.php?id=" $row['campid'] . "\">" $row['campname'] . " - " $row[campcity] . "</a><br>\n";
    }
  }
  
  
$query "SELECT campcity,campname,campid FROM camps ORDER BY campname ASC";
  
$result mysql_query($query); 

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Pingdom Talks Top Web Hosting Cities and Countries Web Hosting News 2013-03-27 18:49:54
Web Hosts, IT Services Firms Make Inc 5000 Fastest Growing Companies Web Hosting News 2012-09-19 16:45:40
Why you should get on the blogging bandwagon Blog 2011-11-03 16:26:00
Web Hosts Climb Inc. 5000 List of Fastest-Growing US Companies for 2011 Web Hosting News 2011-08-23 20:52:20
Web Hosts iWeb and Tenzing Make PROFIT's 100 Fastest Growing Companies List Web Hosting News 2011-06-10 15:13:57


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?