PHP echo rows in table

Skythrust

Member
Jul 9, 2019
133
7
Goodafternoon all,

I am trying to show a limit records per row.

I use this code but whatever I tried the values is always + 2 any idea what the problem could be?

PHP:
if ($i>6){
   $i=1; 
      echo '</tr>';
   };  
   $i++;



Thanks in advance!
 

JynX

Posting Freak
Feb 6, 2016
710
438
Either you're missing parts to your code or I have no clue what you are trying to accomplish..
 

Higoka

Active Member
Dec 16, 2018
174
74
dont know exactly what you want to do but looking at the snippet you provided you want something like this
PHP:
for ($i = 0; $i <= 6; $i++) {
    echo '</tr>';
}
 
Last edited:

Skythrust

Member
Jul 9, 2019
133
7
Hi All, sorry about the short information. I would like to have is when I set $i = 6 that the records on the row are 6 and that he will start a new line till 6 for the whole page so when I got 19 results then I got 5 rows, 4 with 6 results. and 1 line with 1 result.

When I use the code about whatever I set on the $i value is always value +2.

Example with the current code;

PHP:
if ($i>6){
   $i=1;
      echo '</tr>';
   };
   $i++;

{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}
{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}
{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}

But it should be;

{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}
{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}
{RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT} - {RESULT}
{RESULT}
 

Higoka

Active Member
Dec 16, 2018
174
74
try this, not tested
PHP:
$i = 0;

foreach (.....) {
    if ($i === 6) {
        $i = 0;
        echo '</tr>';
        continue;
    }
    ..... 
    $i++;
}
 

Skythrust

Member
Jul 9, 2019
133
7
Here it is

PHP:
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<?php include '../Management/Configuration.php';
    $stmt = SQLSRV_QUERY( $conn, $SQL_Query );
    if( $stmt === false) {
        die( print_r(
        $System_Message, true) );
    }
    echo "<table>    ";
    while( $row = SQLSRV_FETCH_ARRAY( $stmt, SQLSRV_FETCH_ASSOC) ) {
        echo "<td>";
            $ImageFilter = ($row['Available'] == 0 || $row['Available'] == 31) ? '<div class="ImageON">' : '';
            $useFilter = ($row['Available'] == 0 || $row['Available'] == 31) ? '<div class="tooltipON">' : '';
            $useImage = $row['Data'] === NULL ? 'images/Avatar.png "height="50" width="50""' : /* 'data:image/jpeg;base64,'.base64_encode($row['Data']); */ ($row['Data'])  ?>

            <div class="middle"><?php if ($row['Available'] == 1) { ?>
            <div class="textGreen"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 2) { ?>
            <div class="textDarkGreen"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 3) { ?>
            <div class="textBlue"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 4) { ?>
            <div class="textDarkBlue"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 5) { ?>
            <div class="textLightBlue"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 6) { ?>
            <div class="textPurple"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 7) { ?>
            <div class="textOrange"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 8) { ?>
            <div class="textYellow"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 9) { ?>
            <div class="textDarkRed"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 10) { ?>
            <div class="textSilver"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 21) { ?>       
            <div class="textGreenBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 22) { ?>
            <div class="textDarkGreenBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 23) { ?>
            <div class="textBlueBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 24) { ?>
            <div class="textDarkBlueBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 25) { ?>
            <div class="textLightBlueBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 26) { ?>
            <div class="textPurpleBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 27) { ?>
            <div class="textOrangeBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 28) { ?>
            <div class="textYellowBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 29) { ?>
            <div class="textDarkRedBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 30) { ?>
            <div class="textSilverBHV"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else if ($row['Available'] == 31) { ?>
            <div class="textBHVOUT"><?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } else { ?>
            <div class="textout"> <?php echo $useFilter. '<img src="'.$useImage .'" height="50" width="50" alt="Avatar" '.$ImageFilter.''; } ?>
              
            <b><div id="NameString"><?php echo $row["Firstname"]. ' '; ?></b></div>
            <br><div id="TitleString"><?php echo $row["Middlename"]. ' ' .$row["Lastname"]; ?></div>
        </div>
        </div>
        </div>
        
        <?php echo "</td>";
            
            if ($i>6){
                $i=1;
                echo '</tr>';
            }; 
            $i++;                     
        }
        echo "</table>";

    sqlsrv_free_stmt( $stmt);
?>
 

Higoka

Active Member
Dec 16, 2018
174
74
before the while() add $i = 0;
PHP:
if ($i === 6) {
    $i = 0;
    echo '</tr>';
}

$i++;

havent tested because im on mobile right now
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
I have test that yet, it shows the same results as before with the strange row setting
You should use limiting in your SQL query, not after you've already fetched the rows...
Google is your best friend
 

Skythrust

Member
Jul 9, 2019
133
7
You should use limiting in your SQL query, not after you've already fetched the rows...
Google is your best friend
It is not the pages where I am looking for. I would like to have 1 full page with always 6 records in a row, when he has reached the limit of 6 he need to start a new row.
 

oxuwazet

New Member
Sep 17, 2019
5
2
PHP:
<?php

// the number of results.
$noof = 19;

// the number of results in a row.
$ross = 6;

$i = 0;
for ($ii = 0; $ii < $noof; $ii++) {
    $i++;
    echo "{RESULT}";
    if ($i % $ross == 0) {
        echo "<br />";
    }
}

?>

This will result in:

{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}

For tables:

PHP:
<?php

// the number of results.
$noof = 19;

// the number of results in a row.
$ross = 6;

// start tables logic.

echo "<tr>\n";
$i = 0;
for ($ii = 0; $ii < $noof; $ii++) {
    $i++;
    echo "<td>{RESULT}</td>\n";
    if ($i % $ross == 0) {
        echo "</tr>\n";
        if (!($noof - 1 < $ii)) {
            echo "<tr>\n";
        }
    }
}
if (!($i % $ross == 0)) {
    echo "</tr>\n";
}

// end tables logic.

?>

This will result in:

HTML:
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
</tr>

If you have further questions please ask.
 
Last edited:

Skythrust

Member
Jul 9, 2019
133
7
PHP:
<?php

// the number of results.
$noof = 19;

// the number of results in a row.
$ross = 6;

$i = 0;
for ($ii = 0; $ii < $noof; $ii++) {
    $i++;
    echo "{RESULT}";
    if ($i % $ross == 0) {
        echo "<br />";
    }
}

?>

This will result in:

{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}{RESULT}
{RESULT}

For tables:

PHP:
<?php

// the number of results.
$noof = 19;

// the number of results in a row.
$ross = 6;

// start tables logic.

echo "<tr>\n";
$i = 0;
for ($ii = 0; $ii < $noof; $ii++) {
    $i++;
    echo "<td>{RESULT}</td>\n";
    if ($i % $ross == 0) {
        echo "</tr>\n";
        if (!($noof - 1 < $ii)) {
            echo "<tr>\n";
        }
    }
}
if (!($i % $ross == 0)) {
    echo "</tr>\n";
}

// end tables logic.

?>

This will result in:

HTML:
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
<td>{RESULT}</td>
</tr>
<tr>
<td>{RESULT}</td>
</tr>

If you have further questions please ask.


Seems like this will work, but It doesn't work for me. I got this now;
 

Users who are viewing this thread

Top