Jump to content

First letter query and display


adman4054

Recommended Posts

Hi --

I'm using the following code to display an alphabetical list at the top of a long page. It displays "A",  "B", "C" and so on as links.

<ul style="list-style: none; display:inline;">
				<? foreach (range('A','Z') as $i): ?>
					<li style="width: 18px;display:inline;"><a style="padding: 0;margin: 0;background: none;" href="?first_letter=<?= $i ?>"><?= $i ?></a></li>
				<? endforeach ?>
			</ul>

I'm trying to add this code to display the appropriate letter between the results and to move the page to the letter that is clicked, like an anchor tag, but I can't seem to make it work

$firstletter = substr($row['name'], 0,1);
		if ($firstletter != $firstletter) {
			$firstletter = $firstletter;
			echo "<tr>
						<td class='firstletter' colspan='6'><h3 class='firstletter' id='$firstletter'>$firstletter</h3></td>
					</tr>";

My query and "while" code:

include("../inc/conn.php");		  
				//$query = mysql_query("SELECT company.companyName t1, company.companyID t2, companycontact.contactID t3 from company, companycontact where company.companyID=companycontact.companyID ORDER BY company.companyName");
				
				$query = mysql_query("SELECT company.companyName t1, company.companyID t2 from company ORDER BY company.companyName");
				$num_rows=mysql_num_rows($query);       
					   
			    echo "<table width='650' align='center'>";
                echo "<tr>"; 
                echo "<td colspan='2'> <div class='headerText'>".$num_rows." Active Companies: </div><div class='bodyTxt'><i>(click to view contact and listing details)</i></div><br></td>";
                echo "</tr>";
                echo "<tr> <td width='37'></td>";
                echo "<td width='601' align='left' valign='top' class='bodyTxt'>";
		  
		  
								echo "<table>";
								echo "<tr><td class='headerText'>Company ID</td><td class='headerText'>Company Name</td><tr>";

								//$exists = 0;
								while($company = mysql_fetch_array($query)){
								echo "<tr><td width='125' class='bodyTxt'>". $company['t2'] ." </td><td class='bodyTxt'><a href='reportCompanyContact.php?companyID=".$company['t2']."' class='adminLink'>". $company['t1']." </a></td></tr>";
								}
								?>




Any help would be greatly appreciated.

 

Thanks

 

 

 

Link to comment
Share on other sites

So you don't want a new page for each link, rather clicking the link scrolls you down the page to that letter?

 

First a thought. What do you do if there's no content for a letter? Disable the letter's link? Show the letter heading with nothing under it?

 

For the scrolling you do want anchors - the link you have now goes to a separate page.

= $i ?>
Your H3s are already set up to work with anchors because you gave them IDs. Oh, and notice how there's no style in there? Use actual CSS rules somewhere, not inline styling: it'll save you not just bandwidth but time later when making changes to it.

 

As for the code you want to add, it's not quite right. See that it uses $firstletter everywhere? That won't help you do anything.

Set $currentletter = "" before the while loop. That's what you'll be comparing each letter against. Inside the loop you can grab the $firstletter (though you have to look at $company['t1'] instead of $row['name']). By the way, fix those column names if you still can: "t1" and "t2" are meaningless.

Then insert the rest of the code (substituting in $currentletter where appropriate) before the output you already have in place, remembering to add the } that's missing from what you posted.

 

If you're having problems after that, please post the full code you have and explain what problem you're having.

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.