How to Retrive data of the Previous month with Loop
Hi,
I'm using a php & MySQL. In the MySQL table name Employee. it has 3 fields EmpYear, EmpName, EmpDateAdded where data are records from january to december 2008. On the otherhard, In my php code i created also a table with 4 columns ColYear, ColName, ColDateAdded, ColPrevYear. My concern is on the ColPrevYear, how do i code in php to get the data in fieldScore to display in ColPrevYear assuming that i am now viewing data for the month of July and i want to display in ColPrevYear the last month(June). However, if no year found for the month of June, it will loop back on the next previous months until score is found.
Anybody please help me with this...
Thanks in advance.
Re: How to Retrive data of the Previous month with Loop
You can achieve this in one of Several ways ...
One way is to build an array using a numeric index as you fetch results from your data source. Then as you build your html code, use $yourArray[$i - 1] to access the previous element in your array.
Another way is to store off the currently fetched data in a variable and then refer back to that variable as "previous".
Re: How to Retrive data of the Previous month with Loop
PHP Code:
<?php
include 'init.php';
if (!is_authed())
{
header("Location: http://xxxx/wewewe/wee.ph");
exit;
}
$ID = $_SESSION['ID'];
$results = mysql_query("SELECT * FROM `WorkExperience` WHERE ID=$ID");
$row = mysql_num_rows($results);
if($row == 0)
{
mysql_query("INSERT INTO `WorkExperience` (ID) VALUES ('$ID')");
}
mysql_query($query);
}
if($_POST['next'])
{
header("Location: gotonextpage.php");
exit();
}
$query = "SELECT * FROM `WorkExperience` WHERE ID=$ID";
$results = mysql_query($query);
$data = mysql_fetch_array($results);
$CountryOfEmployment = $data['CountryOfEmployment'];
$CommencementDay = $data['CommencementDay'];
$CommencementMonth = $data['CommencementMonth'];
$CommencementYear = $data['CommencementYear'];
?>
This should work for you. Obviously change the SQL to suit your database, etc.