PHP MYSQL A SIMPLE QUERY

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peter162in
Forum Newbie
Posts: 12
Joined: Mon Jul 06, 2009 11:37 am

PHP MYSQL A SIMPLE QUERY

Post by peter162in »

I have two php files. one is calender.php which generates calender
the other one is week.php which is below.When I click date in calender.php it passes the date to week.php
my table is in mysql ,name testdb. it contain three columns( date item and price.)
After getting date

date item price

eg.
2009-01-20 xerox paper 90
2009-01-20 transparency cover 12
2009-01-13 xerox paper 100
2009-01-13 transparency cover 14

end result should be

xerox paper 90-100 -10 and % of that
ransparency cover 121-4 -2 %

I am not a professional programmer, I learn programming using internet, reading books and asking experts.
MY CODE LOOK LIKE THIS

Code: Select all

< php 
include "dbcon1.php"; 
$date1=$_GET['date']; // this is initial date 2009-01-20 
echo"$date1". "<br />"; 
$difference = strtotime ( '-1 week' , strtotime ( $date1 ) ) ; // getting week ago 
$date2=date ( 'Y-m-d' , $difference ) . "<br />\n" ; //this is week ago in the required format 2009-01-13 
$sql = "SELECT item, price FROM table1 WHERE (((table1.date)='$date1')) "; //getting price on 2009-01-20 
$sql1 = "SELECT price FROM table1"."WHERE (((table1.date)='$date2'))"; //getting price on 2009-01-13 
$result = mysql_query($sql) 
or die(mysql_error()); 
$result2 = mysql_query($sql1) 
or die(mysql_error()); 
$header=<<<EOD 
<h2><center>price variation - <font color="red" size="6"> </center></h2> 
<table width="70%" border="1" cellpadding="2" 
cellspacing="2" align="center"> 
<tr> 
<th>DATE</th> 
<th>ITEM</th> 
<th>PRICE</th> 
<th>DIFFERENCE</th> 
<th>PERCENT</th> 
</tr> 
EOD; 
$details = ''; 
while ($row = mysql_fetch_array($result2)) 
{ 
$price2 = $row['price']; 
} // 
 
while ($row = mysql_fetch_array($result)) 
{ 
// $date = $row['date']; 
$item = $row['item']; 
$price = $row['price']; 
$price1 = $price2; // This Line is out of reach for looping through. of $result2.
$gain =($price-$price1); // this is what i exaclty required. 
$percentage=(($gain*100)/$price1); 
$details .=<<<EOD 
<tr> 
<td>$date</td> 
<td><font color="red" size="4"> $price</td> 
<td> $price11</td> 
<td><font color="red" size="4"> $gain</td> 
<td><font color="blue" size="4"> $percentage</td> 
</tr> 
EOD; 
} 
$footer ="</table>"; 
$output =<<<ALL 
$header 
$details 
$footer 
ALL; 
echo $output; 
?>
Please help IT is not working.
Last edited by peter162in on Tue Jul 07, 2009 1:11 am, edited 3 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP MYSQL A SIMPLE QUERY

Post by requinix »

peter162in wrote:Please help IT is not working.
What is not working? How is it not working?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: PHP MYSQL A SIMPLE QUERY

Post by omniuni »

Well, for an obvious note, line 8 does not have the comment escaped.
peter162in
Forum Newbie
Posts: 12
Joined: Mon Jul 06, 2009 11:37 am

Re: PHP MYSQL A SIMPLE QUERY

Post by peter162in »

The first result is not looping through.
My screen out put is

item price as on date1 price as on date2 diffr
xerox 90 100 10
paper 20 blank 20
Post Reply