Jump to content

Show Everything from last month


Bendude14

Recommended Posts

Ok i have this code that queries my database and prints out the latest results... The problem is at the moment i can only get it to print out the one set of results but i would like it to print out the results from the last month... Can it be done with out duplicating this code and then setting the $seconds variable to 1week 2 week etc.

 

Thanks in advance

 

<?php

$sql = "SELECT win_team, lose_team, win_score, lose_score FROM results WHERE time > NOW()- $seconds LIMIT 1";
$result = mysql_query($sql) or trigger_error('Query failed: '. mysql_error());


?><table cellpadding="5px">
<?php
while($dbarray = mysql_fetch_array($result)){

	echo "<td style=\"font-weight:bold;\">".$dbarray['win_team']."</td>";
	$win_team = $dbarray['win_team'];

	echo "<td> </td>";
	echo "<td style=\"font-weight:bold;\">".$dbarray['win_score']."</td>";
	echo "<td> </td>";
	echo "<td style=\"font-weight:bold;\">".$dbarray['lose_score']."</td>";


	echo "<td> </td>";
	echo "<td style=\"font-weight:bold;\">".$dbarray['lose_team']."</td>";
	$lose_team = $dbarray['lose_team'];
}

$sql2 = "SELECT player1, player2, player3, player4, player5, player6 FROM teams WHERE team_name = '$win_team'";
$result2 = mysql_query($sql2) or trigger_error('Query failed: '. mysql_error());
$result2 = mysql_fetch_row($result2) or trigger_error('Query failed: '. mysql_error());

$sql3 = "SELECT player1, player2, player3, player4, player5, player6 FROM teams WHERE team_name = '$lose_team'";
$result3 = mysql_query($sql3) or trigger_error('Query failed: '. mysql_error());
$result3 = mysql_fetch_row($result3) or trigger_error('Query failed: '. mysql_error());
       $i = 0;
  foreach($result2 as $players) {
  
  
 	echo "<tr><td>".$players."</td><td> </td><td> </td><td> </td><td> </td><td> </td> <td>".$result3[$i]."</td></tr>";
		$i++;

  }

?>

Link to comment
https://forums.phpfreaks.com/topic/115374-show-everything-from-last-month/
Share on other sites

I suppose you can use switch as a method of viewing the latest records.

 

<?php
$from = $_GET['from'];

function month(){
$m = ((date("m")-1) > 0) ? date("m")-1 : 12;

$day = 86400;
$to = 31*$day;
$t = 30*$day;
$ms = array(1 => $to, 2 => 28*$day, 3 => $to, 4 => $t, 5 => $to, 6 => $t, 7 => $to, 8 => $to, 9 => $t, 10 => $to, 11 => $t, 12 => $to);

return $ms[$m];
}

switch($from){
case 'week': $seconds = 7*86400; break;
case 'biweek': $seconds = 14*86400; break;
case 'month': $seconds = month(); break;
}

// your query
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.