Welcome Guest, Not a member yet? Register   Sign In
Total n00b PHP loop question
#1

[eluser]Einspruch[/eluser]
I hope the day comes soon enough when I can answer these types of questions instead of ask of them! :-)

I have data that looks like this in the database:

Code:
title A...datapoint 1
title A...datapoint 2
title A...datapoint 3
title A...datapoint 4
title B...datapoint 1
title B...datapoint 2
title B...datapoint 3
title B...datapoint 4

How can I output it so it looks like this:

Code:
<h2>title A</h2>
datapoint 1<br />
datapoint 2<br />
datapoint 3<br />
datapoint 4<br />
<h2>title B</h2>
datapoint 1<br />
datapoint 2<br />
datapoint 3<br />
datapoint 4<br />

Basically I need a loop inside of a loop. But I can't figure out how to code the logic that says if the title is the same, go to the next record, if not stop and go to the first record of the next title.

Help! :-)
#2

[eluser]demogar[/eluser]
I can't help you really much with so little information but you have to make something like this:

Code:
Controller:
$data['info'] = $this->info_model->getInfo();

Model
Make your query and joins for the datapoint or something similar.

View:
&lt;?php foreach ($info->result() as $title): ?&gt;
<h2>&lt;?php echo $title; ?&gt;</h2>

&lt;?php foreach $title->data->result() as $data();
  &lt;?php echo $data->title; ?&gt; <br />
&lt;?php endforeach; ?&gt;

&lt;?php endforeach; ?&gt;

If you have more info let us know.
#3

[eluser]jedd[/eluser]
[quote author="Einspruch" date="1259472404"]
How can I output it so it looks like this:
[/quote]

Much depends on how you got to that data.

Does [url="http://ellislab.com/forums/viewreply/622401/"]this thread[/url] help?
#4

[eluser]Einspruch[/eluser]
Thank you for your quick responses. Below are the model and controller.

Code:
function bfg_charts()
    {
    $this->db->select('Portal_Rankings.Date');
    $this->db->select('Portal_Rankings.Title');
    $this->db->select('Portal_Rankings.Rank');
    $this->db->select('DATEDIFF((Portal_Rankings.Date),("2009-11-20")) AS Days');
        $this->db->from('Portal_Rankings');
        $this->db->join('Sites', 'Portal_Rankings.SiteID = Sites.ID');
        $this->db->where('Portal_Rankings.SiteID', '1');
        $where = "(Portal_Rankings.Title like 'Samantha Swift and the Golden Touch%'
            OR Portal_Rankings.Title like 'Midnight Mysteries%')";
        $this->db->where($where);
        $this->db->order_by('Portal_Rankings.Title', 'ASC');
        $query = $this->db->get();
        return ($query->num_rows() > 0)? $query->result() : FALSE;
    }


Code:
function bfg_charts()
    {
        $this->load->model('rankings_model');
        $data['bfg_charts'] = $this->rankings_model->bfg_charts();
        $this->load->view('rankings/bfg_charts', $data);
    }

Hope this helps, and I really appreciate the assistance.
#5

[eluser]John_Betong[/eluser]
&nbsp;
Can you temporarily add these lines immediately after $query = $this->db->get();
Code:
if (TRUE}
{
   echo '<pre>';
     print_($query->result());
   echo '</pre>';
}

&nbsp;
then post the array values;
&nbsp;
&nbsp;
&nbsp;
#6

[eluser]jedd[/eluser]
[quote author="jedd" date="1259477043"]

Does [url="http://ellislab.com/forums/viewreply/622401/"]this thread[/url] help?[/quote]
#7

[eluser]Einspruch[/eluser]
Thanks! I am really close. I modified your code to the following:

Code:
&lt;?php
    
    $last_topic = NULL;

    echo "<categories>\n";
    foreach($bfg_charts_midnightmysteries as $row)  {
        // If parent matches the previous parent, don't show it - just indent a bit
        echo "<category label='" . $row->Days . "'/>\n";
    }
    echo "</categories>\n";
    
    foreach($bfg_charts as $row)  {
        if ($row->Title == $last_topic)
            echo "";
        else
            echo "<dataset seriesName='" . $row->Title . "'/>\n";

        // You're always showing the child information
        echo "<set unusedLabelDate='" . $row->Date . " unusedLabelDays='" . $row->Days . "' value=" . $row->Rank . "/>\n";
        
        // Set last topic to current topic (for test on next loop)
        $last_topic = $row->Title;
        }
?&gt;

This is 99% of what I need. The there is one last item. I need it to to close the "dataset" tag before it goes to the next title. In other words the current output (abbreviated):

Quote:<dataset seriesName='Midnight Mysteries: The Edgar Allan Poe Conspiracy'/>
<set unusedLabelDate='2009-07-19 unusedLabelDays='1' value=5/>
<set unusedLabelDate='2009-07-20 unusedLabelDays='2' value=5/>
....
<set unusedLabelDate='2009-11-29 unusedLabelDays='35' value=50/>
<dataset seriesName='Samantha Swift and the Golden Touch'/>
<set unusedLabelDate='2009-05-01 unusedLabelDays='1' value=6/>
<set unusedLabelDate='2009-05-02 unusedLabelDays='2' value=5/>

should look like this:

Quote:<dataset seriesName='Midnight Mysteries: The Edgar Allan Poe Conspiracy'/>
<set unusedLabelDate='2009-07-19 unusedLabelDays='1' value=5/>
<set unusedLabelDate='2009-07-20 unusedLabelDays='2' value=5/>
....
<set unusedLabelDate='2009-11-29 unusedLabelDays='35' value=50/>
</dataset>
<dataset seriesName='Samantha Swift and the Golden Touch'/>
<set unusedLabelDate='2009-05-01 unusedLabelDays='1' value=6/>
<set unusedLabelDate='2009-05-02 unusedLabelDays='2' value=5/>
</dataset>
#8

[eluser]jedd[/eluser]
Can't you just change this line:

Code:
echo "<dataset seriesName='" . $row->Title . "'/>\n";

To this:

Code:
echo "</dataset>\n<dataset seriesName='" . $row->Title . "'/>\n";
#9

[eluser]Einspruch[/eluser]
Close but not quite! That puts a closing tag at the very beginning and does not close the last set of results.
#10

[eluser]jedd[/eluser]
We're definitely in the realm of standard logic patterns now ...
Code:
&lt;?php
    
    $last_topic = NULL;

    $dataset = 0;

    echo "<categories>\n";
    foreach($bfg_charts_midnightmysteries as $row)  {
        // If parent matches the previous parent, don't show it - just indent a bit
        echo "<category label='" . $row->Days . "'/>\n";
    }
    echo "</categories>\n";
    
    foreach($bfg_charts as $row)  {
        if ($row->Title == $last_topic)
            echo "";
        else  {
            if ($dataset)
                echo "</dataset>\n";
            echo "<dataset seriesName='" . $row->Title . "'/>\n";
            }
        // You're always showing the child information
        echo "<set unusedLabelDate='" . $row->Date . " unusedLabelDays='" . $row->Days . "' value=" . $row->Rank . "/>\n";
        
        // Set last topic to current topic (for test on next loop)
        $last_topic = $row->Title;

        $dataset++;
        }
    echo "</dataset>\n";
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB