1

I'm trying to generate a list from database in a HTML table just like image below;

https://i.sstatic.net/61XLl.png

And here's what i did;

https://i.sstatic.net/lLsvF.png

And the code;

    <table cellpadding="3" border="1"  style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
    <thead>      
        <tr>
            <th>KURSUS</th>
            <th rowspan="2">NAMA PENSYARAH</th>
            <th rowspan="2">NO. SIRI</th>
        </tr>
        <tr>
            <th>NAMA</th>
        </tr>
    </thead>
    <tbody align="center">
        <?php
            if($numrow>0)
            {
                while($row = $select->fetch_assoc()){

                    $code=explode("/",$row['po_code']);
                    $list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
                    ?>

        <tr>
            <td><?php echo $row['po_name']; ?></td>
            <?php while($rowlist = $list->fetch_assoc()){ 

                    $name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
                    $rowname=$name->fetch_array();?>
            <td><?php echo $rowname['user_name']; ?></td>
            <td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
            <?php } ?>
        </tr>

                    <?php
                }
            }
        ?>
    </tbody>
</table>

Help me. Thank you in advance :)

2 Answers 2

1

Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.

<tbody align="center">
        <?php
            if($numrow>0)
            {
                while($row = $select->fetch_assoc()){

                    $code=explode("/",$row['po_code']);
                    $list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
                    ?>

        <tr>
            <td><?php echo $row['po_name']; ?></td>
            <?php
                $user_names = $codes = '';  // define empty variables
                while($rowlist = $list->fetch_assoc()){ 

                    $name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
                    $rowname=$name->fetch_array();
                    $user_names .= $rowname['user_name']."<br/>"; //concat to a single string
                    $codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
            }?>
            <td><?php echo $user_names;?></td>
            <td><?php echo $codes;?></td>
        </tr>

                    <?php
                }
            }
        ?>
    </tbody>
Sign up to request clarification or add additional context in comments.

Comments

0

Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){

Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

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.