Solved

PHP MYSQL ARRAYS PARENT CHILD

Posted on 2011-02-21
9
96 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 108

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 108

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
 
LVL 108

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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 dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now