Jump to content

While Loop


Go to solution Solved by arunpatal,

Recommended Posts

Hi, I am trying this task with the help of while loop but its not working....

Please check and correct me if this task can be done by while loop.....

 

I have 3 tables

 

1- category, 2- subcategory and 3- via $datatb variable.

 

In my index page am show categorys. I will pass category id and category table values by clicking on category link.

<a href='$_SERVER[PHP_SELF]?datatb=$row[cat_table]&cat_id=$row[cat_id]'>$row[cat_name]</a>

Now am trying to make a function which will display subcategory name and under it the list of topics.....

 

For example

 

subcat name 1

topic name 1

topic name 2

topic name 3

topic name 4

 

subcat name 2

topic name 1

topic name 2

topic name 3

My category table is like this

cat_id int auto_increment primary key,
cat_name varchar(255),
cat_table varchar(255)
My subcategory table is like this

sub_cat_id int auto_increment primary key,
sub_cat_name varchar(500),
category_id int(11)
My $datatb table is like this

id int auto_increment primary key,
    topic_title mediumtext,
    topic_detail mediumtext,
    added_date date,
    sub_id int(11)

This function am trying is this

function subcat_nav(){
    global $sub_category;
    
    if (isset ($_GET["datatb"])){
    $datatb = $_GET["datatb"];
    $cat_id = $_GET["cat_id"];
    
    $sql1 = mysql_query("SELECT * FROM $sub_category WHERE category_id = '$cat_id'")
    or die(mysql_error());
    
    while ($row1 = mysql_fetch_array($sql1)){
    
    $sql2 = mysql_query("SELECT * FROM $datatb WHERE sub_id = '12'")
    or die(mysql_error());
    
    while ($row2 = mysql_fetch_array($sql2)){
    
    echo "$row1[sub_cat_name] <br>";
    echo "$row2[topic_title] <br>";
    
    }}}}
Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/
Share on other sites

You need to organise the data into an array. If you are using mysql functions try the following:

<?php
$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM category c INNER JOIN subcategory s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
	$categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
	$categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row[['sub_cat_name']);
}
foreach($categories as $cat_id => $row): ?>
	<h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['subcats'] as $sub_cat_id => $sub_row): ?>
    	<li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php 
endforeach; 
?>
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1456866
Share on other sites

 

You need to organise the data into an array. If you are using mysql functions try the following:

<?php
$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM category c INNER JOIN subcategory s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
	$categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
	$categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row[['sub_cat_name']);
}
foreach($categories as $cat_id => $row): ?>
	<h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['subcats'] as $sub_cat_id => $sub_row): ?>
    	<li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php 
endforeach; 
?>

Thanks, i will try and will get back in 15mins

Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1456868
Share on other sites

I tried it......

 

The code is doing different then what i want......

 

I have 3 table

1- category, 2- subcategory and 3- via $datatb variable.

 

I am already showing category menu like this

<a href='$_SERVER[PHP_SELF]?datatb=$row[cat_table]&cat_id=$row[cat_id]'>$row[cat_name]</a>

Now am trying to make a function which will display subcategory name and under it the list of topics.....

 

For example

 

subcat name 1 (subcategory table)

topic name 1 ($datatb table)

topic name 2

topic name 3

topic name 4

 

subcat name 2 (subcategory table)

topic name 1 ($datatb table)

topic name 2

topic name 3

Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1456871
Share on other sites

<?php
$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM $category c INNER JOIN $sub_category s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
    $categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
}
foreach($categories as $cat_id => $row): ?>
    <h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['subcats'] as $sub_cat_id => $sub_row): ?>
        <li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php
endforeach;
?>

This code is not displaying category name..

 

Notice: Undefined index: name in C:\xampp\htdocs\demo\index.php on line 20

 

When i remove ['cat'] from the line under

$categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);

 

it shows category name but its not looping subcategory.......

It just show 1 subcategory under it.

Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1456875
Share on other sites

First, run the query in your database admin so you can see what data is coming out (is it correct). If the query needs extending to add your other table in then do it and test the query again. Modify the $categories array to hold the extra data if you need to.

Second, dump the array out so you can see that the data is all contained correctly.

Don't expect code to just work first time. Always test to see what the data looks like.

$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM category c INNER JOIN sub_category s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
    $categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
}

print "<pre>";
print_r($categories);
print "</pre>";
exit();
Edited by neil.johnson
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1457025
Share on other sites

Okay When i run the code below.....

Its showing category name as well

<?php
$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM $category c INNER JOIN $sub_category s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
    $categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
}

print "<pre>";
print_r($categories);
print "</pre>";
exit();
?>

But when i run the code below......

i get error

Notice: Undefined index: name in C:\xampp\htdocs\demo\index.php on line 21

 

This is line 21 <h4><?php echo $row['name']; ?></h4>

<?php
$categories = array();
$result = mysql_query("SELECT c.cat_id, c.cat_name, s.sub_cat_id, s.sub_cat_name FROM $category c INNER JOIN $sub_category s ON(c.cat_id=s.category_id) ORDER BY c.cat_name ASC, s.sub_cat_name ASC");
while($row = mysql_fetch_assoc($result))
{
    $categories[$row['cat_id']]['cat'] = array('name' => $row['cat_name']);
    $categories[$row['cat_id']]['subcats'][$row['sub_cat_id']] = array('name' => $row['sub_cat_name']);
}

foreach($categories as $cat_id => $row): ?>
    <h4><?php echo $row['name']; ?></h4>
    <ul>
    <?php foreach($row['subcats'] as $sub_cat_id => $sub_row): ?>
        <li><?php echo $sub_row['name']; ?></li>
    <?php endforeach; ?>
    </ul>
<?php
endforeach;
?>
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1457034
Share on other sites

  • Solution

OK....  GOT IT...

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

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

This line <?php echo $row['name']."<br>"; ?>

should be  <?php echo $row['subcat']['name']."<br>"; ?>

 

 

Thanks neil.johnson

Edited by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/283581-while-loop/#findComment-1457052
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.