Categorize results A-Z

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
efsguitar
Forum Newbie
Posts: 1
Joined: Sat Nov 05, 2005 1:18 pm
Location: Surf City, NC

Categorize results A-Z

Post by efsguitar »

Hello,

I am parsing and returning results from an xml doc and calling it ($name)
Ultimately I want to return all properties A-Z and print out a header for each.
I also need to start a new row every 4 records returned!

Similar to this: http://www.topsail-realty.com/pages/alp ... _list.html

I tried using Xpath on an xsl stylesheet but not being able to reassign variables stumped me.

I would prefer to do it in php anyways.
Below is one poor method I came up with. It works but is not very resourceful. I am sure there is a better way to do this, than to repeat If statement for each letter. I have been paging through google for days, Just need a little direction!

Code: Select all

$drawheader= "open";

foreach ($prop as $name) {
  
  $header= substr($name, 0,1);

if ($header == "A") {

if ($drawheader == "open"){
$drawheader= "close";
echo "<tr class='tdcolorhead'><td colspan='4'>$header</td><tr>";
}/* Close if drawheader*/

$countbreak++ ;
if ($countbreak == "4"){
echo "</tr><tr>";
$countbreak = "0";}/* Close if count break*/
 echo "<td><a href='http://secure.instantsoftwareonline.com/stayUSA/Property.aspx?coid=0008&propid=$propid'>$name</a> </td>";

}/* Close if header*/
}/* Close loop*/

If you think this would be the only efficient way, please let me know.
Any help would be appreciated!

thanks, Nick
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

$array = array("a","b","c","d","e","f","g","h","i","j","k","l","m");

foreach($array AS $letter)
{
   // do your thang
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

for ($x=ord("a"); $x<=ord("z"); $x++) {
$current_letter=chr($x);
}
Post Reply