I need to select multi-column information from a table and display it in another table which has just two columns. The scenario is this:
First Table(multi-columns):
MATH111 MATH112 MATH113 MATH114
67 89 54 23
I want to display it in this(two column table):
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
I am conversant enough in php to obtain and display the information from the multicolumn table:
"select * from xx where id=''";
echo "<table border='1'>
<tr>
<th>MATH111</th>
<th>MATH112</th>
<th>MATH113</th>
<th>MATH114</th>
</tr>";
while($row=(mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['MATH111'] . "</td>";
echo "<td>" . $row['MATH112'] . "</td>";
echo "<td>" . $row['MATH113'] . "</td>";
echo "<td>" . $row['MATH114'] . "</td>";
echo "</tr>";
}
echo "</table>";
The table i want to display it in will be :
echo '<table><tr><th>Course</th><th>Score</th></tr>';
What i need is help on code to display it as i have outlined above, which is in this format:
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
Please let me add that the values for the "course" column
is based on the table being selected.So i can not hard code it.
re-write your SQL query to something like this
select 'MATH111' Course, MATH111 Score from xx where id=something
union
select 'MATH112' Course, MATH112 Score from xx where id=something
union
select 'MATH113' Course, MATH113 Score from xx where id=something
union
select 'MATH114' Course, MATH114 Score from xx where id=something
and that should take care of the rest
Why not just:
echo '<tr><td>MATH111</td><td>', $row['MATH111'], '</td></tr>',
'<tr><td>MATH112</td><td>', $row['MATH112'], '</td></tr>' [ ... ];
Related
Hello and thanks for your time!
I'm trying to pull data from a mysql table using PHP, and am fairly new to both mysql and php.
I have a table built on racing information, where the following race results are recorded:
Date, Type, WL1, L1, L2, etc up to L15
WL1 column contains a value from L1 to L15, and stands for "winning lane 1" - basically what lane the car was in that came in first place.
Columns L1 to L15 contain racers' names.
Given the above, I'm trying to display the following in a table:
Date, Type, WL1 and the value of L1 through L15 as "Winner" when the data in columns L1 through L15 match the entry in column WL1.
Example:
-----------------------------------------
Date | Type | WL1| Winner
-----------------------------------------
May 5 | Rally | 13 | John
May 7 | Stock | 2 | Stewart
May 15 | Touring | 7 | Eddy
I'm able to achieve results like this using the following code:
$sql = "SELECT * FROM races";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>Type</th>";
echo "<th>WL1</th>";
echo "<th>Winner</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['WL1'] . "</td>";
if($row['WL1'] == 'L1'){
echo "<td>" . $row['L1'] . "</td>";
}elseif($row['WL1'] == 'L2'){
echo "<td>" . $row['L2'] . "</td>";
That being said, I'm thinking there has to be an easier way than iterating through 15 values with if/elseif statements. I've tried both "while" and "for" loops with incremental values, but I only get empty results or errors, most likely due to my inexperience. I've also reproduced the above using a complex CASE statement in MySQL, but again, I'm trying to reduce lines of code.
If someone could suggest a more concise method of pulling this data, either through a MySQL statement or PHP code, I'd appreciate it. Even better if you can point to some good online examples/tutorials - the whole "teach a man to fish" thing...
Your data structure is what is causing your problem; you should really have a table that links racers and lanes for each race and then JOIN to that. Having said that, you can simplify your existing code by noting that the value in $row['WL1'] is the column name of the winner, so you can simply replace your if structure with:
echo "<td>" . $row[$row['WL1']] . "</td>";
I've created a sample data structure here with races, lanes and racers tables. To query from this structure you would do something like:
SELECT DATE_FORMAT(r.Date, '%b %e') AS Date, r.Type, r.Winner AS WL1, rs.Name AS Winner
FROM races r
JOIN lanes l ON l.Race = r.id AND l.Lane = r.Winner
JOIN racers rs ON rs.id = l.Racer
Which for my sample data gives:
Date Type WL1 Winner
May 5 Rally 13 John
May 7 Stock 2 Stewart
May 15 Touring 7 Eddy
I'm running following query and displaying the results with php:
<?php
$con=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select
p.id,
p.date,
(select
t.title
from
table02 t
where
p.family = t.family) title,
(select
a.author
from
table03 a
where
p.family = a.family) author,
(select
n.note
from
table04 n
where
p.family = n.family) note,
from
table01 p
where
p.family in (48766 , 276197, 265242, 334879)";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>ID</th><th>Date</th><th>Title</th><th>Author</th><th>Note</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['note'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
So we would have a table like that:
ID - DATE ---------- TITLE -------------------------- AUTHOR --- NOTE
01 - 07/01/2013 - Thank you for your help - Some Guy - 8.3
07 - 07/03/2013 - You are welcome - Mr. Nice - 7.6
11 - 09/27/2013 - I hope you enjoy us - J. Growth - 8.9
etc.
This works fine if I find only one result for each column.
But the problem is that we can have more than one author for some "songs" as we will see below (when ID = 13).
So when we run the query we receive a message that author in table03 has more than one value and nothing is displayed at all.
How could I manage to have a table that gives me the results of this column with multiple authors too?
ID - DATE ---------- TITLE -------------------------- AUTHOR --- NOTE
01 - 07/01/2013 - Thank you for your help - Some Guy - 8.3
07 - 07/03/2013 - You are welcome - Mr. Nice - 7.6
11 - 09/27/2013 - I hope you enjoy us - J. Growth - 8.9
13 - 11/14/2013 - Houston, we have a problem - B. Lee & T. Hanks - 6.4
17 - 12/09/2013 - Now we have only one - P. Neuer - 7.1
Many thanks!
One option is to use MySQL's GROUP_CONCAT() in your author subquery.
For your case, your subquery might look like:
(select
GROUP_CONCAT(a.author)
from
table03 a
where
p.family = a.family) authors
Note:
The default separator between values in a group is comma (“,”)
Use LIMIT (and ORDER BY optionally) clausule in subqueries or GROUP_CONCAT() grouping function in subqueries too.
LIMIT => https://dev.mysql.com/doc/refman/5.5/en/select.html
GROUP_CONCAT => http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat
I have three tables:
Campaign
Customer
Office
As in the Campaign table the customer_id the same is as ID in the Customer table,
then he must show the customer_name also in Customer table.
Now he is showing the customer id with: $row->customer_id
I now have the following
if ($result = $mysqli->query("SELECT * FROM campaign ORDER BY campaign_id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->campaign_name . "</td>";
echo "<td>" . $row->customer_id . "</td>";
echo "<td>" . $row->office_id . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
Thanks!
It's not incredibly clear what you're asking, but maybe try this query?
SELECT c.*, cust.* FROM campaign c, customer cust WHERE c.customer_id=cust.customer_id ORDER BY c.campaign_id
Using explicit (instead of implicit) JOINs, this query can be written as this:
SELECT c.*, cust.* FROM campaign c JOIN customers cust ON c.customer_id=cust.customer_id
As a note, I usually prefer to call out column names explicitly in queries, but since you didn't post your schema, I wasn't sure the names of your columns. Possibly something like this:
SELECT c.campaign_name, cust.customer_id FROM campaign c JOIN customers cust ON c.customer_id=cust.customer_id
I have two databases. The first contains a list of upload doucuments and who they are for when uploaded and expiry date. This is stored as numbers representing each grade of person using implode. so column name 'foagrade' has 10,11,12 in row one. The second database is a list of grades and there ID's eg 10 = manager, 11 = HR etc. I am using left join and a basic html table to display a query.
$result = mysqli_query($con,
"SELECT UPLOAD.id, UPLOAD.title, UPLOAD.faoGrade,UPLOAD.faoLocation, UPLOAD.date, UPLOAD.expiry, GRADE.ID, GRADE.GRADE as GR, GRADE.id
FROM UPLOAD
LEFT JOIN GRADE ON
UPLOAD.faoGrade=GRADE.ID
where owner=$user");
echo "<table id='previous'>
<tr>
<th>Title/Document name:</th>
<th>For attention of Grade</th>
<th>For Location</th>
<th>date</th>
<th>Date expires</th>
<th>Delete this briefing</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id=$row['id'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['GR'] . "</td>";
echo "<td>" . $row['foaLocation']."</td>";
echo "<td>" . date("D, d M Y ",($row['date']));
echo "<td>" . date("D, d M Y ",($row['expiry']));
echo "<td>delete</td> ";
echo "</tr>";
}
echo "</table>";
The query shows all the details but only shows the first grade stored in the column faograde (MANAGER) not 11 or 12.
How would I explode or split this? So for each number in this column a grade name is shown. i will also have to do the same for column 'faoLocation', but working on one issue at a time.
These tables should be normalized, but with what you have already you can do something like this:
SELECT UPLOAD.id as uploadID,
UPLOAD.title,
UPLOAD.faoLocation,
UPLOAD.date,
UPLOAD.expiry,
GRADE.ID as gradeID1,
GRADE.GRADE as GR,
GRADE.id as gradeID2 FROM UPLOAD, GRADE
WHERE
FIND_IN_SET(gradeID2,UPLOAD.faoGrade)
and UPLOAD.owner=$user;
Update
Here's the SQL Fiddle.
It is a bit confusing that there are multiple columns called id & ID. It's best to give them intuitive names like grade_ID or user_ID. From a normalization standpoint you could be using a relational table instead of the comma separated column (depending on how this data enters the database). Here's a video about normalization.
I'm now hopeless about this problem and that's why I'm here. I'm kind of a starter in PHP and mysql programming. I searched for a solution on the web but I wasn't succeeded. I'm working on a project what is a car repairing administration system. This is a part of it that I need.
I have two tables:
repairs (j_id /this is the primary key/, rendszam, javitas, megjegyzes, datum)
and
pictures (kep_id /this is the primary key/, j_id /it's from repairs j_id/, kepnev)
I need to display a result in a table, where I can see that which of the repairs have a picture in the pictures table, but without redundancy, so I don't want multiple repair rows that are similar to eachother, but one repair row with multiple picture columns after it.
What I have done already:
<?php
$sql = "SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>" . $row[$i] . "</td>";
echo "<td>" . $row[$i + 1] . "</td>";
echo "<td>" . $row[$i + 2] . "</td>";
echo "<td>" . $row[$i + 3] . "</td>";
echo "<td>" . $row[$i + 4] . "</td>";
echo "<td>" . $row[$i + 5] . "</td>";
}
?>
This one displays information like this:
repair id | picture
1 | fdgdfg.jpg
1 | fgdfg.jpg
1 | fghh.jpg
25 | dfg.jpg
25 | jkjk.jpg
But I don't want to have multiple repair rows but one repair row with multiple pictures after it.
I tried this:
$sql2="SELECT DISTINCT $tbl_name2.j_id, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result2=mysql_query($sql2) or die(mysql_error());
$sql="SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id GROUP BY j_id;
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row[$i]."</td>";
echo "<td>".$row[$i+1]."</td>";
echo "<td>".$row[$i+2]."</td>";
echo "<td>".$row[$i+3]."</td>";
echo "<td>".$row[$i+4]."</td>";
echo "<td>".$row[$i+5]."</td>";
while($sor=mysql_fetch_array($result2))
{
if($sor['j_id']==$row[$i]){
echo "<td><a href=kepek/".$sor['kepnev']." target=_blank>".$sor['kepnev']."</a></td>";
}
}
}
The sql statement differs in the group by in $sql, but this one displays the results like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 |
So it's just not continuing after the first repair id, not showing the repair pictures for repair id 25.
I don't know, how can I get it right. I need a table which displays information like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 | dfg.jpg | jkjk.jpg
Could you help me out in this?
This sounds fairly straightforward, looks like you're off to a good start.
I've done something similar in the past, and I just used a variable to keep track of the current ID being displayed and the previously displayed ID. Here's a quick code sample to outline what I mean by that.
while($row=mysql_fetch_array($result))
{
$currentID=$row[$i];
if ($currentID != $lastID) {
echo "</tr><tr><td>".$row[$i]."</td>";
} else {
echo "<td>".$row[$i]."</td>";
}
$lastID=$row[$i];
}
So, the idea is to keep track of what the last ID was. In the next row, if the ID is the same, just output <td>"variable goes here"</td> to keep it on the same column. If the ID changed, end the row and create a new row using </tr><tr>". Feel free to comment if you need more help.