Jump to content

show alphanumric list of DB entries fails if nothing starts with one letter


jasonc

Recommended Posts

I have got this far that my code shows the list but say nothing starts with the letter 'y' then all of the 'x' and 'z' are grouped in the list.

 

can anyone suggest a better way to have the list show correctly.

 

the 'multiItemBoxes' css class is a rounded box that shows around each letter group.

 

here is me code.

					//
					$letters = array(); $letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
					$L = 1;
					?><div class="multiItemBoxes"><? // place first part of DIV.
					$previousLetter = "/"; // set previousLetter to the letter before '0' zero. used as a dummy string.
							while($results = mysql_fetch_assoc($getResults)) {
											if ($previousLetter == "/") {
														?><strong style="font-size: 15pt;"><? echo(strtoupper($results['productTitle'][0])); ?></strong><br /><?
														}
									if (strtoupper($results['productTitle'][0]) == $letters[$L]) {
									?></div><? // this is the ending of the DIV. If needed.
									?><br class="clearfloat" />
											<? if (strtoupper($results['productTitle'][0]) != " ") {
											?><div class="multiItemBoxes"><strong style="font-size: 15pt;"><? echo(strtoupper($results['productTitle'][0])); ?></strong><br /><?
											$L++;
											}
									// this is the extra DIV start if not at end of DB list.
									}
									?><a href="<? echo("cart.php?query=" . $results['Pid']); ?><? echo("&"); ?>i=y"><? echo(htmlspecialchars($results['productTitle'])); ?></a><br /><?
									$previousLetter = $results['productTitle'][0];
							} // end of while
							?></div><br class="clearfloat" /><?
					//

Link to comment
Share on other sites

Why not use ORDER BY in your SELECT statement to ensure they come out alphabetically.

 

Then run a test on the 1st letter of the result.  If it is the same as the previous one, just echo out the result, if not then echo out the letter to show a new group, then any entries for this group.

 


$sql="SELECT producttitle FROM table ORDER BY producttitle ASC";
$sql=mysql_query($sql);
while($row=mysql_fetch_array($sql)){

if(substr($row['producttitle'],0,1)==$letter){
echo $row['producttitle'];
$letter=substr($row['producttitle'],0,1);
}
else {
$letter=substr($row['producttitle'],0,1);
echo $letter;
echo $row['producttitle'];
}

 

This way if a letter doesn't exist it will skip it.

 

Link to comment
Share on other sites

when i used my code if there was nothing starting with a Y then x and z showed in the same section.

i did sort alphnumric when doing the query.

 

what i am after is something like this

 

 

A

a1

a2

a3

 

B

b1

b2

b4

 

D

d2

d3

d4

 

 

in the above if nothing starts with C then it goes to the next one.

 

but in my code

it showed something like this

 

 

 

A

a1

a2

a3

 

B

b1

b2

b4

d2

d3

d4

 

 

joining both B and D

 

what other method could i use instead ?

Link to comment
Share on other sites

The method described in my post would do this.  It would output the first letter as a title then any records which match below it.

 

Have you tried the code I gave?  You would need to amend the details in the SELECT statement but it should acheive what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.