Hi, How can I use modulus to add TR tag for my output below?

<tr>
<td>data1<td>
<td>data1<td>
<td>data1<td>
<td>data1<td>
<td>data1<td>
</tr>
<tr>
<td>data1<td>
<td>data1<td>
<td>data1<td>
<td>data1<td>
<td>data1<td>
</tr>
<tr>
<td>data2<td>
<td>data2<td>
<td>data2<td>
<td>data2<td>
<td>data2<td>
</tr>
<tr>
<td>data2<td>
<td>data2<td>
<td>data2<td>
<td>data2<td>
<td>data2<td>
</tr>

In the example above got 2 set of data. It will loop 5 times inside the first 2 tr tag

    <tr>
    <td>data1<td><---loop data 
    <td>data1.1<td><---loop data
    <td>data1.2<td><---loop data
    <td>data1.3<td><---loop data
    <td>data1.4<td><---loop data
    </tr>
    <tr>
    <td>data1<td><---loop data
    <td>data1.1<td><---loop data
    <td>data1.2<td><---loop data
    <td>data1.3<td><---loop data
    <td>data1.4<td><---loop data
    </tr>

and after 5 times will create another tr tag to do the samething.

        <tr>
        <td>data2<td><---loop data 
        <td>data2.1<td><---loop data
        <td>data2.2<td><---loop data
        <td>data2.3<td><---loop data
        <td>data2.4<td><---loop data
        </tr>
        <tr>
        <td>data2<td><---loop data
        <td>data2.1<td><---loop data
        <td>data2.2<td><---loop data
        <td>data2.3<td><---loop data
        <td>data2.4<td><---loop data
        </tr>

and so on.. Hopefully you can understand my explaination.

Recommended Answers

All 5 Replies

I can't understand the code you gave me. I don't know how to change it into while($row = mysql_fetch_array($sql)) format. =(

Use the second snippet from cdoggg94.

Hi,

I don't know if this will help you, but this is the representation of your example above.

css credit goes here.

Modulu is really good in doing the odd and even rows, but when you need to be specifically want to plug something between the iterated items, a simple for loop will probably do the job.

As I already mentioned.. codes below represents you sample html above. Feel free to experiment with it. Just make sure that the loops has ends, otherwise it will go infinite..

<style type="text/css">
table.gridtable {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
}
table.gridtable th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
}
table.gridtable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
}
</style>

<table class="gridtable">
    <tr>

<?php

$col = 5; $i = 0; $x = 0; $y = 0; $z = 0;
$tdArr = array('Data One', 'Data Two', 'Data Three');

for($z = 1; $z <= $col; $z++){

## echo your  table cols.

        echo '<th>Col '.$z.'</th>';


}

 echo '</tr><tr>';
                ## for first row 
                for($i = 1 ; $i <= $col; $i++){
                echo '<td>'.$tdArr[0].' '.$i.'</td>';
                    if($i == $col){
                    echo '</tr>';
                    }}

        ## second row
    echo '<tr>';
            for($x = 1 ; $x <= $col; $x++){
    echo '<td>'.$tdArr[1].' '. $x .'</td>';
        if($x == $col){
        echo '</tr>';
        }}

        ## third row
    echo '<tr>';
            for($y = 1 ; $y <= $col; $y++){
    echo '<td>'.$tdArr[2].' '. $y .'</td>';
        if($y == $col){
        echo '</tr>';
        }}          
?>

</table>

Thank you everyone ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.