1

I have query result like this

Query result Table:

enter image description here

I need to display this data to html table view like

HTMLTable:

enter image description here

So how should i convert this result array variable in to such PHP array variable so i can loop that array variable and display the result according to attached html Table view

Please help. Thanks.

1

1 Answer 1

1

One way is you can use mysql_fetch_assoc() function in a loop like this :

// The array which will store all questions
$questions = array();

while ($row = mysqli_fetch_assoc($result)) {

   // Put in the array questions an array per question using mysql fieldsnames

   // if a question with main_question_id=2 exists 
   if (!isset($questions[$row['main_question_id'])) {

      //build your question and put it in your array
      $questions[$row['main_question_id']] = array(
         'question_num' => $row['main_question_number'],
         'description' => $row['descritption'],
         'obtained_mark' => $row['obtained_mark'],
         // etc ...
      );
   }
}

// Displays description for question 2 for example :
echo $questions[2]['description'];

Then you can build your html (were also doable during first loop)

// Builds head of html table
$html = '<table><tr><th>Question Number</th><th>Desc.</th><th>Mark</th></tr>';

// Builds html content table with another loop
foreach ($questions as $question){
    $html .= '<tr>';
    $html .= '<td>'.$question["main_number_question"].'</td>';
    $html .= '<td>'.$question["description"].'</td>';
    $html .= '<td>'.$question["obtained_mark"].'</td>';
    $html .= '</tr>';
}

// build the bottom of table
$html .= '</table>;


// Displays all table
echo $html;

Dont forget to check doc and comments behind her here : http://php.net/mysqli_fetch_assoc

You can also use http://php.net/mysqli_fetch_array

Cheers,

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

1 Comment

Thanks for the reply bro but i already have fetched result in to a PHP array variable but i need to store these rows in to multi dimensional array like **main_question > sub_question > child_question **

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.