Jump to content

looping through tables


woodplease

Recommended Posts

You don't want to be looking at the example from that other thread. That was just to demonstrate a simple join and how you categorise your data. Which is basically what you're needing to do.

 

So forgetting that other example. You should look at the code I posted earlier which is this

<?php
$query = 'SELECT m.section_id,
       m.section_title,
       s.section_sub_title,
       s.section_sub_desc
FROM section_main AS m
LEFT JOIN section_sub as s ON s.section_title = m.section_title
ORDER BY m.section_id, s.section_sub_title ASC';
$result = mysql_query($query);

$data = array();
while($row = mysql_fetch_assoc($result))
    $data[$row['section_title']][] = $row;
?>

<dl>
<?php foreach($data as $section_title => $section): ?>
    <dt><h1><?php echo $section_title; ?></h1></dt>
    <?php foreach($section as $sub_section): ?>
        <dd><?php echo $sub_section['section_sub_title']; ?></dd>
    <?php endforeach; ?>
<?php endforeach; ?>
</dl>

 

If you ran that code you'll get something similar to this

Film
   Avatar
   Speed
   Starteck

TV
   Eastenders
   Xfactor

The output above is from my test data. Your result will differ.

 

How it is displaying like that is to do with the PHP code.

 

First I'm adding all the results into the data array

$data = array();
while($row = mysql_fetch_assoc($result))
    $data[$row['section_title']][] = $row;

 

I assigned the section_title as the key to the $data array. With print_r you'll see how this array is formatted. This is my result.

Array
(
    [film] => Array
        (
            [0] => Array
                (
                    [section_id] => 1
                    [section_title] => film
                    [section_sub_title] => Avatar
                    [section_sub_desc] => tall blue people
                )

            [1] => Array
                (
                    [section_id] => 1
                    [section_title] => film
                    [section_sub_title] => Speed
                    [section_sub_desc] => Run away bus
                )

            [2] => Array
                (
                    [section_id] => 1
                    [section_title] => film
                    [section_sub_title] => starteck
                    [section_sub_desc] => space odyssey 
                )

        )

    [tv] => Array
        (
            [0] => Array
                (
                    [section_id] => 2
                    [section_title] => tv
                    [section_sub_title] => eastenders
                    [section_sub_desc] => crap soap
                )

            [1] => Array
                (
                    [section_id] => 2
                    [section_title] => tv
                    [section_sub_title] => Xfacter
                    [section_sub_desc] => for people that cant sing
                )

        )

)

 

A nice easy to use multi-dimensionally. Now we can easily loop through this array using a couple of loops. All the hard work was done by MySQL.

<dl>
<?php foreach($data as $section_title => $section): ?>
    <dt><h1><?php echo $section_title; ?></h1></dt>
    <?php foreach($section as $sub_section): ?>
        <dd><?php echo $sub_section['section_sub_title']; ?></dd>
    <?php endforeach; ?>
<?php endforeach; ?>
</dl>

 

You can change this to what ever you wish. Just change the html to your liking.

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.