How to fetch previous date data in php mysql - php

I am working on stock project in PHP.Currently while searching according date it is showing data according searching date which is good.But those date which is not avail in database ,it is showing blank,while i need prevous date data.(Like if i enter 25.12.2015,in the result it should show previous date data like 23.12.2015 in this case.)
my html code is:
<form action="dateview.php" method="post">
<table width="60%" border="2" bordercolor="green">
<tr>
<td>DATE</td>
<td>
<input type="date" name="date">
</td>
<td colspan="2">
<center><input type="submit" value="search"/></center>
</td>
</table>
</form>
my php code is:
<?php
if($qw="select * from details where date='$date'"){
$qq = mysqli_query($con,$qw);
while($r=mysqli_fetch_array($qq,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $r['itemname']; ?></td>
<td><?php echo $r['deposit']; ?></td><td><?php echo $r['withdraw']; ?></td>
<td><?php echo $r['total']; ?></td>
<td><?php echo $r['approvedby']; ?></td>
<td><?php echo $r['receivedby']; ?></td>
<td><?php echo $r['givenby']; ?></td>
<td><?php echo $r['receivedto']; ?></td>
</tr>
<?php } ?>

Just tweak your SELECT query:
SELECT * FROM details WHERE date <= 'your-date-here' ORDER BY date DESC LIMIT 1

You might use something like this
date('Y-m-d', strtotime("23.12.2015")); // o/p: 2015-12-23
Where 'Y-m-d' format you stored in DB.
Refer date format here

Related

how to know the max number in each row of mysql database and return the column name or return name in phpmysql

How to know the max number in each row of mysql database and return the column name or return name in phpmysql
I want the dominant row to display the max value of a row , the row has this column (o_result, c_result,e_result,_result )
My code is
<tbody>
<?php
$result= mysqli_query($connection,"select * from test_report order by test_id ASC" ) or die (mysqli_error($connection));
while ($row= mysqli_fetch_array ($result) ){
$id=$row['test_id'];
?>
<tr>
<td><?php echo $row['test_id']; ?></td>
<td><?php echo $row['date_of_submission']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['o_result']; ?></td>
<td><?php echo $row['c_result']; ?></td>
<td><?php echo $row['e_result']; ?></td>
<td><?php echo $row['a_result']; ?></td>
<td><?php echo $row['n_result']; ?></td>
<td><?php echo $row['dominant']; ?></td>
<td><button class="btn search"></button>
<button class="btn edit"></button>
<button class="btn btn-danger"></button>
</td>
</tr>
<?php } ?>
</tbody>
You want to identify the greatest value on each row, and the name of the column that holds that value.
A simple method uses greatest() and case:
select
t.*,
greatest(o_result, c_result, e_result, a_result, n_result) as max_result,
case greatest(o_result, c_result, e_result, a_result, n_result)
when o_result then 'o_result'
when c_result then 'c_result'
when a_result then 'a_result'
when n_result then 'n_result'
end as max_column_name
from mytable t

PHP MYSQL Table delete button not working

Here is the query statement that I have. For some reason it works for another table I have but doesn't work with this table. I checked phpmyadmin to see if I would create an error but there were zero errors.
$Store_ID = filter_input(INPUT_POST,'Store_ID',FILTER_VALIDATE_INT);
//DELETE the Store
$query="DELETE FROM store_location WHERE Store_ID = :Store_ID";
$statement2=$db->prepare($query);
$statement2->bindValue(':Store_ID',$Store_ID);
$statement2->execute();
$statement2->closeCursor();
Here is the table code for the Delete Button
<?php foreach ($stores as $s) : ?>
<tr>
<td><?php echo $s['Store_ID']; ?></td>
<td><?php echo $s['Loc_Street']; ?></td>
<td><?php echo $s['Loc_City']; ?></td>
<td><?php echo $s['Loc_State']; ?></td>
<td><?php echo $s['Rent']; ?></td>
<td><?php echo $s['License']; ?></td>
<td><?php echo $s['Inspect']; ?></td>
<td><form action="stores.php" method="post">
<input type ="hidden" name ="Store_ID" value ="<?php echo $s['Store_ID']; ?>"/>
<input type="button" name="delete" value="Delete"/>
</form></td>
</tr>
<?php endforeach; ?>
</table>

Making table in PHP from database

I have a database called clanovi (members in englsih) which has stored name, surname, adress, email and gender of members. What I am trying to do is make a table from this data from database.
Here is my code https://jsfiddle.net/9j34xfwx/2/
<body>
<?php
require_once("Konekcija.php");
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("umv");
$sql=mysql_query("SELECT * FROM clanovi ORDER BY rbroj ASC");
$ime='ime';
$prezime='prezime';
$adresa='adresa';
$email='email';
$spol='spol';
?>
<table id='display'>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
<tr><td>Prezime clana: <?php echo $rows[$prezime]; ?></td>
<tr><td>Adresa clana: <?php echo $rows[$adresa]; ?></td>
<tr><td>Email clana: <?php echo $rows[$email]; ?></td>
<tr><td>Spol clana: <?php echo $rows[$spol]; ?></td>
<?php
}
?>
</table>
Here is photo of my web page when it lists all members
from database http://pho.to/ACgD1
My question is, how can I make a table so members name, surname, adress, email and gender are in columns, and beneath are the data from database. As you can see on the photo, my code adds one member beneath another member.
You're creating a new row for every value (and then never closing those rows, which the browser is attempting to correct for you):
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
^--- here
Just create the row once for each iteration of the loop:
<tr>
<td>Ime clana: <?php echo $rows[$ime]; ?></td>
etc.
</tr>
I assume that you don't need to repeat the text like "Ime clana:" (say label) in each Iteration, and need to set it as column header instead, cause that's the exact problem that the HTML <table> is designed to solve. Also if you need the label data to be redundant, then you must be defeating the purpose of using a table, IMHO.
You need to do something like this..
<table id='display'>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td></tr>
<?php
}
?>
</table>
Use this It's work for you.
<table id='display'>
<thead>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
</thead>
<tbody>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

fetching data according date in php and mysql [duplicate]

This question already has answers here:
How to fetch previous date data in php mysql
(2 answers)
Closed 6 years ago.
enter image description here
I am working on stock project.While I am fetching data date wise ,all data is displaying.Like i am searching 23.12.2015 ,it is fetching both records(item in 300 and 200),while i need only last record on particular date like here only second record(item in 200).
my html code is:
<form action="dateview.php" method="post">
<table width="60%" border="2" bordercolor="green">
<tr>
<td>DATE</td>
<td>
<input type="date" name="date">
</td>
<td colspan="2"><center><input type="submit" value="search"/></center></td>
</table>
</form>
my php code is:
<?php
if($qw="select * from details where date<='$date'"){
$qq = mysqli_query($con,$qw);
while($r=mysqli_fetch_array($qq,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $r['itemname']; ?></td>
<td><?php echo $r['deposit']; ?></td><td><?php echo $r['withdraw']; ?></td>
<td><?php echo $r['total']; ?></td>
<td><?php echo $r['approvedby']; ?></td>
<td><?php echo $r['receivedby']; ?></td>
<td><?php echo $r['givenby']; ?></td>
<td><?php echo $r['receivedto']; ?></td>
</tr>
<?php
In your code
$qw="select * from details where date<='$date'"
Replace with
$qw="select * from details where STR_TO_DATE(date,'%d/%m/%Y') <='$date'"

Cannot connect rows in html with php (logical error display)

The html table rows aren't displayed properly. I want to combine the two while loops in the table row but sadly the Update and Delete button are not arranged properly.
here's my code I used two queries that's why it has two while loops
$sql_sel=mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name FROM students, enrollments, subjects WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
$sql_sel1=mysql_query("SElECT * FROM enrollments");
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<?php
while($row=mysql_fetch_array($sql_sel1)) //for the second query
{ <!-------The Update and Delete Button are not displayed properly------>
?>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
<?php
}
?>
</tr>
<?php
}
?>
Here is the visual scenario of the problem:
The desired output must be like this:
In table header use colspan='4' for the last column.
Also be sure that you fill the table with empty columns, where you don't have information to fill with.
Edit 1:
Sorry, I haven't seen what's really the problem was. Here should be the working code:
while($row=mysql_fetch_array($sql_sel))
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<!-- You were already in a while loop -->
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
As you can see you were already in a while loop, and the second one was unnecesary.
Edit 2:
There is a single SQL query now:
<?php
// UPDATED SQL QUERY
$sql_sel = mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name, enrollments.enroll_num
FROM students, enrollments, subjects
WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
while($row = mysql_fetch_array($sql_sel)){
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
Why it didn't worked?
You were reading the whole informations for every student. Then you were reading the whole informations in enrollments table.
You started writing the first row with student information, and inside it you told the server to start writing all the information he had regarding enrollments (without even be linked to that student's id).
When the server reached the second row, all the information available for enrollments were depleted.
Now you have them linked in your first query. Please ask in comments if you need further explanations.
Try this,
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.gender, sub.sub_code, sub.subject_name, ets.enroll_num
FROM students sts
JOIN enrollements ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.sub_code = ets.sub_code)
GROUP BY sts.stud_id, sub.sub_code";
$sql_sel=mysql_query($sql);
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
I have added enroll_num column in select, And you dont need two queries for this. One query with proper join will be fine.

Categories