Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$var = "Date" vs. $var = $_POST['Date']

Status
Not open for further replies.

BiJae

Programmer
Oct 1, 2002
154
US
This post is a new problem relating to my previous post:
As that post concluded I got everything working and displaying the chart that I wanted! So now it is time to integrate it to the application...

The code that was working used two variables, $begin and $end and they were declared as such:

Code:
$begin = "2007-12-01";
$end = "2007-12-12";

As such the sql returned the data set and the chart program produced a wonderful chart as expected. To make this program dynamic I changed the code to be:
Code:
$begin = $_POST['alpha'];
$end = $_POST['omega'];
The SWF/PHP chart no longer produces dataset results but puts up the default chart.

Alpha and Omega are a simple test select list I created:
Code:
echo "<html><head><title>Test A File</title></head><body>
<form name='mine' action='' method='POST'>
<select name='alpha'>
<option value='2007-12-01'>2007-12-01</option>/n
<option value='2007-12-02'>2007-12-02</option>/n
<option value='2007-12-03'>2007-12-03</option>/n
<option value='2007-12-04'>2007-12-04</option>/n
<option value='2007-12-05'>2007-12-05</option>/n
<option value='2007-12-06'>2007-12-06</option>/n
<option value='2007-12-07'>2007-12-07</option>/n
<option value='2007-12-08'>2007-12-08</option>/n
<option value='2007-12-09'>2007-12-09</option>/n
</select>
<select name='omega'>
<option value='2007-12-01'>2007-12-01</option>/n
<option value='2007-12-02'>2007-12-02</option>/n
<option value='2007-12-03'>2007-12-03</option>/n
<option value='2007-12-04'>2007-12-04</option>/n
<option value='2007-12-05'>2007-12-05</option>/n
<option value='2007-12-06'>2007-12-06</option>/n
<option value='2007-12-07'>2007-12-07</option>/n
<option value='2007-12-08'>2007-12-08</option>/n
<option value='2007-12-09'>2007-12-09</option>/n
</select>
<input type='submit' name='submit' value='submit'>";

I changed the variables back and it worked. To further test I just declared four sets of variables:
Code:
$begin = "2007-12-01";
$end = "2007-12-12";
$dyn_begins = $_POST['alpha'];
$dyn_ends = $_POST['omega'];
I declared the four variables but continued to use $begin and $end in my function to get the data. Oddly enough simply declaring the $dyn variables causes the default chart to display. If I comment both the declarations out it works fine. Even if I'm not using the variables declaring them in the same file causes the SWF/PHP chart program to break.

I've been playing around with ob_clean functions to try to get rid of any extraneous noise that I might be causing, however I'm not able to clear it up.
Does any one know of why these two variables might be creating noise to break the files? Moreover does any one have an idea why $_POST['alpha'] != $begin when I have selected '2007-12-01' for both?

Thank you for taking the time to read this and any help you can offer.


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
you need to post more of your code really. there is no reason for the behaviour you describe from the facts that you give. so it's likely to be a coding logic issue.
 
No problem, jpadie. I understand your concern.

I had two files that I got working with the SW chart program. The first includes two lines of code:
Code:
include('charts.php');

echo InsertChart("charts.swf", "charts_library", "test2.php", 900 ,500 );
The second contained all the data calls and the $chart building:
Code:
<?php
include('DBManagerDS.php');

$begin = "2007-12-01";
$end = "2007-12-10";

$graphdata = get_evtdisp($begin, $end);

$graphDate = array("");
$Event = array("Events");
$Disp = array("Dispatches");

$count = count($graphdata);
foreach($graphdata as $graphdata) {
	$strdate = $graphdata->strdate;
	$evtcnt = $graphdata->evtcnt;
	$dspcnt = $graphdata->dspcnt;
	$graphDate[] = substr($strdate,5);
    $Event[] = $evtcnt;
    $Disp[] = $dspcnt;
	}
//now turn it into a chart data

require("charts.php");
//ob_end_clean();

$chartB['chart_type'] = 'stacked column';
$chartB['chart_data'] = array( $graphDate, $Event, $Disp );
$skip = 0;
if($count >= 20 && $count < 40) $skip = 2;
if($count >= 40 && $count  < 60) $skip = 4; 
if($count >= 60 && $count < 100) $skip = 8;
if($count >= 100 && $count < 150) $skip = 12; 
if($count >=150 && $count < 200) $skip = 15;
if($count >=200 ) $skip = 30;
$chartB['axis_category'] = array( 'skip' => $skip, 'orientation' => 'diagonal_up');
SendChartData ( $chartB);


function get_evtdisp($begin, $end) {
	$begin = $begin . " 00:00:00";
	$end = $end . " 23:59:59";
	$dbmgr = new DBManager();
	$sql = "select
  strdate = a.strdate
, evtcnt = sum(a.evtcnt)
, dspcnt = sum(a.dspcnt)
from (
	select
	  strdate = convert(varchar(10), Date_Time, 120)
	, evtcnt = count(*)
	, dspcnt = 0
	from Event_Log
	where Date_Time between '$begin' and '$end'
	group by convert(varchar(10), Date_Time, 120)
	union all
	select
	  strdate = initdisp.strdate
	, evtcnt = 0
	, dspcnt = count(initdisp.dspcnt)
	from (
select strdate = convert(varchar(10), dsp.Time_Sent, 120)
, dspcnt = count(*)
, dsp.EventLogID
from
Event_Log evt
, Dispatch dsp
where
dsp.Time_Sent between '$begin' and '$end'
and evt.EventLogID = dsp.EventLogID
and convert(varchar(10), evt.Date_Time, 120)
= convert(varchar(10), dsp.Time_Sent, 120)
group by convert(varchar(10), dsp.Time_Sent, 120)
, dsp.EventLogID
) initdisp group by initdisp.strdate
) a
group by a.strdate
order by a.strdate asc";

$result = $dbmgr->execQuery($sql);
$num_rows = $dbmgr->numrows();
$evtdisarray = array();
if(!$num_rows) return $evtdisarray;
for($i = 0; $i < $num_rows; $i++ ) {
	$r = $dbmgr->next($i); 
	$get_num = new disp();
	$get_num->strdate = $r['strdate'];
	$get_num->evtcnt = $r['evtcnt'];
	$get_num->dspcnt = $r['dspcnt'];
	$evtdisarray[$i] = $get_num;
	}
	return $evtdisarray;
	}
	
	class disp {
		var $strdate;
		var $evtcnt;
		var $dspcnt;
		function get_evtdisp() {
			}
		}
?>

To put this all in a user driven form I put the following file in front of the other two:
Code:
global $begin, $end;
$begins = $_POST['alpha'];
$ends = $_POST['omega'];

echo "<html><head><title>Test A File</title></head><body>
		<form name='mine' action='' method='POST'>
		<select name='alpha'>
			<option value='2007-12-01'>2007-12-01</option>/n
			<option value='2007-12-02'>2007-12-02</option>/n
			<option value='2007-12-03'>2007-12-03</option>/n
			<option value='2007-12-04'>2007-12-04</option>/n
			<option value='2007-12-05'>2007-12-05</option>/n
			<option value='2007-12-06'>2007-12-06</option>/n
			<option value='2007-12-07'>2007-12-07</option>/n
			<option value='2007-12-08'>2007-12-08</option>/n
			<option value='2007-12-09'>2007-12-09</option>/n
		</select>
		<select name='omega'>
			<option value='2007-12-01'>2007-12-01</option>/n
			<option value='2007-12-02'>2007-12-02</option>/n
			<option value='2007-12-03'>2007-12-03</option>/n
			<option value='2007-12-04'>2007-12-04</option>/n
			<option value='2007-12-05'>2007-12-05</option>/n
			<option value='2007-12-06'>2007-12-06</option>/n
			<option value='2007-12-07'>2007-12-07</option>/n
			<option value='2007-12-08'>2007-12-08</option>/n
			<option value='2007-12-09'>2007-12-09</option>/n
		</select>
		<input type='submit' name='submit' value='submit'>";
include('test_data.php');

There are the three files in this test application The form; supertest.php, the two lines calling the graph; test_data.php, and the workhorse of the application; test2.php.

The user selects the begin and end date from two drop down lists and clicks submit.


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
ok

let's rework the form to look like this

Code:
<?php
//check to see whether form has been submitted
if (isset($_POST['submit'])){
	global $begin, $end;
	$begins = $_POST['alpha'];
	$ends = $_POST['omega'];
	displayForm();
	displayChart();
} else {
	displayForm();
}

function displayChart(){
	include('charts.php');
	echo InsertChart("charts.swf", "charts_library", "test2.php", 900 ,500 );
}


function displayForm(){
	echo <<<HTML
<html>
	<head>
		<title>Test A File</title>
	</head>
	<body>
        <form name='mine' action="{$_SERVER['PHP_SELF']}" method='POST'>
        <select name='alpha'>
            <option value='2007-12-01'>2007-12-01</option>/n
            <option value='2007-12-02'>2007-12-02</option>/n
            <option value='2007-12-03'>2007-12-03</option>/n
            <option value='2007-12-04'>2007-12-04</option>/n
            <option value='2007-12-05'>2007-12-05</option>/n
            <option value='2007-12-06'>2007-12-06</option>/n
            <option value='2007-12-07'>2007-12-07</option>/n
            <option value='2007-12-08'>2007-12-08</option>/n
            <option value='2007-12-09'>2007-12-09</option>/n
        </select>
        <select name='omega'>
            <option value='2007-12-01'>2007-12-01</option>/n
            <option value='2007-12-02'>2007-12-02</option>/n
            <option value='2007-12-03'>2007-12-03</option>/n
            <option value='2007-12-04'>2007-12-04</option>/n
            <option value='2007-12-05'>2007-12-05</option>/n
            <option value='2007-12-06'>2007-12-06</option>/n
            <option value='2007-12-07'>2007-12-07</option>/n
            <option value='2007-12-08'>2007-12-08</option>/n
            <option value='2007-12-09'>2007-12-09</option>/n
        </select>
        <input type='submit' name='submit' value='submit'>
HTML;
} //end of displayForm function
?>

and now the chart data script

Code:
<?php
include('DBManagerDS.php');

$begin = empty($_POST['alpha'] ? "2007-12-01": trim ($_POST['alpha']);
$end = $begin = empty($_POST['omega'] ? "2007-12-10": trim ($_POST['omega']);


$graphdata = get_evtdisp($begin, $end);
$graphDate = array('');
$Event = array("Events");
$Disp = array("Dispatches");

foreach($graphdata as $_graphdata) {		//you MUST use different variable names for the foreach ... as ... 
    $strdate = $_graphdata->strdate;
    $evtcnt = $_graphdata->evtcnt;
    $dspcnt = $_graphdata->dspcnt;
    $graphDate[] = substr($strdate,5);
    $Event[] = $evtcnt;
    $Disp[] = $dspcnt;
}
//now turn it into a chart data

require("charts.php");
//ob_end_clean();

//i don't get this $count thing it will always be 10 or less?
$count = count ($graphdata)
$chartB['chart_type'] = 'stacked column';
$chartB['chart_data'] = array( $graphDate, $Event, $Disp );
$skip = 0;
if($count >= 20 && $count < 40) $skip = 2;
if($count >= 40 && $count  < 60) $skip = 4; 
if($count >= 60 && $count < 100) $skip = 8;
if($count >= 100 && $count < 150) $skip = 12; 
if($count >=150 && $count < 200) $skip = 15;
if($count >=200 ) $skip = 30;
$chartB['axis_category'] = array( 'skip' => $skip, 'orientation' => 'diagonal_up');
SendChartData ( $chartB);


function get_evtdisp($begin, $end) {
    $begin = $begin . " 00:00:00";
    $end = $end . " 23:59:59";
    $dbmgr = new DBManager();
    $sql = "select
  strdate = a.strdate
, evtcnt = sum(a.evtcnt)
, dspcnt = sum(a.dspcnt)
from (
    select
      strdate = convert(varchar(10), Date_Time, 120)
    , evtcnt = count(*)
    , dspcnt = 0
    from Event_Log
    where Date_Time between '$begin' and '$end'
    group by convert(varchar(10), Date_Time, 120)
    union all
    select
      strdate = initdisp.strdate
    , evtcnt = 0
    , dspcnt = count(initdisp.dspcnt)
    from (
select strdate = convert(varchar(10), dsp.Time_Sent, 120)
, dspcnt = count(*)
, dsp.EventLogID
from
Event_Log evt
, Dispatch dsp
where
dsp.Time_Sent between '$begin' and '$end'
and evt.EventLogID = dsp.EventLogID
and convert(varchar(10), evt.Date_Time, 120)
= convert(varchar(10), dsp.Time_Sent, 120)
group by convert(varchar(10), dsp.Time_Sent, 120)
, dsp.EventLogID
) initdisp group by initdisp.strdate
) a
group by a.strdate
order by a.strdate asc";

	$result = $dbmgr->execQuery($sql);
	$num_rows = $dbmgr->numrows();
	$evtdisarray = array();
	
	if(!$num_rows) return $evtdisarray;
	
	for($i = 0; $i < $num_rows; $i++ ) {
		$r = $dbmgr->next($i); 
		$get_num = new disp();
		$get_num->strdate = $r['strdate'];
		$get_num->evtcnt = $r['evtcnt'];
		$get_num->dspcnt = $r['dspcnt'];
		$evtdisarray[$i] = $get_num;
    } //end of for loop
    return $evtdisarray;
} //end of function
?>
 
Thank you, that is very Slick... but it still does not produce results.

Initial load gives me the two drop down submit form..

after post I get the default chart.


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
Found that it was posting an error...

Parse error: syntax error, unexpected '?', expecting ')' in C:\Program Files\Apache Group\Apache2\htdocs\charts\test2.php on line 4



"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
I found a couple of syntax things and fixed them, For any one using this in the future, update line four and line five of the second file to :

Code:
$begin = empty($_POST['alpha']) ? "2007-12-01": trim ($_POST['alpha']);
$end = $begin = empty($_POST['omega']) ? "2007-12-10": trim ($_POST['omega']);
and add the class to the second file at the bottom of the file
Code:
    class disp {
        var $strdate;
        var $evtcnt;
        var $dspcnt;
        function get_evtdisp() {
            }
        }

We're making good progress jpadie!

I'm not sure what is meant by your comment:

//you MUST use different variable names for the foreach ... as ...

I've been using that type of foreach loop in my code for years and it's been working fine. I learned it from some one a long time ago and it may not be the proper way to code but it's been affective so I've continued to use it. However, I'll slap my knuckles with a ruler and say 'bad programmer' and I'll train myself not to use it any longer.. ;-)

The code you gave appears to be working with the exception of it's only giving me the last record in the record-set. When I look at the array without sending it to the chart program I'm getting only one record returned...


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
//i don't get this $count thing it will always be 10 or less?
I added these lines of code because of the way that the users will be generating charts. Some users choose to pull a years worth of information at a time. So the count($graphdata) could be upwards of 365. Without adding a control to the skip the dates write a thick black line on the bottom. This $count allows me to modify the skip=> in the $chart['chart_axis'] array and space the number of dates displayed on the X axis.


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
I've been playing with the code you gave me and have found that
Code:
$begin = empty($_POST['alpha']) ? "2007-12-01": trim ($_POST['alpha']);
$end = empty($_POST['omega']) ? "2007-12-10": trim ($_POST['omega']);
is giving me some thing interesting.

If I run the charting program I get the date range 12-01 - 12-12 every time, no matter what I select in the drop down list. But if I post out the SQL statement in an echo I get the alpha and omega values in my sql.

select strdate = a.strdate , evtcnt = sum(a.evtcnt) , dspcnt = sum(a.dspcnt) from ( select strdate = convert(varchar(10), Date_Time, 120) , evtcnt = count(*) , dspcnt = 0 from Event_Log where Date_Time between '2007-12-02 00:00:00' and '2007-12-05 23:59:59' group by convert(varchar(10), Date_Time, 120) union all select strdate = initdisp.strdate , evtcnt = 0 , dspcnt = count(initdisp.dspcnt) from ( select strdate = convert(varchar(10), dsp.Time_Sent, 120) , dspcnt = count(*) , dsp.EventLogID from Event_Log evt , Dispatch dsp where dsp.Time_Sent between '2007-12-02 00:00:00' and '2007-12-05 23:59:59' and evt.EventLogID = dsp.EventLogID and convert(varchar(10), evt.Date_Time, 120) = convert(varchar(10), dsp.Time_Sent, 120) group by convert(varchar(10), dsp.Time_Sent, 120) , dsp.EventLogID ) initdisp group by initdisp.strdate ) a group by a.strdate order by a.strdate asc


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
Code:
foreach ($graphdata as $graphdata){

}

will not work as you are changing the value of $graphdata at each iteration.

you would normally do something like

Code:
foreach ($rows as $row){

}

to fix the code change the relevant lines as follows

Code:
$begin = empty($_POST['alpha']) ? "2007-12-01": trim ($_POST['alpha']);
$end = empty($_POST['omega'] ? "2007-12-10": trim ($_POST['omega']);

 
Thank you, jpadie!

If I'm understanding this code:
Code:
$begin = empty($_POST['alpha']) ? "2007-12-01": trim ($_POST['alpha']);
$end = empty($_POST['omega'] ? "2007-12-10": trim ($_POST['omega']);
We're setting it to be on of two values, either the $_POST value : (or) the manually coded value.

So when I bypass the charting program and just echo out the sql I'm getting the $_POST value. When I run it through the charting program I'm getting the manually coded value. My results reflect this every time I run it.

How is it this is coming to be?


"If the only prayer you said in
your whole life was, 'thank you,'
that would suffice."
-- Meister Eckhart
 
there's a missing ')' after the empty($_POST['omega'] above. my bad.

remember that the POST values are coming from the form. i'm not sure how you would be debugging to test the various behaviours.

if you fix the bracket do things now work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top