0

I have written the following code, but somehow the headers are not matching the generated columns in the rendered table,, can someone give me a hint on how to improve it?

    <?php

$database =& JFactory::getDBO(); 

//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');

// Display quizzes
echo "</br>";
echo "Quizzes History for : " ;
echo "<b>";
echo $name;
echo "</b>";


echo "</br>";
echo "</br>";

$database->setQuery('SELECT distinct qui.title AS name,' .
        ' ( SELECT GROUP_CONCAT(profiles.title) 
              FROM #__jquarks_users_profiles AS users_profiles
              LEFT JOIN #__jquarks_profiles AS profiles ON users_profiles.profile_id = profiles.id
              WHERE users_profiles.user_id = sessionWho.user_id ) AS profile, ' .
        ' ( SELECT sum(score)  
              FROM #__jquarks_quizzes_answersessions
              WHERE quizsession_id = quizSession.id
              AND status <> -1 ) AS score,' .
        ' ( SELECT count(distinct(question_id))
              FROM #__jquarks_quizzes_answersessions 
              WHERE quizsession_id = quizSession.id ) AS maxScore,' .

            ' ( SELECT count(id)
              FROM #__jquarks_quizzes_answersessions 
              WHERE status=-1
              AND quizsession_id = quizSession.id ) AS evaluate,' .
' quizSession.finished_on,sessionWho.email' .     
        ' FROM #__jquarks_quizsession AS quizSession' .
        ' LEFT JOIN #__jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id' .
        ' LEFT JOIN #__jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id' .
        ' LEFT JOIN #__jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id' .
        ' LEFT JOIN #__jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id' .
        ' LEFT JOIN #__jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id' .
' LEFT JOIN #__jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id '.

' WHERE sessionWho.user_id =' .$id) ;

if (!$database->query()) { //write data and if error occurs alert
    echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}

//var_dump($database);
$tableStyle = "padding: 5px;border:1px"; 
$tdStyle = "padding:5px "; 

echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">'; 
echo "<tr> <th> Quiz Title </th><th> Score </th><th>Maximum Score </th><th> Unanswered </th> <th>Finished On </th></tr>"; 

$row = $database->loadRowList();
foreach($row as $valuearray)
{
echo '<tr style=" align="center">';
foreach($valuearray as $field)
{

echo "<td>$field</td>";
}
echo "</tr>";
}
echo "</table>";
?>
3
  • Look into using HEREDOCs for building those SQL queries. It'll save you the headaches of repeated string concatenation and allow you write it out 'nicely' with indenting. Commented Aug 11, 2011 at 16:18
  • 1
    dunno if it influenced on something but in this string "echo '<tr style=" align="center">';" you have style="(double-quote) not a style=''(two single quotes). i.e. this is generate invalid html(i'm sure browser will fix it) Commented Aug 11, 2011 at 16:25
  • wow, what a Query you have 4 subqueries and 6 joins, can you tell me what is performance of this? just wondering Commented Aug 11, 2011 at 19:39

1 Answer 1

2

You have 5 <th> headers but select 6 columns from your table. Either explicitly print the fields from the table you want (you should), or change the query to only select the 5 you want.

Sign up to request clarification or add additional context in comments.

7 Comments

I need the 6 fields can you show me how can increase my table to 6, and possibly separate the headers to match with the columns,,, ?
Write <th>6th Field</th> in the first row of the table where you want it to be. The headers will appear in the order you write them in the HTML.
the headers are now no inline with the output, how can give a static measure to the generated HTML
Instead of a loop foreach($valuearray as $field) write out the echo '<td>' . $valuearray['fieldname'] . '</td>'; code in the order you want them displayed.
this {//var_dump($database); $tableStyle = "padding: 5px;border:1px"; $tdStyle = "padding:5px "; echo '<table style="' . $tableStyle . '" cellpadding="9" cellspacing="9">'; echo "<tr> <th> Quiz Title </th><th> Score </th><th>Maximum Score </th><th> Unanswered </th> <th> Finished On </th> <th></th></tr>"; $row = $database->loadRowList(); foreach($row as $valuearray) { echo '<tr style=" align="center">'; echo '<td>' . $valuearray['score'] . '</td>'; echo "</tr>"; } echo "</table>"; ?>}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.