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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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