Jump to content

[SOLVED] Directory listing by letter


timmah1

Recommended Posts

I'm trying to do a listing, but showing them in a table with the letter on top then any listings beginning with that letter on the bottom.

Meaning, I want to grab the first letter of the company name

        <?php
		$coupon = "SELECT company FROM coupons";
		$listing1 = mysql_query($coupon);
		while ($c = mysql_fetch_array($listing1))	{

			$name = $c['company'];

			$k = substr($name, 0, 1);		

	?>

 

Then, do another query and list all the company's that start with that letter

<?php
            $coupon2 = "SELECT * FROM coupons WHERE company  = '$k'";
		$listing2 = mysql_query($coupon2);
		$numrows2 = mysql_num_rows($listing2);
			if($numrows2 == 0) {
				echo "Nothing";
			}
			while ($c2 = mysql_fetch_array($listing2))	{

			echo $c2['company']."<br>";
		}
		?>

 

Right now, I'm getting no results, even though I have 2 listings with the letter K.

 

I'm sure there's an easier way to do this, but I can't figure it out.

 

Any help would be greatly appreciated.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/148359-solved-directory-listing-by-letter/
Share on other sites

I figured it out

 

Here is my solution

<?php
            $coupon2 = "SELECT * FROM coupons WHERE substring(company, 1, 1) = 'K'";
		$listing2 = mysql_query($coupon2);
		$numrows2 = mysql_num_rows($listing2);
			if($numrows2 == 0) {
				echo "Nothing";
			}
			while ($c2 = mysql_fetch_array($listing2))	{

			echo $c2['company']."<br>";
		}
		?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.