Jump to content

[SOLVED] Display particular record that match the database data..


alvinchua

Recommended Posts

i need help~~ i need to display the data only for the paricular year and this is the code

[code]<?php

if (isset($_POST['enter']))
{
$year  = $_POST['select'];
echo $year;
echo $myrow["Year"];
$Year = $myrow["Year"];
if ($Year == $year)
{
do {
printf("<tr><td>%s</td><td>%s</td><td>RM %s</td><td>RM %s</td></tr>\n", $myrow["Date"], $myrow["Description"], $myrow["Debit"], $myrow["Credit"]);
} while ($myrow = mysql_fetch_array($result));
//echo "</table>\n";
}
//else 
//	{
//	echo 'That record appears to be unavailable'; 
//}
}	

 

what this code does it just comparing the first year in the database only... how to make it to compare every row and display only the data match the year ?? [/code]

Link to comment
Share on other sites

Hi, firstly PHP is case sensitive when it comes to using variable names so it isnt good practice to have two variables names Year and year and this can cause major headaches when trying to debug code.  Also to display records from the database you need to run a query on it ie. Select * from table where date >= today

 

Only once you have queeried your database and got a recordset back can you loop through it.  You will need to write your query using SQL and you will need to specify what data you require from what table.

 

As an example take the following SQL query example:

 

1.  $query = ("SELECT * FROM `order` WHERE CUSTOMERID = '$id' ORDER BY ID DESC");

2.  $sql = mysql_query($query) or die(mysql_error());

 

line 2 $sql is assigned the result of the query contained in line 1

 

You need to insert your query before the while loop which checks for record existance.

 

Link to comment
Share on other sites

Another issue I forgot to mention is date comparrison.  Are you comparing two strings or two dates?

 

If the values are contained in strings then you will need to convert them to date objects before trying to do a comparrison such as if this date is later than another date

 

$mydate = DATE("d/m/Y");

 

Bear in mind also that MySQL stores dates in YYYY/MM/DD format compared to DD/MM/YYYY format that we have in UK.  Not sure whether this applies to you.  If you need to split a date string simply use the PHP explode function as follows:

 

$templist = explode("/", $mydate);

$newdate = $day.'/'.$month.'/'.$year;

 

Hope this helps

Link to comment
Share on other sites

errm.. i think i din make myself clear.. lol.. okok is like that... i got all those query to fetch the result from the database and compare.. i am comparing the string and using the php to generate the date... what i need is to write a statement to compare every single row of record see whether it is year 2005,2006,2007 and only display the respectively record... for example, if a row have the year 2006 and i selected 2006 from the combo box ... it will display that particular year.. and this is the code...

 

<?php
$year1 = mktime(0, 0, 0, date("m"), date("d"), date("y"));
$year2 = mktime(0, 0, 0, date("m"), date("d"), date("y")-1);
$year3 =mktime(0, 0, 0, date("m"), date("d"), date("y")-2);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("login", $con);

$query="SELECT * FROM transaction";

$result = mysql_query($query)
or die(" - Failed More Information:<br><pre>$query</pre><br>Error: " . mysql_error());

$num_rows = mysql_num_rows($result);
$myrow = mysql_fetch_array($result);
?>
<table class="view">

<tr bgcolor="lightsteelblue"><td><b>Date</td><td width="381"><b>Description:</td><td><b>Debit</td><td><b>Credit</td></tr></b>

<?php

if (isset($_POST['enter']))
{
$year  = $_POST['select'];
echo $year;
echo $myrow["Year"];
$Yearfrmdatabase = $myrow["Year"];
if ($Yearfrmdatabase == $year)
{
do {
printf("<tr><td>%s</td><td>%s</td><td>RM %s</td><td>RM %s</td></tr>\n", $myrow["Date"], $myrow["Description"], $myrow["Debit"], $myrow["Credit"]);
} while ($myrow = mysql_fetch_array($result));
//echo "</table>\n";
}
//else 
//	{
//	echo 'That record appears to be unavailable'; 
//}
}	
mysql_free_result($result);
mysql_close();

 

if you guys notice that i echo the result .. both will dispaly the same year..and echo $myrow["Year"]; this is to retrieve the year from the database and $year is from a form ... what this code does it just comparing the year of the first record in the database and will NOT move to the next one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.