Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2004, 01:02 PM
Authorized User
 
Join Date: Jan 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default problems with related query

i want to display a chart that will display by month if the user choose the year...

this is my snippet of code:
<?php
include("../inc/config.php");
include("phpchartdir.php");
session_start();
//===============FUNCTION AREA===============================
function query($query, $link, $line, $file)
{
$result = mysql_query($query);
if (empty($result) && ini_get('display_errors') == 1)
{
echo mysql_error().": ";
echo mysql_errno()."<br />\n";
echo $query."<br />\n";
echo "@ $line in $file\n";
}
return $result;
}
$tahun=$_POST["year"];
?>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Zend Studio">
</HEAD>
<BODY>
<form name="myform" action="failureRate1.php" method="post">
<table width="100%" border="1" bgcolor="#666666" bordercolor="#333333" align="center">
<tr>
<td align="center">
<b>
KADAR KEROSAKAN MESIN BERDASARKAN BULAN
</b>
</td>
</tr>
</table>
<br>
<?php
//****************PANGGIL SEMUA PEKERJA***********

$viewall=query("Select noTag,waktuTamat from repairbreakdown WHERE noTag = '{$_POST["noTag"]}' AND DATE_FORMAT(waktuTamat,'%Y') = '$tahun'", $link, __LINE__, __FILE__);
$senarai=mysql_fetch_array($viewall);
?>
<?php
if (empty($senarai))
{
?>
<br>
<table width="100%" border="0"align="center">
<tr>
<td align="center">
<b>
Maaf ! Tiada maklumat di dalam pangkalan data
</b>
</td>
</tr>
</table>
<P><BR>
<CENTER>
<A HREF="failureRateMenu.php">
<STRONG>
KEMBALI
</STRONG>
</A>
</CENTER>
<?php
}
else
{
?>

<?php
$bil=1;
while($senarai=mysql_fetch_array($viewall))
{
$viewCheck=query("SELECT id FROM tempfailure WHERE id = DATE_FORMAT('{$senarai["waktuTamat"]}','%m') ORDER BY id", $link, __LINE__, __FILE__);
$periksa=mysql_fetch_array($viewCheck);

if (date("m",$senarai["waktuTamat"])==$periksa["id"])
{
$nilai=$periksa["jumlah"]+1;
$SQLupd=query("UPDATE tempfailure SET jumlah = '$nilai' WHERE id =DATE_FORMAT('{$senarai["waktuTamat"]}','%m')", $link, __LINE__, __FILE__);
}
$bil=$bil+1;
}
echo date("m",$senarai["waktuTamat"]);
?>

</table>
<P><BR>
<?php

$viewCheck1=query("SELECT bulan,failureRate FROM tempfailure order by id", $link, __LINE__, __FILE__);


?>
<?php
while($periksa1=mysql_fetch_array($viewCheck1, MYSQL_NUM)) {
$array1[]=$periksa1[0];
$array2[]=$periksa1[1];
?>
<?php
}

$_SESSION["firstquarter"]=$array1;
$_SESSION["firstquarter2"]=$array2;

#The data for the pie chart
$data =$_SESSION["firstquarter2"];
#The labels for the pie chart
$labels=$_SESSION["firstquarter"];
#Create a XYChart object of size 300 x 280 pixels
$c = new XYChart(600, 380);
#Set the plotarea at (45, 30) and of size 200 x 200 pixels
$c->setPlotArea(230, 30, 300, 300);
#Add a title to the chart using 12 pts Arial Bold Italic font
$c->addTitle(" Kadar Kerosakan Mesin", "arialbi.ttf", 12);
#Add a title to the y axis
$c->yAxis->setTitle("Failure Rate");
#Add a title to the x axis
$c->xAxis->setTitle("Bulan");
#Add a blue (0x6666ff) 3D line chart layer using the give data
$barLayerObj = $c->addBarLayer($data, 0x6666ff);
$barLayerObj->set3D();
#Set the x axis labels using the given labels
$c->xAxis->setLabels($labels);

#Create the image and save it in a temporary location
$chart1URL = $c->makeSession("chart1");
#Create an image map for the chart
$imageMap = $c->getHTMLImageMap("xystub.php", "", "title='{label}:RM{value|0}K'");

?>
<IMG SRC="failureRateImg.php?<?PHP ECHO $chart1URL?>" BORDER="0" USEMAP="#map1"> <MAP NAME="map1">
<?PHP ECHO $imageMap?>
</MAP>
<table width="50%" border="1" align="center">
<tr>
<td align="center">
<table align="center" width="100%">
<tr>
<td align="center">
<b>Petunjuk Graf</b>
</td>
</tr>
<tr>
<td width="50%" align="center">
<b>Paksi X : Bulan Operasi Pada Tahun <?php echo $_POST["year"]; ?></b>
</td>
</tr>
<tr>
<td width="50%" align="center">
<b>Paksi Y : Jumlah Kerosakan (Failure Rate) Pada Bulan</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<INPUT TYPE="HIDDEN" name="noTag" value="<?php $_POST["noTag"];?>">
<INPUT TYPE="HIDDEN" name="year" value="<?php $_POST["year"];?>">
<center>
<input style="BACKGROUND-COLOR: #cccccc; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; COLOR: #333333; FONT-FAMILY: Verdana; FONT-SIZE: 7pt" type="submit" value="ANALISA FAILURE RATE" name="submit">
</center>
<?php
}
?>
</form>
<center><b>Sila klik pada sektor untuk mengetahui nilai carta</b></center>
</BODY>
</HTML>

first ,user choose a year...
than at the $viewCheck, the query will select only months within the year selected by the user..
id is an id represents 01,02,03....12(months)...
$senarai["waktuTamat"] is date in timestamp format....i query it earlier..
and i want to select all id(or months) that same to the months in $senarai["waktuTamat"] ..which i queried earlier...
can u help me?
the chart dont display..
if it does,it only show only the first month....

 
Old February 3rd, 2004, 02:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know if this is causing a problem, but you end up throwing away one of your result rows.

You call mysql_fetch_array($viewall) before your if() statement, then you call it in the conditional expression of your while() loop again, without ever using the first result row.

There's a lot of code there, and it's pretty messy, so it's difficult for someone to just read through it looking for problems. If you're still having problems, I STRONGLY suggest you beautify your code -- use proper indentation and spacing.

For example, this is your code:

Code:
<?php
$bil=1;
while($senarai=mysql_fetch_array($viewall))
{
$viewCheck=query("SELECT id FROM tempfailure WHERE id = DATE_FORMAT('{$senarai["waktuTamat"]}','%m') ORDER BY id", $link, __LINE__, __FILE__);
$periksa=mysql_fetch_array($viewCheck);

if (date("m",$senarai["waktuTamat"])==$periksa["id"])
{
$nilai=$periksa["jumlah"]+1;
$SQLupd=query("UPDATE tempfailure SET jumlah = '$nilai' WHERE id =DATE_FORMAT('{$senarai["waktuTamat"]}','%m')", $link, __LINE__, __FILE__);
}
$bil=$bil+1;
}
echo date("m",$senarai["waktuTamat"]);
?>
And this is it again with more sensible formatting:
Code:
<?php

$bil = 1;

while ($senarai = mysql_fetch_array($viewall))
{
    $waktuTamat = &$senerai['waktuTamat'];

    $query = "SELECT id FROM tempfailure 
               WHERE id = DATE_FORMAT('{$waktuTamat}', '%m')
               ORDER BY id";

    $viewCheck = query($query, $link, __LINE__, __FILE__);

    $periksa = mysql_fetch_array($viewCheck);

    if (date("m", $waktuTamat) == $periksa["id"])
    {
        $nilai = $periksa["jumlah"] + 1;
        $query = "UPDATE tempfailure
                     SET jumlah = '$nilai'
                   WHERE id     = DATE_FORMAT('{$waktuTamat}','%m')"

        $SQLupd = query($query, $link, __LINE__, __FILE__);
    }

    $bil = $bil + 1;
}

echo date("m", $senarai["waktuTamat"]);
?>
In my opinion, the second version is MUCH easier to read.


Take care,

Nik
http://www.bigaction.org/
 
Old February 3rd, 2004, 03:44 PM
Authorized User
 
Join Date: Jan 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i tried..
but it still the same....
nothing changes....

 
Old February 3rd, 2004, 08:07 PM
Authorized User
 
Join Date: Jan 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what do you mean by "throwing away one of your result rows"??

 
Old February 4th, 2004, 11:42 AM
Registered User
 
Join Date: Dec 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you have to do is to isolate the problem, even if it means putting echo statements incrementally through your code to see what gets executed. Start an echo statement where you think the code should start putting out the months, and see if it is looping properly, etc. You will innevitably find the source of the problem as some of the echo statements you think should be put to the screen will not do so. Either that or start with a smaller bit of code to debug, and then slowly add the extra.

ie. use the scientific method... experiment and test. Trying to get one big piece of code working without testing the parts is insane :) not to mention impossible.

 
Old February 4th, 2004, 02:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Throwing away one of your result rows" means exactly what it sounds like. You get a result row and overwrite it without ever using the values from it.

Here's your code, with irrelevant parts trimmed out:


$viewall = query((snip), $link, __LINE__, __FILE__);
$senarai = mysql_fetch_array($viewall);

if (empty($senarai))
{
    (snip)
}
else
{
    $bil = 1;
    while ($senarai = mysql_fetch_array($viewall))
    {
        (snip)
    }
(snip)
}


Look at what you're doing. You're getting a row of data from your "viewall" query, but you never use the data in that row -- you throw it away when you start your while() loop because you overwrite the value by getting the next row in the result set.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
database related query dkvaseeta Pro VB Databases 2 February 22nd, 2007 10:59 AM
Repeater Control Related Query param99 ASP.NET 1.0 and 1.1 Professional 1 December 14th, 2006 04:17 AM
query related to function nimeshkumargupta SQL Server 2000 0 January 18th, 2005 04:05 AM
Related Field Does Not Display in Query twsinc Access VBA 1 November 18th, 2004 10:39 AM
query related problem Abhinav_jain_mca General .NET 2 September 6th, 2004 02:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.