MySQL display multiple rows in a table using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imrannawabkhan
    New Member
    • Sep 2007
    • 4

    MySQL display multiple rows in a table using PHP

    I was working on project page where I required to get the row data from mySql db in side the table cells. I knew how to use the php script to get the row data into the table cells like.

    Id | Name | Exame | Nos
    1 | Nasir | Maths | 25
    2 | Abdulla | Maths | 50
    3 | Fazalull | Maths | 41
    4 | Jaraba | Maths | 05

    But what If I want list of students in this order

    List of Students4

    Nasir | Abdulla
    Fazalull | Jaraba


    I couldn't do the multi rows even I search alot but couln't find a solution.
    But then I found a solution by my self. So here how to do it may be it can help some buddy.

    Lets create a table

    [PHP]
    <table width="100%" border="0" cellspacing="0" cellpadding="0" >
    <tr>
    <td></td>
    <td></td>
    </tr>

    <?php
    $query=" SELECT * FROM table order by id ASC";
    $result=mysql_q uery($query);
    echo mysql_error();
    ?>

    <tr>

    <?php while($ezkez = mysql_fetch_arr ay($result)) { ?>

    <td width="41%" ><a href="student.p hp?Name=<?php echo "$ezkez[Name]" ?>" class="sublink" ><?php echo "$ezkez[Name]" ?></a></td>

    <?php next($ezkez = mysql_fetch_arr ay($result)); ?>

    <td width="41%" ><a href="student.p hp?Name=<?php echo "$ezkez[Name]" ?>" class="sublink" ><?php echo "$ezkez[Name]" ?></a></td>

    </tr>
    <?php
    }

    $anymatches=mys ql_num_rows($re sult);
    if ($anymatches == 0)
    {
    ?>
    <tr >
    <td class="sublink" colspan="2" align=left><div align='center'> <b>Sorry, no record found</b></div></td>
    </tr>

    <?php
    }
    ?>
    <tr>
    <td></td>
    <td></td>
    </tr>
    </table>[/PHP]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You would post this in the 'howtos' section, as this is for questions ;)

    Also, that code is ok but not great as you are constantly opening and closing the php parser.

    take a look here.

    I had the same problem once and pbmods helped me out greatly.

    Comment

    Working...