2

In my website I want to show student details in a page. I am using tables for the designing purpose. what I need is, I want to show two students in each rows. So if there are total 10 students in database, then there should be 5 rows in the display page with each rows having two students.

How can I achieve this?

5
  • I'm not entirely sure why you would want to malform the nature of a table, but it's really just a case of outputting the same data in a different way.. Commented Oct 10, 2013 at 7:50
  • simply iterate over recordset, open a TR for Odd number and close TR on Even iteration, and put student info in a TD, to format information use Table within your TD. to make it look good or DIV or P, OR SPAN whatever you like. Commented Oct 10, 2013 at 7:51
  • without code how can we help? Commented Oct 10, 2013 at 7:54
  • 1
    Please use the search function: stackoverflow.com/questions/1793716/… stackoverflow.com/questions/10902356/… ... Commented Oct 10, 2013 at 7:57
  • share your HTML code please. Commented Oct 10, 2013 at 8:02

2 Answers 2

2
$students = Array('S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10');
$html = '<table>';
for ($i = 0; $i<count($students); $i+=2)
{
    $html.= '<tr>
                <td>'.$students[$i].'</td>
                <td>'.$students[$i+1].'</td>
            </tr>'; 
}
$html.= '</table>';
Sign up to request clarification or add additional context in comments.

Comments

0

So let's say that you have an array with your 10 students, for example $myStudents

To create your table try this code

<table>
<tr>
  <td>Student Name</td>
  <td>Student Name</td>
</tr>
<?php
 $cols = 2;
 $colNum=0;
 $trOpened = false;
 for($i=0;$i<count($myStudents);$i++):

    if( $colNum % 2 != 0 || $colNum==0 ){
       echo "<tr>";
        $trOpened = true;
    }
   ?>
     <td><?=$myStudents[$i]['StudentName']?></td>
 <?php
   $colNum++;
   if(  $trOpened == true && $colNum % 2 == 0 ){
        echo "</tr>";
        $trOpened = false;
        $colNum = 0;
   }

 endfor;


   ?>
</table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.