select all rows with sport=padel - php

I want to select all the content of the row when sport= padel.
My code is this:
$result = mysqli_query($con,"SELECT sport FROM posts WHERE sport='padel'
");
while($row=mysqli_fetch_assoc($result)){ ?>
<div class= 'box1'>
<p align='middle'><?php echo $row['username']; ?></p>
<h3 align='middle'>Playing</h3>
<h3 align='middle' style= 'color:blue'><?php echo $row['sport']; ?> </h3>
<div><p align='middle' >Disponibilidade:</p>
<h3 align='middle' style='color:blue'><?php echo $row['text']; ?></h3></div>
<p></p>
<h3 align='middle' style='color:blue'><?php echo $row['city']; ?></h3>
<button text-align='middle' id='submit1' type='submit1'>
contact </button>
</div>
This, in fact, select all the row with sport=padel, but all the other content in the different columns is missed (gives me undefined variable)
I think I could do this:
$result = mysqli_query($con,"SELECT sport FROM posts WHERE sport='padel'
WHERE text='' WHERE city=''");
but didn't work. Can anyone help me?

As others have mentioned in the comments, you need to specify what columns you want to pull back, at the moment you are pulling back the sport column only.
SELECT * FROM posts WHERE sport="padel"
This will ensure you pull back all columns. Alternatively you can specify multiple comma separated columns
SELECT col1, col2, col3 FROM posts WHERE sport="padel"

Related

Cannot group table values by ID

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

IF inside WHILE with database result not working

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.

PHP/MYSQL: showing data out of 2 tables

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 want to distinct a specific column from a table

Hi i want to get distinct column data from a table.
my code is below.
how can i make the season column distinct?
<div class="episodes">
<?php
$name=$_GET['name'];
$select=mysql_query("select * from data where name='$name'");
while($show=mysql_fetch_array($select))
{ ?>
<div class="episodes_data">
<div class="t_s_pic">
<img src="admin/photo/movieForsaken-2016-movie-Justin-Price-6-355x500.jpg" height="100%" width="100%"/>
</div>
<div class="t_s_name"><h3>Game Of Thrones<br/>season<?php echo $show['season'];?></h3></div>
<div class="t_s_download">Download series</div>
</div>
<?php } ?>
</div><br>
for obtain distinct column you don't should use select * by explicitally select the column you need eg:
select distinct column1, column2, column3 from data where name='$name'

Two query at the same time

Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
your SQL statement needs a JOIN in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1 via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"], $row["path3"], etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.

Categories