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

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

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

Member Avatar for rajarajan2017

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.

commented: His suggestion led me to fixing the problem. +1

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

Member Avatar for rajarajan2017

Mark the thread as solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.