Display all respective rows from a particular date - php

Can you all please do me a favor ? I am stuck at one place. I want to display all records from a particular date in loop.
for eg. I have 8 records of 29 march, then I want to display header of 29 march and show all the records of 29 march via loop inside a div.
I have 12 records of 29 march and 10 records of 27 march. So far I have been successful in showing 27 march and 29 march dates from a loop via GROUP BY clause but all the records are not showing inside a particular date
Instead of showing all 12 records of 29 march, I am getting only 1 row for each date. I am attaching screenshots and codes for better understanding. What I am getting is below:
As you can see above, I am getting only 1 row for each date instead of all rows related to that date
What I want is below:
Not good editing haha...But I hope it gives you all clear idea as what I want to accomplish. All rows of 29 march via loop and all rows of 27 march via loop. I have attached codes...Please see what can be done
Code (PHP & MYSQLI)
<?php
$Prepare_Dealers_Purchases_Query = "SELECT * FROM `purchases` WHERE `DealerEmailAddress` = '$ShowDealerEmailAddress' GROUP BY `PurchaseDate` ORDER BY `InvoiceID` DESC";
$Fire_Dealers_Purchases_Query = mysqli_query($CreateConnection, $Prepare_Dealers_Purchases_Query);
if(mysqli_num_rows($Fire_Dealers_Purchases_Query) == 0) {
echo "<div id='userFeedbackPanel' style='margin:15% auto;padding:10px;'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td>You have not purchased any product yet</td></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr><td><a onClick=\"$.fn.openProductSelectionBox();\" class=\"StartShoppingButton\"><i class=\"fa fa-shopping-cart fa-lg\"></i> Start Shopping</a></td></tr></table></div>";
} else {
while($Show_Purchases = mysqli_fetch_array($Fire_Dealers_Purchases_Query, MYSQLI_ASSOC)) {
if($Show_Purchases['SelectedProduct'] == "Regular Colours") {
$Shade_Colour = $Show_Purchases['SelectedShade'];
$Purchase_Date = $Show_Purchases['PurchaseDate'];
$Prepare_Dealers_Purchases_As_Per_Date_Query = "SELECT * FROM `purchases` WHERE `DealerEmailAddress` = '$ShowDealerEmailAddress' AND `PurchaseDate` = '$Purchase_Date' ORDER BY `InvoiceID` DESC";
$Fire_Dealers_Purchases_As_Per_Date_Query = mysqli_query($CreateConnection, $Prepare_Dealers_Purchases_As_Per_Date_Query);
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br>";
while($Show_Purchases_As_Per_Date = mysqli_fetch_array($Fire_Dealers_Purchases_As_Per_Date_Query, MYSQLI_ASSOC)) {
echo "<div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td style='background:$Shade_Colour;border-radius:5px;width:54px;height:64px;'></td><td>You purchased ".$Show_Purchases_As_Per_Date['SelectedQuantity']." boxes of ".$Show_Purchases_As_Per_Date['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases_As_Per_Date['FinalAmount']." by using ".$Show_Purchases_As_Per_Date['PaymentMethod']." of ".$Show_Purchases_As_Per_Date['BankName']." at ".$Show_Purchases_As_Per_Date['PurchaseTime']." on ".$Show_Purchases_As_Per_Date['PurchaseDate']."</td></tr></table></div><br>";
}
} else {
$Product_Image = $Show_Purchases['SelectedProductImage'];
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br><div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td><img src='$Product_Image' width='54' height='64'></td><td>You purchased ".$Show_Purchases['SelectedQuantity']." boxes of ".$Show_Purchases['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases['FinalAmount']." by using ".$Show_Purchases['PaymentMethod']." of ".$Show_Purchases['BankName']." at ".$Show_Purchases['PurchaseTime']." on ".$Show_Purchases['PurchaseDate']."</td></tr></table></div><br>";
}
}
}
?>
I am using 2 while loops. 1 loop for getting all dates and other loop for getting rows of that particular date. Please show me where I am making mistake. Thank you all...

It seems you're going there:
else {
$Product_Image = $Show_Purchases['SelectedProductImage'];
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br><div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td><img src='$Product_Image' width='54' height='64'></td><td>You purchased ".$Show_Purchases['SelectedQuantity']." boxes of ".$Show_Purchases['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases['FinalAmount']." by using ".$Show_Purchases['PaymentMethod']." of ".$Show_Purchases['BankName']." at ".$Show_Purchases['PurchaseTime']." on ".$Show_Purchases['PurchaseDate']."</td></tr></table></div><br>";
}
There is no while loop here, so only one line is displayed. Add the same while loop as you did in the if statement and it should be fine :)

Related

php - Accordion Content Using PHP/MySQL Query Data

I have a list of soccer results stored in a MySQL database. Where a result meets a specific criteria, I've been able to use a PHP/SQL query to generate a static list where each result is grouped by its appropriate season using the solution provided here.
The structure of the table from which I'm looping the data through (plus example data) is this:
Date Season Comp Opponent Score Match ID
======================================================================================
1980-08-16 1980-1 UCL1 2 3-2 1
1980-09-23 1980-1 UCL1 4 1-0 5
1981-01-13 1980-1 UCL1 11 0-2 20
1982-03-24 1981-2 UCLP 9 2-2 42
1982-09-15 1982-3 UCLP 24 0-0 58
1982-12-12 1982-3 UCLP 20 4-1 74
1983-02-04 1982-3 UCLP 26 0-2 90
1983-05-01 1982-3 UCLP 25 1-1 104
Using the above linked solution, I can bring through the data to create a basic static list like this:
1980-1
- Result 1: 1980-08-16, 3-2 (vs. 2)
- Result 2: 1980-09-23, 1-0 (vs. 4)
- Result 3: 1981-01-13, 0-2 (vs. 11)
1981-2
- Result 4: 1982-03-24, 2-2 (vs .9)
1982-3
- Result 5: 1982-09-15, 0-0 (vs. 24)
- Result 6: 1982-12-12, 4-1 (vs. 20)
... and so on
But instead of showing results in a static list, the goal is to use this data to dynamically generate a jQuery accordion – using individual seasons as panel buttons/headers and then filtering relevant results into the panel-body. The query/code I've so far developed to try and achieve this is:
$query = "SELECT mt.ID,mt.ID1,re.date,re.season,re.venue,re.comp,re.opponent,re.score,ce.name,ce.ID,re.ID FROM resultengine re INNER JOIN matchteam mt ON re.ID=mt.ID INNER JOIN clubengine ce ON re.opponent=ce.ID WHERE (mt.ID1='0' or mt.ID1='9998') ORDER BY re.season ASC, re.date ASC";
$result2 = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
$totalRows = mysqli_num_rows($result2);
if($result2) {
if ($totalRows == 0) {
echo "<p>No games here</p>";
}
else {
while ($row = mysqli_fetch_array($result2)) {
{$yearonly = date("l d F Y",strtotime($row['2']));}
if($row['3'] != $season) {
echo "<button class='accordion'>";
echo $row['3'];
$season = $row['3'];
echo "</button>";
}
echo "<div class='panel'>";
echo "<h4 class='result-date'>".$yearonly."</h4>";
echo "<div class='result-info'>";
echo "<a href='/seasons/match/".$row['10']."/' class='result-data result-link'>";
echo "<span class='result-column sidebar-comp'>".$row['5']."</span>";
echo "<span class='result-column sidebar-venue'>".$row['4']."</span>";
echo "<span class='result-column sidebar-team'>".$row['8']."</span>";
echo "<span class='result-column sidebar-score'>";
echo "<span class='player-score-box'>";
echo "<span class='player-score-main'>".$row['7']."</span>";
echo "</span>";
echo "</span>";
echo "</a>";
echo "</div>";
echo "</div>";
}
}
}
I know that this current code will create a new <div class='panel'> for each individual result. So, this means one result is visible under each season – the live example is here. To get it showing as intended with all results from one season showing in the appropriate panel, I'm missing a step (or component) from my query – and I can't work out what it is.
My initial thought is that I need to use foreach in some capacity, but can't figure this out. I've used answers to questions here and here as possible solutions. But I've either ended up having season panels repeated based on how many results correspond or the first result from each season being repeated multiple times. What am I missing to get the desired outcome?
I could probably make it work with a nested query, but am led to believe this should be avoided?

Creating a table of dates from a database

Before I begin I'm not asking for a solution - as I'm trying to learn I'm just looking for some pointers so I can work it out myself.
I'm currently learning php and mysql, and have been given the task of creating a resource booking table and form. A form places a name, resource, start and end date into a database, and then this needs to be extracted and displayed as a table on a web page, so users can see if there is a meeting already booked at their desired time.
Final product mockup:
Here is the database table I made in phpmyadmin (called "meetings"):
I've written a while loop to loop through hours (from 8:00 to 19:00). This works left to right not up and down, so when checking if a meeting is already booked I'll have to search for 8:00 monday, then 8:00 tuesday etc:
$i = 8;
while($i < 20)
{
echo "<tr>".
"<td>".$i.":00</td>".
"<td></td>".
"<td></td>".
"<td></td>".
"<td></td>".
"<td></td>".
"<td></td>".
"</tr>";
$i++;
}
and I've started a function "isMeeting" but I have no idea where to go from here
function isMeeting()
{
$query= "SELECT * FROM meetings"; //WHERE hour part of start datetime field is $hour?
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
}
}
What would be my next steps to populating the table?

Everything is Out of Order: Php - MySQL

I am trying to create a php script that gets values from a MySQL database, displays a header with the time_stamp that comes from an array, and a sub header with a value called "working_ra" with values underneath those categories. The values that go underneath both categories are being displayed in a table. I am sorting the values of the working_ra in the query so I can run a while loop saying that if they're not the same to display the next working_ra and continue echoing values. Here is my code:
foreach ($date as $i) {
echo "<pre>"."<h2>".$i."</h2>"."</pre>";
$result = mysqli_query ($con, "SELECT * FROM Signing WHERE value1 = 'Ings' AND time_stamp = '$i' ORDER BY working_ra");
$num_results = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$desk = $row["working_ra"];
echo "var_desk starting value ","<b>", $desk,"</b>";
echo "<table border='1'>";
echo "<tr>";
echo "<td>", $row["first"],"</td>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ((string)$desk != (string)$row["working_ra"]) {
echo "</table>";
$desk = $row["working_ra"];
echo "var_desk next value ","<b>",$desk,"</b>";
echo "<br />";
echo "<table border='1'>";
}
echo "<tr>";
echo "<td>", $row["first"],"</td>";
echo "</tr>";
}
$result->free();
}
$con->close();
Everything works fine until it hits to the second "desk" name. Then the tables that are supposed to be with the next value get shifted to the next date. Here is what it looks like on the page:
aSFashagh, Jeremy, Jeremy, and Johnny are all supposed to be under (Phil, January 10 2013) but is shifted down into the next date and working_ra (Phil, January 20 2013). zffaA, andsdfdsggsdhj are all supposed to be under (Phil,January 20 2013) but are under (Phil, February 25 2013) instead. Whats weird is that the method did work WITHOUT trying to put all of the values in tables.
My assumption is that it could be the ordering of which the table tags are ending and starting, but I have tried numerous amounts of things and still cannot figure out what is wrong.
EDIT
Sorry its very confusing to say. But im trying to get it to where everything is shifted up the way its supposed to be. Where the values of "first" belong with their corresponding time_stamp and working_ra. For instance, the value of Johnny in the MySQL db has a value of working_ra - Phil and a value of time_stamp - January 10, 2013. However as you can see, Johnny is not underneath (Phil, January 10 2013) instead January 20, 2013. Its not the query thats the problem, it has something to do with the while loop and the table and I just can't figure it out.
The only two values that are correct are Zack and Why which have corresponding values of (working_ra = "Bob", time_stamp = "January 10, 2013")
I believe your problem is with the mysql row pointer. You already used mysqli_fetch_array once before the while loop therefore shifting the current mysql row to the next row.
This code
mysqli_data_seek($result, 0);
will reset the pointer.
Place it right before your while loop.
Figured out what the problem was. I needed to add a "/table" tag when the while loop breaks.

bad planing with mysql_fetch_assoc?

I am making a page where people can make posts. All of those posts are then shown in a table of 24 cells. I can have the last 24 posts shown with no problem, but now I don't know how to show the prior group(s) of posts. How can I fix my code to do that? I actually have this:
(I'm removing lines to make it easy to read)
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC";
// ---check everything is fine---- //
function retrieve_info($result)
{
if($row = mysql_fetch_assoc($result))
{echo $topic_if; echo $topic_subject; //and what I want in every cell
}
}
<table width="100%" height="751" >
<tr><td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td></tr>
<!-- repeat a few more times :-) -->
</table>
I though that by changing the variable $row with a number before the if statement would alter the output, but I still see the same data printed on screen. What should I do to be able to show next group of posts?
Thanks!!!
At some point when you have hundreds or thousands of records, you are going to want to paginate the results and not just select all records from the table.
To do this you will run one query per 24 records, your sql would be more like this:
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC
LIMIT 0, 24
";
and for the next 24,
LIMIT 24, 24
then
LIMIT 48, 24
and so on.
You would then make next/previous buttons to click which would refresh the page and dispay the next 24, or you would get the next results with an AJAX request and append the next 24 through the DOM.
This suggests having to take a slightly different approach then calling the same function from each table cell.
More like get the relevant 24 results based on the page number you are on, then loop through the results array and print out the table code with values inside it. Based on if the iterator of the loop is divisible by 4 (looks like your grid is 4x6), you print out new tags for the new row, and that sort of thing.
Search around a bit for pagination in php and mysql to get a sense of how this all fits together.
function retrieve_info($result)
{
while($row = mysql_fetch_assoc($result))
{
$topic_id = htmlspecialchars($row['topic_id']);
$topic_subject = htmlspecialchars($row['topic_subject']);
echo '<td>';
echo $topic_if;
echo $topic_subject; //and what I want in every cell
echo '</td>';
}
}

How do I loop through results and display day of the week once at every change in day using php and mysql?

with my current query and loop:
$sched = mysql_query("SELECT *
FROM `shows`
ORDER BY `shows`.`show_time` ASC")
or die(mysql_error());
echo "<ul>";
while($row = mysql_fetch_array($sched)){
echo "<li><a href=\"#$row[id]\">";
echo $row['title'];
echo "</li>";
}
echo "</ul>";
This works great for displaying my results like this:
Name of show 1
Name of show 2
Name of show 3
However, I want to add an item to the list at the beginning of every change in day so it would display as follows:
Monday
Name of show 1
Name of show 2
Tuesday
Name of show 3
Wednesday
Name of show 4
I can't quite wrap my brain around the loop needed to do this. It might be helpful to know that the field 'show_time' is a datetime type, so it has the information for both time and day of week.
Thanks.
Simple tweak:
echo "<ul>";
$curDay='';
while($row = mysql_fetch_array($sched)){
$d=date('l',strtotime($row['show_time']));
if($d!=$curDay){
echo '<li>'.$d.'</li>';
}
$curDay=$d;
echo '<li><a href="#',$row['id'],'">',$row['title'],"</li>";
}
echo "</ul>";
Initialize $curDay, and then each time through the loop, check to see if the particular day is different than the last time through the loop (or different from the initial value)
The best way to do this is to keep a flag in your loop, and compare to the previous value.
Eg.
$previousDay = '';
while($row = mysql_fetch_assoc()) {
if ($previousDay != date('l', $row['show_time'])) {
echo '<h2>' . $date('l', $row['show_time']) . '</h2>';
}
...
$previousDay = date('l', $row['show_time']);
}
Adjust your query to sort by show_time first.
"SELECT * FROM `shows` ORDER BY `show_time`, `shows` ASC"
Then keep track of the current day as Shad suggests, parsing show_time to determine the day.

Categories