I am trying to make a simple event form, which displays the date of the event and any events with the same date underneath.e.g. matching date events are listed underneath the same date. Currently it is displaying each insert individually rather than grouping by date.
$readquery = "SELECT * FROM events WHERE id=$userid GROUP BY event_date";
$readresult = $link->query($readquery);
if (!$readresult) {
echo $link->error;
}
Above is the data being called from the SQL table.
<?php
while ($row = $readresult->fetch_assoc()) {
$userid = $row["id"];
$event_date = $row["event_date"];
$event_item = $row["event_item"];
echo "
<br>
<div class='card border border-dark'>
<b> $event_date</b>
<div class='card-body' style='background-color:#6497ED;'>
<div class='card border-dark mb-3 mx-auto' style='max-width: 18rem;'>
<div class='card-body text-dark'>
<h5 class='card-title text-center'>$event_item</h5>
</div>
</div>
</div>
</div>";
}
above is trying to display this in a card. Any help would be great!
You say "Currently it is displaying each insert individually rather than grouping by date." and that is because you do not use any aggregator function like (COUNT, MAX, MIN, SUM, AVG) with this work you say to your query to group your result and I think use the aggregator function in SQL will help you to solve your problem.
Related
I have two tables "naujienos" and "apzvalgos" in my SQL Database. I managed to join them both and get all records from both tables using UNION and ORDER BY timestamp. What i need to do is to match records id by current id to show particular record on webpage.
Here is my code :
<?php
$sql="Select * from `naujienos` UNION Select * from `apzvalgos` order by `timestamp` DESC";
$result=mysqli_query($con,$sql);
if($result){
while($row=mysqli_fetch_assoc($result)){
$id=$row['topic_id'];
$topic_image=$row['topic_image'];
$topic_name=$row['topic_name'];
$topic_desc=$row['topic_desc'];
$timeStamp=$row['timeStamp'];
$kategorija=$row['kategorija'];
echo '<div class="card-group pl-5 col-md-3 col-sm-6 mb-5 pb-5">
<div class="card">
<img class="card-img-top paveiksliukas" src='. $topic_image.' alt="Card image cap">
<div class="card-body">
<h5 class="card-title mb-5">'. $topic_name.'</h4>
<p class="card-text">'.substr($topic_desc,0,200).'</p>
Skaityti toliau
</div>
<div class="text-center">
<b>Kategorija: '. $kategorija. '</b>
</div>
<div class="text-center pt-1 pb-1">
<b>Įkelta: '. $timeStamp. '</b>
</div>
</div>
</div>';
}
}
?>
Here is how it looks on webpage (its basically all records from two tables and whenever i click on button i go to that particular record):
This is the code line where when i click on button i get to that particular record by id :
Skaityti toliau
My problem is that when i click on "apzvalgos" table button it sends me to "naujienos" table record, because ive written this:
Skaityti toliau
Right now its only working with "naujienos" records and when i click on "apzvalgos" record it shows me "naujienos" record instead.
Both tables have identical columns.
How can i change the code that when i click on records from "naujienos" and "apzvalgos" table's it shows current record from those two tables?
I've tried to change the code line
Skaityti toliau
to somehow get current table record's id, but i have no idea how to do it.
Can someone help me to fix this problem?
Skaityti toliau
//on naujienos.php file
$id1 = REQUEST['naujienos_id'];
//Show record
$sql = "SELECT * FROM naujienos INNER JOIN apzvalgos ON naujienos.naujienos_id=apzvalgos.apzvalgos_id WHERE naujienos_id='$id1'";
//your code here
Hey there guys/girls I have an issue I'm currently trying to work through being a novice to MYSQL / PHP. Currently I'm using Bootstrap accordion collapsible components to display HTML tables (That are reports). Here is my current table:
Current Table in MYSQL.
So as you can see the reports row contains some HTML information which are tables. I wanted to take the information and display it on a webpage assuming that every row was a different report. So I was able to do so with writing this:
<div class="accordion" id="accordionExample">
<?php
require('db.php');
$i = 0;
$sql = "SELECT `report` FROM `automation-reports`;";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($query))
{
foreach($row as $key => $value)
{
?>
<div class="card">
<div class="card-header" id="heading<?php echo $i ?>">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse<?php echo $i ?>" aria-expanded="true" aria-controls="collapse<?php echo $i ?>">
Report #1: 8/6/2018
</button>
</h5>
</div>
<div id="collapse<?php echo $i ?>" class="collapse" aria-labelledby="heading<?php echo $i ?>" data-parent="#accordionExample">
<div style="text-align: center;" class="card-body">
<h3 style="float: left;"> Rating-Pull: </h3>
<?php
$i++;
echo $key;
echo "$value";
?>
</div>
</div>
</div>
<?php
}
}
?>
</div>
Which is great because it does what I thought I wanted it to do , which is this:
Display Output
What's not so great is now I realize that multiple reports are going to be in one accordion "folder" which is where the reportid row comes into play. So lets say I run my program and two (different) reports run on it but I want it in the say "folder" on the webpage. Both of these get labeled with a reportid of 1.
So what I want to do is loop through reports and then if they have the same ID group them together in that folder and iterate through the whole table like that. So that's the part where I have attempted to do so with a nested loop and SELECT 'report' FROM 'automation-reports' WHERE 'reportid' = '$i' ; and I just ended up getting the first element. Could somebody give me a hand with this and a good explanation so I can understand and learn what's happening?
Thank you!
EDIT:
Maybe a visual would be better?
VISUAL
I think GROUP BY and GROUP_CONCAT are what you are looking for.
SELECT `reportid`, GROUP_CONCAT(`report` SEPARATOR '') as report
FROM `automation-reports`
GROUP BY `reportid`
shoud do the job.
SELECT *
FROM `automation-reports`
GROUP BY `reportid`
will give you one row for each id ie.
id 1,
id 2,
id 3
Or do you want to display each row like so?
id 1, id 1,
id 2,
id 3, id 3,
id 4
if so here is a possible example of combining the reports in a loop first
$sql = "SELECT reportid, GROUP_CONCAT(report SEPARATOR ',') as reports FROM `automation-reports` GROUP BY `reportid`;";
while($row = mysqli_fetch_assoc($query)) {
echo "<div id='{$row['reportid']}'>";
echo $row['reports'];
echo "</div>";
}
I know this isn't the HTML you're after but you should be able to place your HTML in this code
I have a simple ticket sale registration system with 6 types of tickets and 3 different "physical" sales locations. Each order is inserted as a row in the database and stores the amount of each type of ticket along with the sale location, total cost and a datetime timestamp.
I want to display the total amount of each ticket type, that was sold within a given time frame along with the total cost of those tickets. I also want to filter the results based on sale location.
This is my db query:
"SELECT SUM(ticketType1), SUM(ticketType2), SUM(ticketType3),
SUM(ticketType4), SUM(ticketType5), SUM(ticketType6),
SUM(cost), saleLocation
FROM `orders`
WHERE time BETWEEN '2018-07-10 07:00:01'
AND '2018-07-11 07:00:00'"
Then I display it in a HTML table row:
<?php
while ($row = $result->fetch_assoc()) {
if($row["saleLocation"] == 'location1') { ?>
<div class="cell">
<?php echo $row["SUM(ticketType1)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType2)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType3)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType4)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType5)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType6)"] ?>
</div>
<div class="cell">
<?php echo number_format($row["SUM(cost)"], 0, "", "."); ?>
</div>
<?php } //end if
} // end while
?>
It works, but I'm trying to use an IF statement inside the WHILE loop to filter the result based on location, but the IF statement doesn't appear to have any effect and instead it displays the results from all 3 locations.
I know I can easily modify the query to achieve this, but I would rather not do that in this specific case.
I'm guessing that the problem still lies within the query, though?
Some help would be greatly appreciated.
This is my first question and I tried to make it clear enough so you guys/girls can understand it. If I am missing something or doing something wrong please let me know! Its my first question.
What I want:
I want it to show the voornaam out of the table Klanten to show next to the other ones out of the table onderwerpen. For example I want the voornaam to show next to the henk and koken. So I expect it to show the data out 2 different tablesin the block you can see on the pic.
I tried using a foreach loop with 2 conditions but that it not possible. So I was wondering what the solution in this case needs to be. Because It works with 1 condition in the foreach but it is not possible to do this with 2 conditions.
What I have tried:
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Onderwerpen</h3>
</div><!-- /.box-header -->
<div class="box-body">
<?php
$onderrwerp = $app->get_onderwerpen();
$klantten = $app->get_klanten();
foreach($onderrwerp as $onderwerp and $klantten as $klant){
echo '<div class="well well-sm">';
echo '' . $onderwerp['voornaam'].'';
echo '<h3>'.$onderwerp['naam'].'</h3><br>';
echo '' .$onderwerp['naam'].'';
echo '</div>';
}
?>
The code:
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Onderwerpen</h3>
</div><!-- /.box-header -->
<div class="box-body">
<?php
$onderrwerp = $app->get_onderwerpen();
foreach($onderrwerp as $onderwerp){
echo '<div class="well well-sm">';
echo '<h3>'.$onderwerp['naam'].'</h3><br>';
echo '' .$onderwerp['naam'].'';
echo '</div>';
}
?>
The functions:
public function get_onderwerpen(){
$getOnderwerp = $this->database->query("SELECT * FROM onderwerpen ORDER BY id ASC");
$onderwerpen = $this->database->resultset();
return $onderwerpen;
}
public function get_klanten(){
$getKlant = $this->database->query("SELECT * FROM klanten ORDER BY punten DESC");
$klanten = $this->database->resultset();
return $klanten;
}
So you have two tables. they are connected using the ledenpagina_id field I assume looking at your data.
What you are looking for is to JOIN the two tables so that with one query you can fetch the data you want and then do a loop to show the data:
SELECT *
FROM klanten JOIN onderwerpen
ON klanten.lendenpagina_id = onderwerpen.lendenpagina_id
You can then add WHERE clause, ORDER_BY and so on.
I have a donation site and a little bit stuck on finding what percent of the Donation Goal that the Current Donations are at?
In my MySQLi database I have two columns - donation_goal (numerical eg. 500) and current_raised (numerical eg. 20.00).
What I need to do is find and display how much in percentage has been raised currently out of the donation goal. So if £10 was donated and the goal was £100 it would display 10%. But it would do this each time the page was loaded.
I display each page with the code below and need to find a way to add this feature to this code below:
<?php
$sql = "SELECT * FROM pages ORDER BY RAND() LIMIT 3";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$page_name = $row['page_name'];
$count = strlen($page_name);
if($count < 27){
$space = '<br> <br/>';
} else {
$space = '';
}
$page_dp = $row['page_dp'];
$donation_goal = $row['donation_goal'];
$current_raised = $row['current_raised'];
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="pageimg/<? echo $page_dp; ?>" style="width:348px;height:233px;" alt="Page Image">
<div class="caption">
<h3><? echo $page_name; ?> <? echo $space; ?></h3>
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%">10%
<span class="sr-only">10% Complete (success)</span>
</div>
</div>
<p><b>£<? echo $donation_goal; ?>.00</b> Target | <b>£<? echo $current_raised; ?></b> Raised</p>
Here is a little info about this current topic, please help us raise more than we can! We need your help!
<p>Donate Now Details</p>
</div>
</div>
</div>
<?
}
?>
I have tried adding this code to the mix, but receiving errors so am pretty sure I have no idea how to add it correctly:
$sql = "SELECT * FROM pages, COUNT( current_raised ) AS test, concat(round(( current_raised/donation_goal * 100 ),2),'%') AS percentage ORDER BY id DESC LIMIT 3";
This just gave out errors. Any ideas of how to do this efficiently with short code?
If you would like to calculate the percentage of the dontation from the goal you could do this directly in your query:
SELECT page_dp,
donation_goal,
current_raised,
ROUND(current_raised/donation_goal*100) percentage
FROM pages
ORDER BY RAND()
LIMIT 3
Then you only need to output the percentage within your loop:
<span class="sr-only"><?php print $row['percentage']; ?>% Complete (success)</span>
Is this what you want?
SELECT p.*, pc.cr / donation_goal
FROM pages p CROSS JOIN
(SELECT SUM(current_raised) as cr FROM pages) pc
ORDER BY RAND()
LIMIT 3;