Jump to content

while_query w/ while and foreach echoing two links .. do not know why


OldWest

Recommended Posts

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???

 

 

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?

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 />';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.