Solved

PHP MYSQL ARRAYS PARENT CHILD

Posted on 2011-02-21
9
134 Views
Last Modified: 2013-11-30
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.




0
Comment
Question by:stuckintexas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34946007
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
 

Author Comment

by:stuckintexas
ID: 34946032
Hundreds or less
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34946409
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34946506
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
 

Author Comment

by:stuckintexas
ID: 34946650
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
 

Author Comment

by:stuckintexas
ID: 34946656
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34948040
"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
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686709
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question