OldWest Posted December 4, 2010 Share Posted December 4, 2010 My code IS working, but there is a problem. I am only trying to echo the link and category name, but I am getting two urls which I cannot resolve. Some hours into this now and frustrated, so any tips are welcome: For some reason the id is also echoing a full url why? Current code: <?php $queryCats = "select id, cateName from ads_cate"; $resultCats = mysql_query($queryCats); while ($category = mysql_fetch_array($resultCats, MYSQL_ASSOC)) { foreach ($category as $cat_name) { echo '<a href=' . "search_list.php?type=items&keyword=&cate=" . $category['id'] . ">" . $cat_name . '</a><br />'; } } ?> Current Output????? <a href=search_list.php?type=items&keyword=&cate=1>1</a> <a href=search_list.php?type=items&keyword=&cate=1>Appliances</a> <a href=search_list.php?type=items&keyword=&cate=2>2</a> <a href=search_list.php?type=items&keyword=&cate=2>Automobiles / Trucks</a> .... WHy is the id also echoing? I am not printing it to the url??? Quote Link to comment https://forums.phpfreaks.com/topic/220616-while_query-w-while-and-foreach-echoing-two-links-do-not-know-why/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 4, 2010 Share Posted December 4, 2010 Your while loop is fetching each row from the result set into $category as an array. So, since you are SELECTing id and cateName in your query statement, you are getting $category['id'] and $category['cateName''] from each row. When you use a foeach() loop on $category, it loops twice, first for the $category['id'] element of $category and second with the $category['cateName''] element of $category from the current row. What are you trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/220616-while_query-w-while-and-foreach-echoing-two-links-do-not-know-why/#findComment-1142790 Share on other sites More sharing options...
OldWest Posted December 4, 2010 Author Share Posted December 4, 2010 I only want to echo the category name. not the added id number links.. But the id needs to be in the url as I have it.. The links work fine, i just need to get rid of the extra id link loop Quote Link to comment https://forums.phpfreaks.com/topic/220616-while_query-w-while-and-foreach-echoing-two-links-do-not-know-why/#findComment-1142793 Share on other sites More sharing options...
PFMaBiSmAd Posted December 4, 2010 Share Posted December 4, 2010 get rid of the extra id link loop Then just use the while() loop. Someone already told you the while loop gives you $category['id'] and $category['cateName''] Quote Link to comment https://forums.phpfreaks.com/topic/220616-while_query-w-while-and-foreach-echoing-two-links-do-not-know-why/#findComment-1142794 Share on other sites More sharing options...
OldWest Posted December 4, 2010 Author Share Posted December 4, 2010 PFMaBiSmAd, I'm sorry for wasting your time. You're right. I've been on the pc for 10 hours straight like mud mind right now.. But that's for the refresher. It's working now. All I needed to do was remove the foreach(). $queryCats = "SELECT id, cateName FROM ads_cate"; $resultCats = mysql_query($queryCats); while ($category = mysql_fetch_array($resultCats, MYSQL_ASSOC)) { echo '<a href="'."/search_list.php?type=items&keyword=&cate=".$category['id'].'">' . $category['cateName'] . '</a><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/220616-while_query-w-while-and-foreach-echoing-two-links-do-not-know-why/#findComment-1142796 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.