php - Accordion Content Using PHP/MySQL Query Data - php

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?

Related

Display all respective rows from a particular date

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 :)

Build a championship table in PHP from 3 MySQL tables

I am trying to display a championship table from the 3 tables I have in my MySQL database.
tbl_champ holds the championship name (nitro) and date (2015). tbl_rounds holds the round name (round1 so on) the date (01/01/2015 so on) and the champ_id. tbl_position holds the round_id, positions (1, 2, 3 etc) and racer_id (2, 4, 8 etc).
Inserting and updating the tables work great, but I cannot seem to get them to display as I want.
This is what I have:
echo '<table border="1">';
$sql_rou = mysql_query("SELECT * FROM `rounds`") or die(mysql_error());
while ($row_rou = mysql_fetch_array($sql_rou)) {
$rid = $row_rou[0];
echo '<th>' . $row_rou[1] . '</th>';
$sql_pos = mysql_query("SELECT `pos_user_id` FROM `positions` WHERE `pos_round_id`='$rid'") or die(mysql_error());
while ($pos_info = mysql_fetch_row($sql_pos)) {
echo '<tr>';
foreach ($pos_info as $field) {
echo '<td>'.$field.'</td>';
}
echo '</tr>';
}
}
echo '</table>';
But I get:
ROUND 1
[20]
[2]
ROUND 2
[2]
[20]
Any way to get the rounds next to each other, but keeping the []'s as they are?
(I've built the table in Excel).
Any ideas? Am I just thinking about this all wrong?
Take a look at where you close the brackets for the <th> entries. It should be about line 6 of your sample code. You have it at the bottom of the page. This will loop every result for each header value.
Also, you don't seem to have any <tr> in your <thead>. Think of your <th> as cells, like <td>, which need to be wrapped in a row.

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.

Text Injection In Display

Ok, so I'm given a list of parts like so:
PartID CatID PartName
0 1 Part 1
1 2 Part 2
2 1 Part 3
3 3 Part 4
4 2 Part 5
5 2 Part 6
I'm using PHP to pull. How can I inject a blurb of text at each change in CatID, without having to run multiple loops?
So on page, I can display it like:
"BLURB OF TEXT"
Part 1
Part 3
"BLURB OF TEXT"
Part 2
Part 5
Part 6
"BLURB OF TEXT"
Part 4
Here's the code so far. I thought about putting in an assignment to = $row["CatID"], and checking to see if the variable == CatID, but it was always ==...
while($row = sqlsrv_fetch_array($qry, SQLSRV_FETCH_ASSOC)) {
echo $row["PartName"]
}
Put the ID in a variable that is outside the loop. That way you can check the previous ID and the current one. If the current one is not the same as the last one, echo some text:
$LastID = 0;
while($row = sqlsrv_fetch_array($qry, SQLSRV_FETCH_ASSOC)) {
echo $row["PartName"];
if($row["CatID"] > $LastID)
{
// ID changed!
echo "Insert Text!";
}
$LastID = $row["CatID"];
}
This, ofcourse, is assuming that the data is sorted ascending on CatID as your result that you want is showing.
I had my logic backwards.... grr, figures I get it figured out right after I ask for help...
if($cCat == $row["catID"]){
//echo 'nothing here';
}else{
echo '<a name="' . $row["PartCategory"] . '"></a>';
echo $this->GetPartCategories();
$cCat = $row["catID"];
}
is what works, , and above the while statement is a $cCat = '';
I was trying
if(!$cCat == $row["catID"]){
echo '<a name="' . $row["PartCategory"] . '"></a>';
echo $this->GetPartCategories();
$cCat = $row["catID"];
}
which for some reason did not work.
p.s. Already have an order by in my query. It orders by the catID, partSortOrder, then partName

Displaying data from database with php that is meets requirements

I am designing an event feed from a calender I made. I'm using the same data from the database but to match specific dates and times.
I only want 4 events to show at once (why I specified length < 4)
Where the database value 'showFeed' is true, it only displays those rows.
and I want it to show by date time, I have odd id's for each value in the database, which might make them out of order.
My current code:
$sql = "SELECT `title`, `time`, `start`, `showFeed` FROM calender WHERE length('column') > '0'";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
echo "<div class=\"eventfeed\">";
echo "<ul>";
foreach ($result as $row){
$show = $row['showFeed'];
if ($show == 1 && length.$result < 4){
echo "<li>";
echo $row['title']. "<br />";
echo $row['start'].' '.$row['time'];
echo "</li>";
}
else {
return false;
}
}
echo "</ul>";
echo "</div>";
$dbh = null;
echo json_encode($return);
?>
I'm getting results and no errors from the database, but I'm only seeing one return on $results.
I honestly, do not have a clue where else to go from here. I'm lost.
For 1+.2.+3. modify your query to SELECT title, time, start, showFeed FROM calender WHERE length('column') > '0' and showFeed=1 and time<current_timestamp ORDER BY time DESC LIMIT 0,3 and remove your if (...) statement.
I don't know if this is your actual code, but you should determine the length of an array by doing $result.length instead of the other way around.
Also, you can limit the number of results in your query using 'LIMIT 4' at the end of your query. That way MySQL only returns 4 results and you don't have to worry about that in your code, just print everything.

Categories