0

I have this table enter image description here

And my goals is to have a table looking like this

enter image description here

The table has 8 rows and 3 columns

I have already stored the questions but I'm having a hard time printing it in this format.

Wherein after printing 4 questions it will then go to another cell and print 4 questions again and then once there are already 3 columns it will then create a new row and do the same step again.( 8 rows and 3 columns)

The questions are ordered by sequence no. [In the table below the 1st 4 questions are 1,2,3,4 and then the cell on it's right are 6,7,8,9 and so on....]

I'm stuck with this for almost 8 hours already....

Here's my current code:

select = "SELECT q.QuestionID, q.QuestionText, q.m, q.l, q.GroupNo, q.SequenceNo FROM question2 q Order By q.SequenceNo";
$result = mysql_query($select);
$question_id = "";
$question_text = "";
$question_m = "";
$question_l = "";
$question_group = "";
//$table = "<table border='1' cellspacing='1'>";
$count_row = 0;
$array = array();
//$count_column = 0;
while($row = mysql_fetch_array($result))
{
    $question_id = $row['QuestionID'];
    $question_text = $row['QuestionText'];
    $question_m = $row['m'];
    $question_l = $row['l'];
    $question_group = $row['GroupNo'];
    $question_sequence = $row['SequenceNo'];

    if($count_row > 2)
    {
        $array[] .= "</div>";   
    }

    $array[] .= "<div style='border=1px';'>";
    $array[] .= $question_text ."<br/>";
    $count_row++;
}

$table .= "</table>";
echo $table;

Sir/Ma'am your answers would be of great help. Thank you++

1
  • sound like array_chunk() can be useful for you Commented Jun 27, 2012 at 13:48

2 Answers 2

1

I am not sure I fully understand the question but you could try this. I believe it will give you the output you want.

$row_count = 0;
echo "<table border=1>";

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

    if($row_count%12 == 0) {
    echo "<tr>";
    }

    if($row_count%4 == 0) {
    echo "<td>";
    }

    echo $row['QuestionText']."<br>";

    if($row_count%4 == 3) {
    echo "</td>";
    }
    if($row_count%12 == 11) {
    echo "</tr>";
    }
    $row_count++;

}
echo "</table>";

It should give you an output that looks like this:

<table border=1>
<tr>
<td>0<br>1<br>2<br>3<br></td>
<td>4<br>5<br>6<br>7<br></td>
<td>8<br>9<br>10<br>11<br></td>
</tr>
<tr>
<td>12<br>13<br>14<br>15<br></td>
<td>16<br>17<br>18<br>19<br></td>
<td>20<br>21<br>22<br>23<br></td>
</tr>
<tr>
<td>24<br>25<br>26<br>27<br></td>
<td>28<br>29<br>30<br>31<br></td>
<td>32<br>33<br>34<br>35<br></td>
</tr>
...
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Wow great solution sir. It worked perfectly. Do you have another way of doing this? I'm just curious sir. Thank you++ for the immediate reply. I owe you a lot. :D
anyway, it was a very good solution that you provided. wow, I wasn't able to come up with such solution for almost 8 hours and you did it with what? 10 mins. Thank you++ again sir.
1
$querstions = array();
$query = "SELECT QuestionID, QuestionText, m, l, GroupNo, SequenceNo FROM question2 ORDER BY SequenceNo";
$resource = mysql_query($query);
while($row = mysql_fetch_array($result))
{
   $querstions[$row['QuestionID']] = $row;
}

/* print table head */
echo "<table><thead>....

/* split up question in master_rows with 12 question each */
foreach(array_chunk($querstions, 12) as $master_row)
{
    echo ...

    foreach(range(0, 3) as $row_nr)
    {
        echo ... $master_row[$row_nr] ...;

        echo ... $master_row[$row_nr + 4] ...;

        echo ... $master_row[$row_nr + 8] ...;
    }

    echo ...
}

Comments

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.