Jump to content

Can not loop....


arunpatal
Go to solution Solved by arunpatal,

Recommended Posts

Hi,

 

The code is not able to loop topic_title on 2nd foreach loop.....

Its looping the subcategory list but not topic_title.

It shows only one topic_title under each subcategory

 

Please help

<?php
if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){
    $datatb = $_GET["datatb"];
    $cat_id = $_GET["cat_id"];
    
$categories = array();
$result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
}
foreach($categories as $sub_cat_id => $row): ?>
    <h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['topics'] as $sub_cat_id => $sub_row): ?>
        <li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php
endforeach;
}
?>
Edited by arunpatal
Link to comment
Share on other sites

You're overwriting the variable "$row" here:

 

foreach($categories as $sub_cat_id => $row): ?>

 

 

ok..  i changed it but still showing only 1 topic under subcategory list

<?php
if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){
    $datatb = $_GET["datatb"];
    $cat_id = $_GET["cat_id"];
    
$categories = array();
$result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
}
foreach($categories as $sub_cat_id => $row): ?>
    <h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['topics'] as $sub_id => $sub_row): ?>
        <li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php
endforeach;
}
?>
Link to comment
Share on other sites

Um, you didn't change it.  Not in the post above, anyway.  It still says:

 

foreach($categories as $sub_cat_id => $row): 

 

Of course, you may have a more insidious issue.  Your while() is overwriting $row, too.

Something like:

$x = 0;
while($row = mysql_fetch_assoc($result))
{
    $categories[$x][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    $categories[$x][$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
    $x++;
}


 

Link to comment
Share on other sites

You're overwriting the variable "$row" here:

 

foreach($categories as $sub_cat_id => $row): ?>

 

Overwriting $row shouldn't be the issue. The foreach loop isn't being executed in the while loop.

 

Is the following line supposed to maintain all the topic titles?

$categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);

If so, the issue is probably caused by the following line:

$categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);

Every time $row['sub_cat_id'] is the same, it overwrites the old value with the new array.

Link to comment
Share on other sites

This is how my tables look like

 

subcategory

sub_cat_id int auto_increment primary key,
sub_cat_name varchar(500),
category_id int(11)

 

$datatb

id int auto_increment primary key,
    topic_title mediumtext,
    topic_detail mediumtext,
    added_date date,
    sub_id int(11)  => (sub_cat_id from subcategory table)

Link to comment
Share on other sites

Hard to understand where to add this

print '<pre>' . print_r($categories, true) . '</pre>';

Here is the code i am using....

<?php
if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){
    $datatb = $_GET["datatb"];
    $cat_id = $_GET["cat_id"];
    
$categories = array();
$result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
}
foreach($categories as $sub_cat_id => $row): ?>
    
    <?php echo "<green>".$row['name']."</green> <br>"; ?>

    <?php foreach($row['topics'] as $sub_id => $sub_row):
         
         echo $sub_row['name']."<br><br>";
    
    endforeach;
     ?>

<?php
endforeach;
}
?>

Can you kindly edit it....

Link to comment
Share on other sites

As I mentioned in Reply 9, it looks like you are overwriting the array. To avoid the new array from wiping out what's already in place, you could try something like this:

while($row = mysql_fetch_assoc($result))
{
    if(!isset($categories[$row['sub_cat_id']])) {
        $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    }
    $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
}
Link to comment
Share on other sites

cyberRobot

 

Hi, I tried as you said....

But still displaying only one topic title.......

<?php
if (isset($_GET["datatb"]) AND isset($_GET["cat_id"])){
    $datatb = $_GET["datatb"];
    $cat_id = $_GET["cat_id"];
    
$categories = array();
$result = mysql_query("SELECT s.sub_cat_id, s.sub_cat_name, t.sub_id, t.topic_title FROM $sub_category s INNER JOIN $datatb t ON(s.sub_cat_id=t.sub_id)");
while($row = mysql_fetch_assoc($result))
{
    if(!isset($categories[$row['sub_cat_id']])) {
        $categories[$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
    }
    $categories[$row['sub_cat_id']]['topics'][$row['sub_id']] = array('name' => $row['topic_title']);
}

foreach($categories as $sub_cat_id => $row): ?>
    
    <?php echo "<green>".$row['name']."</green> <br>"; ?>

    <?php foreach($row['topics'] as $sub_id => $sub_row): ?>
         
       <?php  echo $sub_row['name']."<br><br>"; ?>
    
    <?php endforeach;
     ?>

<?php
endforeach;
}
?>

 

Link to comment
Share on other sites

  • Solution

Thanks cyberRobot and all others who helped me via this...

 

now its working

while($row = mysql_fetch_assoc($result))
{
    $categories[$row['sub_cat_id']]['subcat'] = array('name' => $row['sub_cat_name']);
    $categories[$row['sub_cat_id']]['topics'][$row['id']] = array('name' => $row['topic_title']);
}
foreach($categories as $sub_cat_id => $row): ?>
    
           <?php echo $row['subcat']['name']."<br>"; ?>

            <?php foreach($row['topics'] as $sub_id => $sub_row): ?>
         
            <?php  echo $sub_row['name']."<br>"; ?>
    
            <?php endforeach; ?>
          
    <?php endforeach; } ?>

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.