PHP MYSQL ARRAYS PARENT CHILD

I have 2 data sets coming from MYSQL results:
ds1. Each row is a day of a given month: 2011-02/21
ds2. Each row is a report generated on a day within the given month.. each report has a designation: RR, CC, AG etc.

What I'm trying to do is generate an <li> list of each day in the given month, put the reports found in a title tag (RR, CC, AG etc) and a condition:
condition 1:  foreach day in ds1, if the matching date in ds2 contains these reports (RR, CC) then display this...
condition 2:  else if the matching date in ds2 contains these reports (AG) then display this...
condition 3:  else display...

DS1 and DS2 are not the same length obviously and DS 2 may not include all the dates in DS1... but I need to display all the dates in DS1.. heres a sample of the data:

DS1: $row['ddate']
2010-02-01
2010-02-02
2010-02-03
2010-02-04
2010-02-05
etc..

DS2: $row['fdate'], $row['freport']
2010-02-01, cc
2010-02-01, rr
2010-02-01, ag
2010-02-02, rr
2010-02-02, cc
2010-02-05, rr
2010-02-05, ag

Hope that helps, thanks in advance.




stuckintexasAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
"like a very slow and costly solution" -- maybe not; in practice I do this sort of thing all the time.  You might want to get some background in computer performance and evaluation and capacity planning.  You can use this little class to time all or part of your scripts.

As far as switch/case syntax goes -- it is just one way of looking at logic.  You can choose from many others, too.
<?php // RAY_oop_stopwatch.php
error_reporting(E_ALL);


// DEMONSTRATE A SCRIPT TIMER FOR ALL OR PART OF A SCRIPT PHP 5+
// MAN PAGE http://us.php.net/manual/en/function.microtime.php


class StopWatch
{
    protected $a, $z;
    public function __construct()
    {
        $this->a = array();
        $this->z = array();
    }

    // A METHOD TO CAPTURE A START TIME
    public function start($name='TIMER')
    {
        $this->a[$name] = microtime(TRUE);
    }

    // A METHOD TO CAPTURE AN END TIME
    public function stop($name='ALL')
    {
        if ($name == 'ALL')
        {
            foreach ($this->a as $name => $start_time)
            {
                if (!isset($this->z[$name])) $this->z[$name] = microtime(TRUE);
            }
        }
        else
        {
            $this->z[$name] = microtime(TRUE);
        }
    }

    // A METHOD TO READ OUT THE TIMER(S)
    public function readout($m=1000, $eol=PHP_EOL)
    {
        $str = NULL;
        foreach ($this->a as $name => $start_time)
        {
            $str .= $name;
            if (!isset($this->z[$name]))
            {
                $str .= " IS STILL RUNNING";
            }
            else
            {
                $lapse_time = $this->z[$name] - $start_time;
                $lapse_msec = $lapse_time * $m;
                $lapse_echo = number_format($lapse_msec, 1);
                $str .= " $lapse_echo";
            }
            $str .= $eol;
        }
        return $str;
    }
}


// DEMONSTRATE THE USE -- INSTANTIATE THE STOPWATCH OBJECT
$sw  = new Stopwatch;

// SET STOPWATCH NAMES
$go = 'GOOGLE ONLY';
$gy = 'GOOGLE AND YAHOO!';
$yo = 'YAHOO! ONLY';

// START SOME TIMERS
$sw->start($go);
$sw->start($gy);

// PERFORM SOME ACTIVITY THAT YOU WANT TO TIME
$page = 'http://google.com';
$html = file_get_contents($page);

// STOP ONE OF THE STOPWATCHES AND START THE OTHER
$sw->stop($go);
$sw->start($yo);

// PERFORM SOME OTHER ACTIVITY THAT YOU WANT TO TIME
$page = 'http://yahoo.com';
$html = file_get_contents($page);

// REPORT THE STOPWATCHES CONTENT (TWO WILL BE INCOMPLETE)
echo nl2br($sw->readout());

// STOP ALL OF THE REMAINING STOPWATCHES
$sw->stop();

// REPORT THE STOPWATCHES CONTENT AGAIN
echo nl2br($sw->readout());

Open in new window

0
 
Ray PaseurCommented:
In total, how many rows of data are we talking about?  Hundreds or hundreds of thousands?

If the former, you might nest the queries.
0
 
stuckintexasAuthor Commented:
Hundreds or less
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Ray PaseurCommented:
That's good news.

I would try something like this "pidgin code" example.  When var_dump() prints out what you want, you know you're getting the correct data.  Then you can generate the HTML.
echo "<pre>"; // MAKE var_dump() EASY TO READ
$ds1_sql = "SELECT ddate FROM ds1 ORDER BY ddate ASC";
$ds1_res = mysql_query($ds1_sql) or die( mysql_error() );
while ($ds1_row = mysql_fetch_assoc($ds1_res))
{
    $ds1_date = $ds1_row["ddate"];
    $ds2_sql  = "SELECT fdate, freport FROM ds2 WHERE fdate = '$ds1_date'";
    $ds2_res  = mysql_query($ds2_sql) or die( mysql_error() );
    while ($ds2_row = mysql_fetch_assoc($ds2_res))
    {
        var_dump($ds2_row);
    }
}

Open in new window

0
 
Ray PaseurCommented:
As far as deciding what to do with cc, rr or ag, you might find the switch / case structure to be an easy answer.  Working example in the code snippet.  Available online here:
http://www.laprbass.com/RAY_switch_example.php
<?php // RAY_switch_example.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE THE USE OF SWITCH / CASE CONDITIONAL LOGIC
// MAN PAGE HERE: http://us.php.net/manual/en/control-structures.switch.php
// NOTE THIS: "The switch statement is similar to a series of IF statements ON THE SAME EXPRESSION."


// IF ANYTHING POSTED
if (!empty($_POST["i"]))
{
    // GET THE POSTED DATA IN A LOCAL VAR
    $i = trim(strtopuper$_POST["i"]));

    // SWITCH ON THE BASIS OF THE CONTENTS OF OUR LOCAL VAR
    switch ($i)
    {
        case "A" :
        case "B" : echo "YOU ENTERED EITHER 'A' OR 'B' ";  
                   break;
        case "C" : echo "YOU ENTERED 'C' ";  
                   break;
        default  : echo "YOU ENTERED NONE OF A, B OR C ";
    }
}

// END PHP, PUT UP THE FORM
?>
<form method="post">
ENTER 'A' 'B' OR 'C'
<input name="i" />
<input type="submit" />
</form>

Open in new window

0
 
stuckintexasAuthor Commented:
Ray,
Thanks for the input.. but that seems like a very slow and costly solution vs getting the data sets and manipulating parent and child arrays surely isn't it?
0
 
stuckintexasAuthor Commented:
Also, case wouldn't work I believe because were not looking for a a list of all no existent reports or all complete reports but all dates and the reports the may or may not have in the same list.
0
 
CWS (haripriya)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
All Courses

From novice to tech pro — start learning today.