Solved

PHP MYSQL ARRAYS PARENT CHILD

Posted on 2011-02-21
9
126 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
  • 4
  • 3
9 Comments
 
LVL 109

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 109

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 109

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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

820 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