Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:48:40+00:00 2026-06-02T13:48:40+00:00

I have the following code: $catsQuery = SELECT id, category FROM categories ORDER BY

  • 0

I have the following code:

$catsQuery = "
        SELECT id, category
        FROM categories
        ORDER BY category
";
$catsSql = mysqli_query($link, $catsQuery);

$cats = array();

mysqli_data_seek($catsSql, 0);
while($rowCats = mysqli_fetch_assoc($catsSql))
{
    $cats[$rowCats['id']]['catName'] = $rowCats['category'];
}

foreach ($cats as $catKey => $cat['$catName'])
{
    $pageContent .= "<p>$catKey | $catName</p>
    ";
}

The two columns id and category contain data like this:

  • id | category
  • 1 | shoes
  • 2 | shirts
  • 3 | hats

What I would like to do is turn the all of the rows in the table categories into a local variable called cats

In the while loop a local array is generated as each rows array key becomes the category id and then a category name is assigned to the key. var_dump($cats) shows that each row is added to the variable as i have described.

But in the foreach, for some reason, each id it returned as a new row, but the name of the category displays as the last possible category name, accross all of the keys.

Why is it that a dump on the variable shows one set of data but a printed loop in this manner of its contents shows this strange hiccup?

Like this:

  • 1 | hats
  • 2 | hats
  • 3 | hats

If anyone could shed some light on what i am doing wrong to cause this, it would be greatly appreciated.

Thank You!!

EDIT:

The functional code created as a result of the answer below:

//categories to local array
$catsQuery = "
    SELECT id, category, catDirPath
    FROM categories
    ORDER BY category
";
$catsSql = mysqli_query($link, $catsQuery);
$cats = array();
mysqli_data_seek($catsSql, 0);
while($rowCats = mysqli_fetch_assoc($catsSql))
{
    $cats[$rowCats['id']]['catName'] = $rowCats['category'];
    $cats[$rowCats['id']]['catPath'] = $rowCats['catDirPath'];
}
// subCats to local array
$subsQuery = "
    SELECT id, subCat, category_id, subDirPath
    FROM subCats, sub_categories
    WHERE subCats.id = sub_categories.sub_id
    ORDER BY category_id
";
$subsSql = mysqli_query($link, $subsQuery);
$subs = array();
mysqli_data_seek($subsSql, 0);
while($rowSubs = mysqli_fetch_assoc($subsSql))
{
    $subs[$rowSubs['id']]['subName'] = $rowSubs['subCat'];
    $subs[$rowSubs['id']]['catId'] = $rowSubs['category_id'];
    $subs[$rowSubs['id']]['subPath'] = $rowSubs['subDirPath'];
}
// loop through categories and if subs exist, add to resultset and display  
foreach ($cats as $catId => $cat)
{
    $pageContent .= "
    <div style='margin-bottom: 10px; border black solid 1px;'>
    <ul>
    <li style='list-style-type: none; margin-bottom: 5px;'><a href='$docPath/category/{$cat['catPath']}/'>{$cat['catName']}</a></li>
    ";
    foreach ($subs as $subId => $sub)
    {
        if($sub['catId'] == $catId)
        {
        $pageContent .= "
        <li style='list-style-type: circle; padding-left: 15px; font-size: 70%;'><a href='$docPath/category/{$cat['catPath']}/{$sub['subPath']}/'>{$sub['subName']}</a></li>
        ";
        }
    }
    $pageContent .= "
    </ul>
    </div>
    ";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:48:46+00:00Added an answer on June 2, 2026 at 1:48 pm

    As you have built your array, you’d have to do your foreach loop like this:

    foreach ($cats as $catId => $cat)
    {
        $pageContent .= "<p>$catKey | ".$cat['catName']."</p>";
    }
    

    But why not just $cats[$rowCats['id'] = $rowCats['category']; in the first place?
    Then this would be enough:

    foreach ($cats as $catId => $catName)
    {
        $pageContent .= "<p>$catKey | $catName</p>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in a html form <select name=category class=input onchange=ShowTB(this,'suggest');> <option value=0
I have following code $criteria=new CDbCriteria; $criteria->with = array('owner'=>array('select'=>array('*','payment_info'=>'payment_account'))); which works fine, but when
I have following code function Model(onChanged) { this.array = new Array(); this.onChanged = onChanged;
i have following code in which, i am fetching the data from the sqlite
I have following code import sys from ctypes import * from ctypes.util import find_library
I have following code: VAR1= ANOTHER_VAR=$VAR1/path/to/file ANOTHER_VAR_2=$VAR1/path/to/another/file ... # getopts which reads params from
I have following code: $telnums = array(10, 20, 30); $obj = new StdClass(); $obj->telnums
i have following code for allocation two dimensional array #include <iostream> using namespace std;
I have following code: <div class='parent'> <div class='left-child'></div> <div class=right-child></div> </div> What I want
I have following code snippet and i am trying to evaluate the time take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.