Jump to content

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>";
		}
		?>

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.