|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

February 3rd, 2004, 01:02 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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....
|

February 3rd, 2004, 02:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: San Diego, CA, USA.
Posts: 833
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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/
|

February 3rd, 2004, 03:44 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i tried..
but it still the same....
nothing changes....
|

February 3rd, 2004, 08:07 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what do you mean by "throwing away one of your result rows"??
|

February 4th, 2004, 11:42 AM
|
|
Registered User
|
|
Join Date: Dec 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

February 4th, 2004, 02:06 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: San Diego, CA, USA.
Posts: 833
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
"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/
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |