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
Related
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.
I am making a sort of forum with subjects out of my database. My website is built in PHP (PDO) the problem is that I cant get the subject out of the database to show the correct way under each other in each a different block.
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Stel jezelf voor</h2>
<h5> Laat wetn wie jij en je business zijn</h5>
</div>
</div>
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
<?php
$toppics = $app->get_topics();
$i = 0;
$j = 0;
foreach ($toppics as $topic) {
if($j >= 1) continue;
echo '' . $topic['onderwerp'] . '';
$j++;
}
?>
</div>
</div>
Function:
public function get_topics(){
$getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC LIMIT 1");
$topics = $this->database->resultset();
return $topics;
}
I want this also to be each record in a new block. In the code, you can see I tried some stuff but it isn't the right way or its not working. I know the j++ isn't good but if I delete that and the LIMIT 1 function it doesn't still don't work
UPDATE
If I use the code like it is in the answer given it looks like this and not 2 diffent blocks.
<?php
$toppics = $app->get_topics();
$i = 0;
foreach($toppics as $topic){
echo '<div>';
echo '<h3>'.$topic['onderwerp'].'</h3><br>';
echo '' .$topic['omschrijving'].'';
echo '</div><br>';
}
?>
First of all: Since you want to fetch multiple topics from the DB, you must remove the LIMIT 1 from the query and the if($j >= 1) continue; in the foreach loop, as they both are restricting your output to only 1 topic.
In your foreach loop for $toppics (correct spelling: topics ;P) you currently only echo an anchor tag (link), but what you want is (to use your words here) a 'block'. Whatever you want that block to look like, the place for defining that is within that foreach loop.
Now I don't know what elements, classes or stylings you use / want to use, so I will make an example of a block that consists of a title and below that the link:
//rename $topic keys to the names of your DB columns
foreach($toppics as $topic){
echo '<div>';
echo '<h3>'.$topic['title'].'</h3><br>';
echo ''.$topic['link_text'].'';
echo '</div><br>';
}
I know my solution will not look exactly like your given image, but it should get the point across how and where you can build your blocks.
I think this problem should have been easily solveable when you know the basics of HTML, so I would really recommend you learning a bit more about HTML before you work on big projects.
Edit after question was edited:
As I mentioned in my answer, my solution will not look exactly like your given image because I don't know what elements, classes or stylings you use. Your remaining problem is now the usage of the correct html tags, classes and stylings.
It appears that the parent element of the generated divs is styled the way you want the single blocks to look like.
So what you could do is remove the parent element and use it as a replacement of the generated div, like so:
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Stel jezelf voor</h2>
<h5> Laat wetn wie jij en je business zijn</h5>
</div>
</div>
<div class="col-md-6">
<!--<div class="well dash-box">-->
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
<?php
$toppics = $app->get_topics();
$i = 0;
foreach($toppics as $topic){
echo '<div class="well dash-box">';
echo '<h3>'.$topic['onderwerp'].'</h3><br>';
echo '' .$topic['omschrijving'].'';
echo '</div><br>';
}
?>
<!--</div>-->
</div>
sidenote: I do not agree with your building of your href attribute #section1. When building these sections you would have to know that exact index from that previous foreach-loop. Instead, use some attribute from the topic itself, maybe its ID, title, or description (like I did in the first codeblock). This way when you are building the sections you can easily know how to set the elements id attribute.
I've the following query
As you see jigsaw repeats twice because the movie has two categories
but i would like to echo both categories but not twice the movie..
<?php
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-xs-6 col-sm-4 col-md-3">
<a href="movie.php?title=<?php echo $mInfo['titleEN']; ?>&m=<?php echo $mInfo['imdbID']; ?>" title="<?php echo $mInfo['titleEN']; ?>" alt="<?php echo $mInfo['titleEN']; ?>" target="_self">
<div class="movie-info">
<img src="assets/images/poster/<?php echo $mInfo['poster']; ?>.jpg" alt="<?php echo $mInfo['titleEN']; ?>">
</div>
<div class="movieinformation">
<div class="movie-title"><?php echo $mInfo['titleEN']; ?></div>
<div class="movie-categories">I WOULD LIKE TO ECHO THE CATEGORIES HERE</div>
</div>
</a>
</div>
<?php
}
?>
So far i just could do it, could anyone help me with that?
Here's a concept for such tasks. I kept it with general names and left out html on purpose.
$oldMainId = null; // Initialize to null to start with. This will be needed later.
while ($item = $result->fetch()) { // get your items (movies in your case)
if($oldMain != $item['mainId']) {
// only show title if you haven't yet
echo $item['mainTitle'];
}
// always show the category though
echo $item['sub'];
// re-set the 'old' variable.
$oldMainId = $item['mainId'];
}
I would use
OPTION 1
$res = $stmt->fetchAll(PDO::FETCH_GROUP);
Then it will group on the first column, assuming that is movie ID, or something unique to the movie you would get multiple rows in a nested array for that movie.
Then when you iterate thorough them you can loop over the nested array for the genre stuff
OPTION 2
Another option is to use GROUP_CONCAT and group by the movie id,
SELECT GROUP_CONCAT(genre) AS genre, ... WHERE ... GROUP BY movieID
But be aware that GROUP_CONCAT does have a setting for max length and it will silently truncate your data if it exceeds it.
OPTION 3
You can build the structure yourself (same as fetch group does)
$data = [];
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
$key = $mInfo['movieid']
if(!isset($data[$key])) $data[$key] = [];
$data[$key][] = $mInfo;
}
Then go through that and do your html by using a second foreach($data as ...) It has to be done after organizing the data as the order of the result is unknown.
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"
I have a website where I am getting information of college student profiles on a database and displaying it as a linked collection. When I'm looping through the database I want to give each row a specific link to the profile of the student. Right now I am linking it to "profilePage.html" with generic information but I want it to be correlated with the row the user chose on the last(college) page.How do I save/transfer that information to the page. I do not want multiple profile pages but one template that is filled with the user previous choice.
<?php
$result = mysql_query("SELECT * FROM student_info WHERE college='Boston College'", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
?>
<a href="profilePage.html" class="collection-item">
<div class="row summary">
<div class="col s4 center">
<img class = "profile-pic" src="img/defaultProfile.jpg">
</div>
<div class="col s8">
<div class="title"> <?php echo $row[student_Name]; ?> </div>
<div class="black-text condensed thin"><i class="tiny material-icons">today</i> Founder, CEO at Linkle since January 1st, 2015</div>
<div></div>
</div>
</div>
</a>
<?php } ?>
Key thing, my urls are mysite.com/college.php and have no id's to specify them.
Structure of the Database student_info:
Shows the structure of the database
First, do you have an URL Rewriting ? If not, your target page should be a PHP page, like profilePage.php.
Your link to this page have to include a parameter (Query String) which is, for example, the student's ID :
<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">
This type of URL will be generated: profilePage.php?id=36
In profilePHP.php, retrieve the parameter in the Query String :
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>
mysql_real_escape_string() is really important, it prevents SQL injections.
After that, you could retrieve the student's informations with a SQL query.
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
// you should check if the ID exists, and if there is 1 result
?>
<p>Student name is <?php echo $student['student_Name'] ?></p>
A little advice: mysql_query() will disappear soon, you should take a look at PDO.