简体   繁体   中英

PHP SQL previous and next date navigation

I have this code that I am using to show current day matches

<?php
$today = date('Y-m-d');
$data = mysql_query("SELECT * FROM todaysmatches where matchdate=$today") or die(mysql_error()); 

echo "<h4>$today Matches</h4> </br>";

//table
if(mysql_num_rows($data)==0){
    echo " No Matches";
}else{      
    echo "<table border='1'>
            <tr>
                <th>Match</th>
                <th>Tourmanet</th>
                <th>Date</th>
            </tr>";

    while($info = mysql_fetch_array( $data )){
        echo "<tr>";
        echo "<td>" . $info['curmatch'] . "</td>";
        echo "<td>" . $info['tournamentname'] . "</td>";
        echo "<td>" . $info['matchdate'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>

what I would like to get is 2 buttons for previous and next day that can go in the query and retrieve the data for that date, those buttons can be clicked multiple times not just ones

Try This

<?php
//$today = date('Y-m-d');
 $firstdate = $_POST['firstdate']; // first date which is today
$seconddate = $_POST['seconddate']; //or make this some date you want

//if submit button is pressed do this
if(isset($_POST['submit'])){

$data = mysql_query("SELECT * FROM todaysmatches where matchdate=$today") 
or die(mysql_error()); 

echo "<h4>$today Matches</h4> </br>";

//table
if (empty($data)){
 echo " No Matches";
 }
else{

echo "<table border='1'>
<tr>
<th>Match</th>
<th>Tourmanet</th>
<th>Date</th>
</tr>";

while($info = mysql_fetch_array( $data ))
 {
echo "<tr>";
echo "<td>" . $info['curmatch'] . "</td>";
echo "<td>" . $info['tournamentname'] . "</td>";
 echo "<td>" . $info['matchdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}

}//end submit button press
?>

<form action="" method="POST">
<input type="hidden" name="firstdate" value="<?php echo date('Y-m-d'); ?>"/>
<input type="submit" name="submit" value="submit" />
</form>

<form action="" method="POST">
<input type="hidden" name="seconddate" value=""/>
<input type="submit" name="submit" value="submit" />

 </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM