timmah1 Posted March 7, 2009 Share Posted March 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148359-solved-directory-listing-by-letter/ Share on other sites More sharing options...
timmah1 Posted March 7, 2009 Author Share Posted March 7, 2009 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148359-solved-directory-listing-by-letter/#findComment-778904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.