Please i need your help with how to optimize my query. I want to fetch 10 different rows data from the same table.
Here is one of the queries
$query1 = mysql_query("SELECT m.Course_code AS 'Course', m.score, m.grade
FROM maintable AS m
INNER JOIN students AS s ON
m.matric_no = s.matric_no
INNER JOIN Courses AS c ON
m.Course_code = c.Course_code
WHERE m.matric_no = '".$matric_no."'
AND m.semester_name = 'hamattarn'
AND m.level = '100' ") or die (mysql_error());
$number_cols1 = mysql_num_fields($query1) ;
this part of the query is changing for all the queries
AND m.semester_name = 'hamattarn'
AND m.level = '100' "
AND m.semester_name = 'rain'
AND m.level = '100' "
AND m.semester_name = 'hamattarn'
AND m.level = '200' "
AND m.semester_name = 'rain'
AND m.level = '200' "
AND m.semester_name = 'hamattarn'
AND m.level = '300' "
AND m.semester_name = 'rain'
AND m.level = '300' "
AND m.semester_name = 'hamattarn'
AND m.level = '400' "
AND m.semester_name = 'rain'
AND m.level = '400' "
AND m.semester_name = 'hamattarn'
AND m.level = '500' "
AND m.semester_name = 'rain'
AND m.level = '500' "
Here’s a picture of what i’ve being able to achieve with the query. After optimization i still want it to look this way.
< href=”http://i.imgur.com/IGEj2.png”>Sample Picture
thanks for you time and patience.
UPDATE — here is how i was displaying the row tables from each of the 10 queries before the optimization.. Please how can i achieve this table type reult with the new optimized query?
echo "<table class=\"altrowstable\" id = " bgcolor = gold >\n";
echo "<tr align=center>\n";
for ($i=0; $i<$number_cols10; $i++)
{
echo "<th>" . mysql_field_name($query10, $i). "</th>";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($query10))
{
echo "<tr align=center>\n";
for ($i=0; $i<$number_cols10; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{echo "NULL";}
else
{
echo "<b>".$row[$i]."</b>";
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo"</span>" ;
thanks
Why not something like this:
As the results will be mixed, you should get the results in an expectable order, for example:
This will allow you to split the results in PHP, something like this is common:
You could open an HTML table before the for loop.
When the semester or level changes, you could close the previous table and open a new one, add a title for the rest of the rows of the same semester/level, etc, etc…
You could close an HTML table after the for loop.
It’s a little tricky but eventually it will get you there.