Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Awesome Programers there!
	I need your help please. I have problem in my codes, I don't know how I could make the things I want to be in my tables results.
	
	The result from mysql it should be like this: (tvalue means total value)
        
  th_name1      | th_name2    | th_name3    | th_name4    | th_name_5   | th_name6    
 -------------------------------------------------------------------------------------
 th_scope_row1 |td_tvalue2.1 |td_tvalue3.1 |td_tvalue4.1 |td_tvalue5.1 |td_tvalue6.1 
 th_scope_row2 |td_tvalue2.2 |td_tvalue3.2 |td_tvalue4.2 |td_tvalue5.2 |td_tvalue6.2 
 th_scope_row3 |td_tvalue2.3 |td_tvalue3.3 |td_tvalue4.3 |td_tvalue5.3 |td_tvalue6.3 


Example:
      In mysql or in database:
(Grade 5)
ID | Grade level | Math_Subj | English_Subj | History_Subj | P.E_Subj |Science_Subj
-------------------------------------------------------------------------------------
1  | Grade 5     |  16       |   10         |   5          |   8      | 3
2  | Grade 5     |   2       |    6         |   8          |   3      | 5

(Grade 4)
ID | Grade level | Math_Subj | English_Subj | History_Subj | P.E_Subj |Science_Subj
-------------------------------------------------------------------------------------
1  |  Grade 4     |   8       |   11         |   8          |   5      | 4
2  |  Grade 4     |   2       |    4         |   2          |   3      | 1

(Grade 3)
ID | Grade level | Math_Subj | English_Subj | History_Subj | P.E_Subj |Science_Subj
-------------------------------------------------------------------------------------
1  |  Grade 3     |   4       |    9         |   5          |   8      | 9
2  |  Grade 3     |   7       |    4         |   3          |   1      | 2


    Table result and shows in page:
      Grade level | Math_Subj | English_Subj | History_Subj | P.E_Subj |Science_Subj
      ------------------------------------------------------------------------------
      Grade 5     |  18       |  16          |  13          |  11      |  8
      Grade 4     |  10       |  15          |  10          |   8      |  5
      Grade 3     |  11       |  13          |   8          |   9      |  11


What I have tried:

<?php
$con=mysqli_connect("localhost","test","abdub","test_table");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT id,( Grade level +  Math_Subj + English_Subj + History_Subj + P.E_Subj ) AS total, Grade level, Math_Subj, English_Subj, History_Subj, P.E_Subj, FROM `Record_show");

echo "<table border='1'>
<tr>
    <th>ID</th>
    <th>Grade level</th>
    <th>Math_Subj</th>
    <th>English_Subj</th>
    <th>History_Subj</th>
    <th>P.E_Subj</th>
    <th>Science_Subj</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Grade_5'] . "</td>";
echo "<td>" . $row['Grade_4'] . "</td>";
echo "<td>" . $row['Grade_3'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);

?>
Posted
Updated 3-Nov-17 0:40am

1 solution

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Grade_5'] . "</td>";
echo "<td>" . $row['Grade_4'] . "</td>";
echo "<td>" . $row['Grade_3'] . "</td>";
echo "</tr>";
}

is trying to display a row with data elements $row['Grade_5'] (etc) that are not results return from your query.

The rows returned are $row['id'], $row['total'], . . ., $row['P.E_Sub']. For that matter, using a period in a column name is bad practice, at least, and if MS SQL, an error.

My impression is that you cut/pasted this block from somewhere as you don't seem to know how any of it works.


Here's the way to " make the things I want to be in my tables results": SQL Tutorial[^] and PHP 5 Tutorial[^]

 
Share this answer
 
v2
Comments
Jireh Capao 5-Nov-17 20:10pm    
Hmm, what I'm trying to say is I want to shows the total number in every column subjects like my example above. Because I need to provide table, pie graph, bar graph and line graph for that data. :)
W Balboos, GHB 6-Nov-17 6:04am    
ll of the things you want to do require you get the actual data. My answer still holds - you don't seem to know how to get the data from the query (assuming it doesn't fail). You need to know that and so I sent you a link to help you understand.
Jireh Capao 8-Nov-17 1:01am    
Okay thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900