I want to show table like :
| | qty | S rank | A rank | B rank | C rank |
+--------------------------------------------------------+
| TODAY | 12 | 0 | 0 | 0 | 0 | //THIS CURRENT DATA
+--------------------------------------------------------+
|MONTHLY TOTAL| 200 | 1 | 0 | 0 | 0 | //THIS MONTHLY DATA
+--------------------------------------------------------+
The table above will show after echoing mysql result use PHP. How to combine tWo case become one table after echo? using double $sql or something else use PHP ?
So far, I'm success during echo current data, but for Monthly data i've become confuse how to deciding which way that should I try to do.
CURRENT DATA script:
..........
$sql = " SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A' AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' GROUP BY Range_sampling";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
echo "<table border='1' width='500'>";
echo "<caption>INSPECTION REPORT</caption>";
echo "<thead><tr>";
echo "<td colspan='3'></td>";
echo "<th>n</th><th>S</th><th>A</th><th>B</th><th>C</th>";
echo "</tr></thead>";
echo "<tbody>";
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>TODAY</th><td>";
echo $row['n'];
echo "</td><td>";
echo $row['S'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['B'];
echo "</td><td>";
echo $row['C'];
echo "</td></tr>";
}
echo "</tbody></table>";
mysql_close($dbc);
?>
This query for count MONTHLY data :
SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Inspection_datetime <=
( SELECT DATE(MAX(Inspection_datetime)) FROM inspection_report
WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A'
AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' )
AND MONTH(Inspection_datetime)=MONTH(CURRENT_DATE) AND Line ='FA 01'
could I get the result like above table ?
From your suggested structure, it contain 3 rows and 6 fields. Now, let's get back to Basic HTML 101.
To create a table, you need:
<table></table>
To create row and columns, you need:
<tr><!-- start of row -->
<td><!-- start of field 1 -->
{table's field 1 content}
</td><!-- end of field 1 -->
<td><!-- start of field 2 -->
{table's field 2 content}
</td><!-- end of field 2 -->
</tr><!-- end of row -->
For more information about how to create HTML table.
Now, back to your case. You already create the first and second rows and by this, you should already understand how to do it.
So, when you need to append another table row, all you have to do is insert the table row code.
Just before:
echo "</tbody></table>";
You do the second (monthly) query and echo it's value, eg:
$sql = "{your sql query}";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>MONTHLY TOTAL</th><td>";
echo $row['{field_name}'];
echo "</td><td>";
.
.
.
echo "</td></tr>";
}
echo "</tbody></table>";
Try it like below code :-
$sql = " SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A' AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' GROUP BY Range_sampling";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
echo "<table border='1' width='500'>";
echo "<caption>INSPECTION REPORT</caption>";
echo "<thead><tr>";
echo "<td colspan='3'></td>";
echo "<th>n</th><th>S</th><th>A</th><th>B</th><th>C</th>";
echo "</tr></thead>";
echo "<tbody>";
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>TODAY</th><td>";
echo $row['n'];
echo "</td><td>";
echo $row['S'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['B'];
echo "</td><td>";
echo $row['C'];
echo "</td></tr>";
}
$sql2 = "SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C FROM inspection_report WHERE Inspection_datetime <=
( SELECT DATE(MAX(Inspection_datetime)) FROM inspection_report
WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A'
AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' )
AND MONTH(Inspection_datetime)=MONTH(CURRENT_DATE) AND Line ='FA 01'";
$result_sql2 =mysql_query($sql2) or die(_ERROR26.": ".mysql_error());
while($rw=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>SECOND</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>MONTHLY</th><td>";
echo $rw['n'];
echo "</td><td>";
echo $rw['S'];
echo "</td><td>";
echo $rw['A'];
echo "</td><td>";
echo $rw['B'];
echo "</td><td>";
echo $rw['C'];
echo "</td></tr>";
}
echo "</tbody></table>";
mysql_close($dbc);
?>
Related
I am currently extracting some timetable information from my database.
I am facing a problem that I need the rows in cols and cols in rows.
The query is as:
SELECT `courses`.`course_id`,`faculty`.`uName`,`rooms`.`room_name`,`slot_allocs`.`slot_num`
FROM `faculty`
LEFT JOIN `courses` ON `courses`.`fac_id` = `faculty`.`uName`
LEFT JOIN `depts` ON `faculty`.`dept_code` = `depts`.`dept_code`
LEFT JOIN `slot_allocs` ON `slot_allocs`.`course_id` = `courses`.`course_id`
LEFT JOIN `rooms` ON `slot_allocs`.`room` = `rooms`.`room_name`
LEFT JOIN `slots` ON `slot_allocs`.`table_name` = `slots`.`table_name` AND `slot_allocs`.`day` = `slots`.`day` AND `slot_allocs`.`slot_num` = `slots`.`slot_num`
WHERE `slot_allocs`.`day` = 1 AND `depts`.`dept_code` = 100
ORDER BY `slots`.`slot_num` ASC
After I execute my PHP file in which this query executes it's in a sequential retrieve and show, I would like to have the output -- > one after the another
> > **I want to align in the sequential manner of the period per day like this:
> >|---------------------------------------------
> >day 1| period 1 |period 2 |period 3 |period 4|
> >|----|----------|---------|---------|--------|
> >day 2| period 1 |period 2 |period 3 |period 4|
> >|---------------------------------------------
And this is my php code:
$sql = file_get_contents("eboard.sql");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<table>";
echo "<tr>";
echo "<td>";
echo " Course: ID ". $row["course_id"]." : ";
echo " Prof.:". $row["uName"]." : ";
echo " Room :".$row["room_name"]." : ";
echo " period".$row["slot_num"]."";
echo "</td>";
echo "</tr>";
echo "</table>";
}
} else
echo "0 results";
$conn->close();
The output i get : ---
Please help !!
Move the <table> out of the loop:
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>";
echo " Course: ID ". $row["course_id"]." : ";
echo " Prof.:". $row["uName"]." : ";
echo " Room :".$row["room_name"]." : ";
echo " period".$row["slot_num"]."";
echo "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Ok, So this code should do it. You should read up on how tables works. W3Schools have a simple page about it. I hope this version is what you want. It is not a full solution but it shows you how to use tables.
if ($result->num_rows > 0) {
echo "<table>";
echo " <tr>";
echo " <th>Course</th>";
echo " <th>Prof.</th>";
echo " <th>Room</th>";
echo " <th>Period</th>";
echo " </tr>";
while($row = $result->fetch_assoc()) {
echo " <tr>";
echo " <td>Course: ID ". $row["course_id"]."</td>";
echo " <td>Prof.:". $row["uName"]."</td>";
echo " <td>Room :".$row["room_name"]."</td>";
echo " <td>period".$row["slot_num"]."</td>";
echo " </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
I have 2 tables
1)students
students table looks like
stu_slno stu_gr stu_name
1 1 ABCDESDFDGFJ
2 3 KJJJHJILKJBJB
3 5 HAHAHAHAHKJHKJH
4 1 BBJHBAHJBHJBAJHK
2)groups
groups table looks like
sl_no pg_groups
1 01-M.A HISTORY
3 03-M.A SOCIOLOGY
5 04-M.A ECONOMICS
i have inserted data into students with groups serial numbers
when i am retrieving data from students i will get the groups serial number but what i want is to group names corresponding to the serial number
my code is of retrieving
<?PHP
$sql="SELECT * FROM students";
if ($us=$conn->query($sql)){;
if ($us->num_rows > 0) {
echo '<table border="2">';
echo "<tr>";
echo "<th>Slno</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = $us->fetch_assoc()) {
echo "<tr>";
echo "<td>" .$i++. "</td>";
echo "<td>" .$row['stu_name']. "</td>";
echo "<td>" .$row['stu_gr']. "</td>";
echo "</table>";
}
}
}
?>
use join in query like this:
$sql="SELECT stu_slno, pg_groups, stu_name
FROM students s
INNER JOIN groups g ON g.pg_groups = s.stu_gr";
$sql = "SELECT s.*, g.pg_groups from students AS s LEFT JOIN groups as g ON g.sl_no = s.stu_gr";
TO display in the HTML table, use the below code
echo "<td>" .$row['pg_groups']. "</td>";
I am making the code of PHP-MySQL. But I gain have the 2 and more as the result of the left join in the MySQL. So I want to union the results in a cell of the result table as the captured picture.
Here is my real captured picture in my computer.
My php code is as like below.
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<table border='1' style='background:#dddddd;border-color:green;'>";
echo "<h2><p >진료과 (Subject) : ".$row['subject']."</p></h2>";
echo "<tr>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['code']."'>Code</th>";
echo "<th ><a href='".$row['ds_url']."'>"."한국병명</a></th>";
echo "<th ><a href='".$row['ds_url']."'>"."Disease name(En.)</a></th>";
echo "<th >"."<form action='search1.php' method='get'>"."<button type='submit'
name='code' value='".$row['family']."'>"." Family History</button> </th>";
echo "</tr>";
echo "<tr>";
echo "<td >" . $row['code'] . "</td>";
echo "<td >" .$row['disease_co']."</td>";
echo "<td >" .$row['disease_en']."</td>";
echo "<td >" .$row['family']."</td>";
echo "</tr>";
---
echo "<tr>";
echo "<th>소아과</th>";
echo "<th>내과</th>";
echo "<th>산과</th>";
echo "<th>정형외과</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row['ped_content']."</td>";
echo "<td>".$row['im_content']."</td>";
echo "<td>".$row['ob_content']."</td>";
echo "<td>".$row['os_content']."</td>";
echo "</tr>";
----
echo "</table>";
}
The While phrase in the above php code is just 1, but the number of the result table depend on the number of im_content.
So, I want to union the result of im_content in a cell as the above captured picture.
But, I am short of ability.
Please give me a piece of advice.
Thank you for your concern.
My sql query sentence is as like below.
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, im.im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"
Thank you, Barmar!
I'm not totally sure I understand the question, but I think this is what you want:
SELECT code_en.code, code_co.disease_co, code_en.disease_en , hx.family,
hx.personal, note.note, inclusion.inclusion, exclusion.exclusion, ds.ds_content,
------
subject.icd_category, subject.group_code, GROUP_CONCAT(im.im_content) AS im_content
FROM code_en
LEFT JOIN subject ON code_en.code = subject.code
LEFT JOIN note ON code_en.code = note.code
-------
left join im on code_en.code = im.code
WHERE code_en.code = '".$code."'"
I'm trying to get all the records in my database and it was successful, however I can only get 1 id value. I need to get all the id in my database and call a javascript function. In my code I can only get the id of the last record. How can I get them all?
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";
/*The start of chaos */
echo "<form action='' method='post' onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr; //This should change and increment the value of id, isn't it?
echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
What's happening here is that it will display records from the database: $row['header'], $row['date'], $row['desc'] then a button which says Read More in a loop. What I would like to happen is to call a function after clicking 'read more', depending on the id.
For example,
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 3
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 2
___________________________
| This is the header |
| Date |
| Description |
| Read More Button | //this button should display $id = 1
___________________________
Is that possible? or am I missing something. My code only displays $id=1 for all the buttons. I'm sorry for this.
you may try like this
make the id field name as array like more[] and bring the form out side loop as shown
no need of for loop there to get the ids u already using while loop
bring submit button also outside loop
if u want to show the details of that post then why again u using form u can simply link it to the details page n can pass the id no need to submit it as a form .
check the code below
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
//echo "<form action='' method='post' onSubmit='ReadMore()'>";
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0;
$message= $row['desc'];
$post = substr($message,$position,300);
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";
/*The start of chaos */
//for($ctr=1;$ctr<=3;$ctr++){
//$id=$ctr; //This should change and increment the value of id, isn't it?
//echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
//}
echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";
</td></tr>";
echo "</tr>";
}
echo "</table>";
//echo "</form>";
mysql_close($con);
?>
I am making a soccer site. I have the following table structure:
season1 (name of the table)
id | player1 | player2 | score
I want to put the scores into a html table. This table is only for player called nickname1 vs every other player. I have this:
$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());
while($row = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo "";
echo "</td><td>";
echo $row['score2'];
echo "</td><td>";
echo $row['score3'];
echo "</td><td>";
echo $row['score4'];
echo "</td><td>";
echo $row['score5'];
echo "</td></tr>";
}
echo "</table> <br>";
Instead of $row['score2'] I need the score between nickname1 vs
nickname2
Instead of $row['score3'] I need the score between nickname1 vs
nickname3
Instead of $row['score4'] I need the score between nickname1 vs
nickname4
Instead of $row['score5'] I need the score between nickname1 vs
nickname5
I tried SELECT * FROM season1 WHERE player1='nickname1' AND player2='nickname2' but then it will show only player1 vs player2 in the table. I want to show all 4 players vs player1 in the same table.
Assuming score1 it's player 1 score:
$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());
while($row = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo "";
echo "</td><td>";
echo $row['player1']." vs ".$row['player2'].", with a score of: ".$row['score'];
echo "</td></tr>";
}
echo "</table> <br>";
I hope it helps.
Saludos ;)
You need to focus your loop only on making one row:
echo "<table><tr>";
while($row = mysql_fetch_array( $query )) {
echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";
This will ONLY output the scores of the players. You can modify it to have nickname headers and so forth.
Take note that the $row array resembles your table structure. $row['score'] will give you the scores of the players where as since score1 is not a column in the table, it does not exist in $row.
Possible design:
echo "<table><tr><th>Player 1</th><th>Player 2</th><th>Score</th></tr><tr>";
while($row = mysql_fetch_array( $query )) {
echo "<td>{$row['player1']}</td>";
echo "<td>{$row['player2']}</td>";
echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";
will output (depending on ID's in database)
Player 1 | Player 2 | Score
p1 p2 0
p1 p3 0
NOTICE: MySQL_* has been deprecated in PHP 5.5. Use MySQLi or PDO instead