Select data from my three tables - php

I have three tables, one named album, while the other one is soundtrack and last but definitely not the least, is the artist.
Under one album is more than one track (obviously) and an artist name...
So this is how it should look like:
Desired Output:
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
| 2. One call Away |
|---------------------|
| Album by Ed Sheeran |
|---------------------|
| 1. Perfect |
|---------------------|
| 2. Dive |
|---------------------|
And below is mysqli_fetch_array() that I used along side with while-loop to populate the <table> tag.
$query = "SELECT a.* AS Album, b.* AS Track, c.* AS Artist
FROM album a
INNER JOIN track b ON a.album_id=b.album_id
INNER JOIN artist c ON a.artist_id=c.artist_id
ORDER BY a.album_id ASC";
$result = mysqli_query($con, $query);
$c=0;
while($row = mysqli_fetch_assoc($result)) {
$album = $row['Album']." (".$row['Artist'].") ";
$track = $row['Track'];
}
The problem is, when I ran this code... I could only get this result...
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
| 2. One call away |
|---------------------|
| 1. Perfect |
|---------------------|
| 2. Dive |
|---------------------|
It got the list of the soundtracks, including the soundtrack's ID right, however, things didn't do well on the Album Name
I want it just to look like the Desired Output. Any ideas? I've tried to search stackoverflow, but it seems it couldn't direct me on the same question, so I made my own. Please forgive (do not flag) me if it had an existing already, its just that I didn't know where to find it. And pleeeeease... I need an immediate and working answer. Thanks guys!
As for DATABASE Structure
album = {album_id, artist_id,album_title}
artist = {artist_id, artist_name}
track = {song_id, album_id, song_title}
UPDATE!
Okay, so this goes a little bit out of control. When I tried #swellar's suggestion this what happened. And when I undo it, the output remains the same.
See the result below...
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. See you again |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 2. One Call Away |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 1. Perfect |
|---------------------|
|AlbumName by Charlie |
|---------------------|
| 2. Dive |
|---------------------|
What I want is to give each Album one or more than soundtrack but it get so messed up each time I dare to change the query. How can I sanitize the mysqli_fetch_array() for it to display the desired output?

Here is your SQL
Notice I've put 3 order by in there. First by the artist name, which would keep all artist's stuff grouped together, then by album name and then by song id.
$sql = "SELECT a.album_title AS Album,
t.song_title AS Track,
t.song_id AS ID,
n.artist_name AS Artist
FROM album AS a
INNER JOIN track AS t
ON a.album_id = t.album_id
INNER JOIN artist AS n
ON a.artist_id = n.artist_ID
ORDER BY n.artist_name ASC,
a.album_id ASC,
t.song_id ASC";
/*
That SQL will generate the following array
Array
(
[Album] => AlbumName
[Track] => See you again
[ID] => 1
[Artist] => Charlie
)
Array
(
[Album] => AlbumName
[Track] => One call Away
[ID] => 2
[Artist] => Charlie
)
Array
(
[Album] => Album
[Track] => Perfect
[ID] => 1
[Artist] => Ed Sheeran
)
Array
(
[Album] => Album
[Track] => Dive
[ID] => 2
[Artist] => Ed Sheeran
)
*/
Below is your while loop
<table>
<?php
// Some placeholders
$album = null;
while($row = mysqli_fetch_assoc($result))
{
if($row['Album'] !== $album)
{
?>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;"><?php echo $row['Album']; ?> by <?php echo $row['Artist']; ?></td>
</tr>
<tr>
<td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
</tr>
<?php
}
else
{
?>
<tr>
<td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
</tr>
<?php
}
$album = $row['Album'];
}
?>
</table>
Overall Output is:
<table>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;">AlbumName by Charlie</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">1: See you again</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">2: One call Away</td>
</tr>
<tr>
<td style="border: 1px solid #000000; background-color: #dedede;">Album by Ed Sheeran</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">1: Perfect</td>
</tr>
<tr>
<td style="border: 1px solid #000000;">2: Dive</td>
</tr>
</table>
There are better ways to go around doing this, but not knowing your work environment, skills, requirements, other code, w/e this is the best quick and dirty solution for you to base your solution of. This works to what you requested.

Related

PHP for html table with dynamic rows and columns where row data relies on column heading

I have created an assessment system that grades each pupil on each lesson. I have a form that creates a table with the following headings:
Pupil name
Pupil ID
Lesson title
Lesson ID
Lesson grade
To view the data I am trying to create a table that works a bit like a spreadsheet.
Where each row takes the pupil ID and searches in the database for the grade i.e.
SELECT * FROM table WHERE pupilid = "rowid" AND lessonid = "columnheading"
I can create the column and row headings using PHP with while loops, but I can't figure out how to make each cell link between the column heading and pupil ID.
The only way I have managed to create this is with floating divs.
It works but it is very hard to style and the names and grades are in different grids so it is difficult to sort.
I would really appreciate any help / links to ways to do this.
Each week new pupils could be added and each lesson column will be added to the spreadsheet.
The table should sort of look like the one below:
+-------+----+----+--- +
| Pupil | L1 | L2 | L3 |
+-------+----+----+----+
| John | B | C+ | D |
+-------+----+----+----+
| Sarah | B | A | F |
+-------+----+----+----+
| Jim | D | A | B |
+-------+----+----+----+
I can create the column/row headings using the code below. I would really appreciate some help getting the code for the grade bit.
<table>
<tr>
<th>Pupil name</th>
<?php
$selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
while($rowslesson=$selectlesson->fetch_array())
{
?>
<th><?php echo $rowslesson['lessonid']; ?></th>
<?php
}
?>
</tr>
<?php
$selectpupil=$connect->query("SELECT DISTINCT pupilid FROM `grades` ");
while($rowspupil=$selectpupil->fetch_array())
{
?>
<tr>
<td><?php echo $rowspupil['pupilid']; ?></td>
</tr>
<?php
}
?>
Thanks in advance for your help.
Actually I figured out how to do it. I repeated the SQL search in the TD and it seemed to work
<table>
<tr>
<th>Pupil name</th>
<?php
$selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
while($rowslesson=$selectlesson->fetch_array())
{
?>
<th><?php echo $rowslesson['lessonid']; ?></th>
<?php
}
?>
</tr>
<?php
$selectpupil=$connect->query("SELECT DISTINCT pupilid FROM `grades` ");
while($rowspupil=$selectpupil->fetch_array())
{
?>
<tr>
<td><?php $pupil= $rowspupil['pupilid'];
echo $rowspupil['pupilid']; ?>
</td>
<?php
$selectlesson=$connect->query("SELECT DISTINCT lessonid FROM `grades` ");
while($rowslesson=$selectlesson->fetch_array())
{
?>
<td><?php $lessongrade = $rowslesson['lessonid'];
$selectgrade=$connect->query("SELECT * FROM `grades` where lessonid ='$lessongrade' and pupilid = '$pupil' LIMIT 1 ");
while($rowsgrade=$selectgrade->fetch_array())
{
echo $rowsgrade ['grade'];
}?>
</td>
<?php
}
?>
</tr>
<?php
}
?>
</tr>

How can I mark the values from one table, that are not in another table (mySQL)?

In mySQL this is my table animals:
+-----------+--------+--------+
| animal_ID | name | animal |
+-----------+--------+--------+
| 1 | alan | dog |
| 2 | sam | frog |
| 3 | marion | cat |
| 4 | george | rabbit |
| 5 | bob | bird |
+-----------+--------+--------+
and this is my table orders
+----------+-----------+
| date | animal_ID |
+----------+-----------+
| 02.03.16 | 4 |
| 12.04.16 | 3 |
| 18.07.16 | 1 |
+----------+-----------+
I want to list all animals but mark the animals that are NOT in the orders table red. This is my expected result
<table >
<tr>
<td>alan</td>
</tr>
<tr>
<td style="color:red">sam</td>
</tr>
<tr>
<td>marion</td>
</tr>
<tr>
<td>george</td>
</tr>
<tr>
<td style="color:red">bob</td>
</tr>
</table>
I have now the problem, that I do not know how to list all animals from the animal table, I just get the animals in my list that are not in the orders table:
$sql = 'SELECT * FROM animals a WHERE EXISTS (SELECT * FROM orders o WHERE o.animal_ID = a.animal_ID)';
Here my result is this:
<table >
<tr>
<td>alan</td>
</tr>
<tr>
<td>marion</td>
</tr>
<tr>
<td>george</td>
</tr>
</table>
You need to make this script using two query.
1. Query for all the animals.
2. Query with the animals ID in the orders.
The first one is:
$sql = 'SELECT * FROM animals";
Loop this and check in the order table inside the first loop-
$sql2 = "SELECT * FROM orders WHERE animal_ID = fetched_animal_ID";
if(mysqli_num_rows($sql2) > 0){
// print the normalrows here
}else{
// print the red rows here
}
==============================================================
You can do this using the LEFT JOIN as well-
$sql = "SELECT *, o.animal_ID as o_animal FROM animals a LEFT JOIN orders o
ON a.animal_ID = o.animal_ID";
Online Example
Now you need to check the o_animal if it is null or empty then use the red rows and else the normal.
If you have any question please ask me.
You can try another method without using join query.
<?php
include 'conn.php';
$sq=mysqli_query($conn,'select * from orders');
$i=0;
$ar=array();
while($re=mysqli_fetch_array($sq))
{
$anid=$re['animal_ID'];
$ar[$i]=$anid;
$i++;
}
$sql=mysqli_query($conn,"select * from animals");
?>
<table >
<?php
while($all=mysqli_fetch_array($sql))
{
if(in_array($all['animal_ID'],$ar))
{
?>
<tr>
<td><?php echo $all['name'];?></td>
</tr
<?php
}
else
{
?> <tr>
<td style="color:red"><?php echo $all['name'];?></td>
</tr>
<?php
}
}
?>
</table>
Simple negation?
$sql = 'SELECT * FROM animals a WHERE NOT EXISTS (SELECT * FROM orders o WHERE o.animal_ID = a.animal_ID)';
Or maybe grab the count with a sub query, highlight the zeroes...
$sql = 'SELECT a.*,
(SELECT COUNT(*) FROM orders WHERE animal_ID = a.animal_ID)
FROM animals a';

How to dynamically split result into nested table

Hi I'm having trouble trying to split my db object result into the correct table for display.
Basically I'm trying to acheive this
<div class="panel"> $row->skill_group
<table class="collapse">
<thead>
<tr>
<th>$row->name</th>
</tr>
</thead>
<tbody>
<tr>
<td>$row->skill_name</td>
</tr>
<tr>
<td> $row->competency_name </td>
</tbody>
</table>
</div>
There can be many unique skill_groups and many skill_names but each person and their competency should only be displayed once for each skill.
My mysql returns the correct data however I can't figure out how to loop over the result so that I can split it in between the table that is nested from the div container.
For example the mysql result would be
======================================================================
| skill_group_name | skill_name | competency_name | name
======================================================================
| PHP Frameworks | Codeigniter | Working Knowledge | User name1
| PHP Frameworks | CakePHP | No Knowledge | User name1
| Database | T-SQL | Working Knowledge | User name2
======================================================================
and I need to fit it into the html structure above.
The result I'm after is
======================================================================
PHP Frameworks
======================================================================
| Codeigniter | Working Knowledge | User name1
| CakePHP | No Knowledge | User name1
======================================================================
Database
======================================================================
| T-SQL | Working Knowledge | User name2
======================================================================
I have managed to get the grouping of the skill_group sorted with:
if($row->skill_group_name != $skill_group_name)
{
echo '<div class="panel-heading"><a href="#" data-target=".skill'.++$counter.'" data-toggle="collapse">' . $row->skill_group_name;
$skill_group_name = $row->skill_group_name;
echo '</a></div>';
}
But I'm missing how to write the loops for the data table itself, how do I do this?
As I commented, I think you should save to an array, then implode() the sections:
//Just some fake data
$array[] = array('PHP Frameworks','Codeigniter','Working Knowledge','User name1');
$array[] = array('PHP Frameworks','CakePHP','No Knowledge','User name1');
$array[] = array('Database','T-SQL','Working Knowledge','User name2');
// Loop through the data
foreach($array as $row) {
// Save the table column html to array
$section[$row[0]][] = '<tr><td>'.$row[1].'</td><td>'.$row[2].'</td><td>'.$row[3].'</td></tr>';
}
// Loop through the section array you made
foreach($section as $title => $all) {
// Wrap it
echo '<div>'.$title;
// Implode
echo '<table>'.implode(PHP_EOL,$all).'</table>';
echo '</div>';
}

How to store an array in a database column

I have been trying to store an array in a database column, but haven't succeeded yet.
I have been looking into php and sql for two weeks now, and I don't have a programming background. So please forgive my ignorance. I'm sure I've made lot's of mistakes.
Let me explain my situation further.
I created a web page so a admin can fill in some data concerning some game. This is after a login process.
When a admin arrives at this point, I'm not bothering with 'correct' input at the moment. I just want to get the data in the db.
I created a table called games with mysql, and filled it with data I have so far:
+--------+--------+-----+-----+------+------+------+
|game_id |round_id|team1|team2|score1|score2|result|
+--------+--------+-----+-----+------+------+------+
| 1 | 1 | A | B | NULL | NULL | NULL |
| 2 | 1 | C | D | NULL | NULL | NULL |
| 3 | 1 | E | F | NULL | NULL | NULL |
| 4 | 2 | E | C | NULL | NULL | NULL |
| 5 | 2 | A | D | NULL | NULL | NULL |
| 6 | 2 | F | B | NULL | NULL | NULL |
+--------+--------+-----+-----+------+------+------+
Then I created a form in a php page which uses data from this table to display the teams per game.
The admin uses this form to fill in the game score and results. See the form below.This form is also needed to store values in the above table.
input.php
<?php
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$query="SELECT games_team1, games_team2 FROM games WHERE round_id = 1";
$result = mysql_query($query);
?>
<form action="someform.php" method="post">
<table id="inputgameresults">
<tbody>
<tr>
<th>Home</th>
<th>Score 1</th>
<th>-</th>
<th>Score 2</th>
<th>Away</th>
<th>Result</th>
</tr>
<?php while($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['team1']; ?></td>
<td><input type="text" name="score1[]"/></td>
<td> - </td>
<td><input type="text" name="score2[]"/></td>
<td><?php echo $row['team2']; ?></td>
<td><input type="text" name="result[]"/></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td</td>
<td</td>
<td</td>
<td</td>
<td><input type="submit" value="Submit"/></td>
</tr>
</tbody>
</table>
</form>
This results into 3 rows in the table in the form above. This table displays the teams for each match and input fields for the scores and the result.
A header, three rows with the games, input fields behind the games, and a sumbit button below.
A admin can submit the input with the form below. I added the echo so I can check if the output is correct. The echo works like a charm.
This is where things don't go the way I'd like them to go. The sql query I used is stated in the form. It fails miserably, probably because of my lack of knowledge.
form1.php
<?php
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sc1 = $_POST['score1'];
$sc2 = $_POST['score2'];
$res = $_POST['result'];
foreach($sc1 as $value) {
echo "score team 1 $value<br/>";
mysql_query("UPDATE games SET game_score1 = '".$value[]"' WHERE games_id = [1,2,3] ");
}
foreach($sc2 as $value) {
echo "score team 2$value<br/>";
}
foreach($res as $value) {
echo "res $value<br/>";
}
?>
I'm sure there are a lot things besides the main issue I can improve, or do more efficient. Although this is not my main focus at the moment, any help is welcome :D
The main focus now is to get the input from the form in the right place in the db. The data from the form needs to be send to the database in columns score1, score2 and result for a game.
Any help or advice is most welcome!
a simple way to store a whole array in a database column is to encode the array to json ... in the example, $sc1 is the array you want to store
mysql_query("UPDATE games SET game_score1 = '". json_encode($sc1) . "' WHERE
games_id = [1,2,3,4,5,6,7,8,9] ");
then when you get your array from the colomnu, it is still on json format, so you need to do this
$array = json_decode($result);
sometimes your array contains elemens with '' all you need to do is to use
addslashes(json_encode($sc1))
instead of using
json_encode($sc1);
It looks like you just need the syntax to insert a new row to mysql. If game_id increments automatically, you don't have to specify it in the statment:
INSERT games (round_id, team1, team2, score1, score2, result)
VALUES (3, 4, 6, 18, 1, 0, 1);
In advance, thank you guys for the possible solutions to my problem. I'm definately gonna look into them.
A friend of mine told me that I should use a 'key' so that it would be able to loop in the database. Kinda like when the data was retreived from the database. This seemed to work.
I used a $key++ so it would start at the first games_id.
foreach($sc1 as $key => $value) {
$key++;
mysql_query("UPDATE games SET game_score1 = '".$value[]"' WHERE games_id = '".$key."' ");
}
My next challenge is to retreive per round_id and also store per round_id.
And to make it more interesting, the current date is gonna point to the current round_id :D

SQL Results in table cells grouped by date in <TR>

I have a table in SQL Server with this fields and values.
ID_Employee | ID_Company | Date | Concept | Hours
--------------------------------------------------------
1 | 1 | 14/03/2013 | 1 | 8
1 | 1 | 14/03/2013 | 2 | 0
1 | 1 | 14/03/2013 | 3 | 3
1 | 1 | 14/03/2013 | 4 | 1
1 | 1 | 16/03/2013 | 1 | 5
1 | 1 | 16/03/2013 | 2 | 2
1 | 1 | 16/03/2013 | 3 | 0
1 | 1 | 16/03/2013 | 4 | 0
What I need is to display the values where ID_Employee=1 and ID_Company=1 ​​in a HTML table grouping the rows by date, and ordering the hours in his column as the value of their concept.
Date | Concept_1 | Concept_2 | Concept_3 | Concept_4 |
------------------------------------------------------------
14/03/2013 | 8 hours | 0 hours | 3 hours | 1 hour |
16/03/2013 | 5 hours | 2 hours | 0 hours | 0 hours |
I don't know how to do the query or what statement (while,for,foreach) to use in php to create 1 row (<tr>) for each different date, containing a single cell (<td>) for each concept and hour.
The html should look like this:
<tr id="14/03/2013">
<td class="concept_1">8 hours</td>
<td class="concept_2">0 hours</td>
<td class="concept_3">3 hours</td>
<td class="concept_4">1 hour</td>
</tr>
<tr id="16/03/2013">
<td class="concept_1">5 hours</td>
<td class="concept_2">2 hours</td>
<td class="concept_3">0 hours</td>
<td class="concept_4">0 hour</td>
</tr>
It may be easy, but now I'm a bit confused and I can't find the solution.
You're looking for a PIVOT operator wich allows you do desired stuff. You can use following query:
select
pvt.date,
[1] AS concept_1,
[2] AS concept_2,
[3] AS concept_3,
[4] AS concept_4
from
(
SELECT
date,
hours,
concept
FROM table1
) p
PIVOT
(
AVG(hours)
FOR concept IN
([1], [2], [3], [4])
) as pvt
Providing SqlFiddle as well.
Check this:
declare #tableHTML nvarchar(MAX);
set #tableHTML = CAST((
select
1 AS Tag,
NULL AS Parent,
pvt.[date] AS 'tr!1!id',
ltrim(rtrim(str([1])))+ ' hours' AS 'tr!1!td class="concept_1"!Element',
ltrim(rtrim(str([2])))+ ' hours' AS 'tr!1!td class="concept_2"!Element',
ltrim(rtrim(str([3])))+ ' hours' AS 'tr!1!td class="concept_3"!Element',
ltrim(rtrim(str([4])))+ ' hours' AS 'tr!1!td class="concept_4"!Element'
from
(
SELECT
date,
hours,
concept
FROM table1
) p
PIVOT
(
AVG(hours)
FOR concept IN
([1], [2], [3], [4])
) as pvt
FOR XML EXPLICIT) AS NVARCHAR(MAX));
PRINT REPLACE(REPLACE(REPLACE(REPLACE(#tableHTML, '</td class="concept_1">', '</td>'), '</td class="concept_2">', '</td>'), '</td class="concept_3">', '</td>'), '</td class="concept_4">', '</td>');
Ouput:
<tr id="2013-03-14">
<td class="concept_1">8 hours</td>
<td class="concept_2">0 hours</td>
<td class="concept_3">3 hours</td>
<td class="concept_4">1 hours</td>
</tr>
<tr id="2013-03-16">
<td class="concept_1">5 hours</td>
<td class="concept_2">2 hours</td>
<td class="concept_3">0 hours</td>
<td class="concept_4">0 hours</td>
</tr>
I used the pivot proposed by #Sergio - thanks a lot (+1)

Categories