0

Hello! I am creating a database driven site that is a directory of businesses. I have a page that displays the categories and a table full of categories in the mysql database. I have a while statement get the categories that start with a certain letter and echo them on the screen. It works, but I just now noticed that it is echoing each one of the categories twice.
Here is the php snippet for that section:

<?php
$letter = $data['letter'];
$getCats = mysql_query("SELECT * FROM categories WHERE catName LIKE '$letter%' ORDER BY catName"); 

while ($cat = mysql_fetch_array($getCats)) {
	$catID = $cat['catID'];
	echo "<li><a href=\"category.php?letID=$letID&catID=$catID\">" . $cat['catName'] . "</a></li>";
}
?>

I have also attached a screenshot of what the output is like. I have looked at the code to try and see what the problem is and can't figure it out.

Thanks ahead of time for any help. I can give more information if necessary.

Key

Attachments Untitled-1.gif 10.87 KB
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by rajarajan07
0

After checking out your code, I can confidently say there's nothing wrong with it. The categories aren't in the database twice, are they? If not, the only thing I'd try is this:

<?php
$letter = $data['letter'];
$getCats = mysql_query("SELECT DISTINCT catName FROM categories WHERE catName LIKE '$letter%' ORDER BY catName"); 

while ($cat = mysql_fetch_array($getCats)) {
	$catID = $cat['catID'];
	echo "<li><a href=\"category.php?letID=$letID&catID=$catID\">" . $cat['catName'] . "</a></li>";
}
?>
0

Yes, I checked the database and it is not in there twice. When I was posting on here I thought of that and checked. When I get back into my office on Monday, I will try what you suggested and let you know if it works.

Thank you,
Key

1

Execute your query seperately in your phpadmin:

SELECT * FROM categories WHERE catName LIKE '$letter%' ORDER BY catName

If it returns more than one record then you can attach DISTINCT keyword to fetch one.

Votes + Comments
His suggestion led me to fixing the problem.
0

Well, I feel stupid now.... :D
I did what rajarajan07 suggested in typing my query into phpmyadmin. It turned out that they are in there twice. I didn't notice it before because there are 400 some odd cats and the repeats are at the end. The query showed me the row numbers and I fixed the problem.

Thank you both for your help,
Key

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.