简体   繁体   中英

PHP Mysql - select * data from a specific Month,Year

I have this code:

<?php

$link = mysqli_connect("localhost", "root", "", "work");
 

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 

$sql = "SELECT count(`data`) AS `cnt_days`, sum(`ore`) AS `sum_hours`, DATE_FORMAT(`data`,'%M %Y', 'ro_RO') AS `year_month`, sum(`castig`) AS `sum_castig` FROM `rapoarte` WHERE 1 GROUP BY EXTRACT(YEAR_MONTH FROM `data`) ORDER BY EXTRACT(YEAR_MONTH FROM `data`) DESC;";


if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo '<br><br><br><table align="center" border="0">';
        echo '<tr>' . '<th height"100%" colspan="5">' . 'Ore Lucrate' . '</th>';   
        echo "<tr>";
                echo "<th>Month</th>";
                echo "<th>Nr. Tickets/month</th>";
                echo "<th>Nr. Hours Worked</th>";
                echo "<th>$ earned</th>";
                echo "<th>View all tickets for this month</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            $luna = $row['year_month'];
            echo "<tr>";
                echo "<td>" . $row['year_month'] . "</td>";
                echo "<td>" . $row['cnt_days'] . "</td>";
                echo "<td>" . $row['sum_hours'] . "</td>";
                echo "<td>" . $row['sum_castig'] . "€" . "</td>";
                echo '<td>' . '<form type="post" action="">' .'<input type="submit" name="export"/>' . '</form>' . '</td>';
        
                
            echo "</tr>";
        }
        echo "</table>";
        
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} 
else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
        



mysqli_close($link);
?>

I have this script that shows me in a table all the values in the mysql table for a month, for example if I have 5 entries in the table for January, it shows me the name of the month and year, it shows me the number of tickets for January, the total amount of hours worked and the amount earned.

I leave here an example of what the generated table looks like.

I don't know how I can do the following:

I want in the column where it says "View all tickets for this month" next to each month that appears to me to have a link / button that will open a new page that shows me all the values in the mysql table for that month (year_month).

Can someone help me? Thanks!

Add link in one of your table rows that display month_and year

 echo "<td>"<a href="/your_page?year_month='.$row['year_month']  . '">' . $row['year_month'] . "</td>"

And then you can get month and year like so

$year_month = explode('_', $_GET['year_month']);
$year = $year_month[0];
$month = $month[1];

In your SQL Query you can add where condition and use SQL MONTH and YEAR functions.

@gerald-allay I did it this way, the link looks something like this

<td><?php echo '<a href="uploads/'.$orow['raport']. '">' . $orow['raport'] . '</a>' ?></td>

it works, but it only shows me the data from November 2021 (which would be the first as an entry), if I click on the link for December or January, it doesn't show me anything. Why? I have data from Nov-2021 to Feb-2022.

Why do my entries appear only from November 2021??

and the rest of the code:

<?php 
    if(isset($_GET['year_month']) && $_GET['year_month']==$luna){
        include('db.php');
                $oquery=mysqli_query($conn,"SELECT * FROM `rapoarte` WHERE luna_an = '$luna'");
    
                if (mysqli_num_rows($oquery) <=0){
                    echo '<table align="center" width="300" border="0">
  <tbody>
    <tr>
      <td width="100" height="22"><img src="img/search.png" width="16" height="16" alt=""/>&nbsp;Niciun rezultat gasit!</td>
    </tr>
  </tbody>
</table>';
                }
                else{
                    echo '<table align="center" border="0">
                    <tr>
                    <th width="100%" colspan="9">Rezultate cautare:</th>
                    </tr>
        <tr>
             <th width="21" height="28">ID</th>
      <th width="166">Tichet</th>
      <th width="150">Data</th>
      <th width="150">Locatie</th>
      <th width="20">Prioritate</th>
      <th width="20">Nr. Ore Lucrate</th>
      <th width="98">SN Echip</th>
      <th width="220">Raport PDF</th>
      <th width="220">Data adaugare</th>
    </tr>';
                while($orow=mysqli_fetch_array($oquery)){
                    
                    ?>
                    <tr>
                        <td><?php echo $orow['id']?></td>
                        <td><?php echo $orow['tichet']?></td>
                        <td><?php echo $orow['data']?></td>
                        <td><?php echo $orow['locatie']?></td>
                        <td><?php echo $orow['prioritate']?></td>
                        <td><?php echo $orow['ore']?></td>
                        <td><?php echo $orow['sn']?></td>
                        <td><?php echo '<a href="uploads/'.$orow['raport']. '">' . $orow['raport'] . '</a>' ?></td>
                        <td><?php echo $orow['data_add']?></td>
                    </tr>
                    <?php 
                }
                }
    
    
    }           
        ?>

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