PHP echo rows in table

Skythrust

Member
Jul 9, 2019
133
7
Over here the (new) code

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>";
/* OLD CODE
            if ($i>6){
                $i=0;
                echo '</tr>';
            }; 
            $i++;       
*/

// 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 />";
    }
}           
        }
        echo "</table>";

    sqlsrv_free_stmt( $stmt);
?>
 

oxuwazet

New Member
Sep 17, 2019
5
2
Kinda hard to spot errors with improper code indentation and missing start tags.

From looking at it is because you put that code in a while loop. So its looping that code so 19 results times how ever times the while loop goes. To clear it up is some rows show up as 7 is because with 19 results it leaves one on a row by its self then returns the whole loop and the loop shows 6 more results so ultimately showing 7.

Here is how you should implement it.

PHP:
<?php

// your code here.

// the number of results.
$noof = sqlsrv_num_rows($stmt);

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

// start tables logic.

echo "<tr>\n";
$i = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    $i++;
    if ($i % $ross == 0) {
        output_row_data($row);
        echo "</tr>\n";
        if (!($noof - 1 < $i)) {
            echo "<tr>\n";
        }
    } else {
        output_row_data($row);
    }
}
if (!($i % $ross == 0)) {
    echo "</tr>\n";
}

// end tables logic.

// make the output row data function.

function output_row_data(array $row)
{
    // out your row data.
}

?>
 

Skythrust

Member
Jul 9, 2019
133
7
Kinda hard to spot errors with improper code indentation and missing start tags.

From looking at it is because you put that code in a while loop. So its looping that code so 19 results times how ever times the while loop goes. To clear it up is some rows show up as 7 is because with 19 results it leaves one on a row by its self then returns the whole loop and the loop shows 6 more results so ultimately showing 7.

Here is how you should implement it.

PHP:
<?php

// your code here.

// the number of results.
$noof = sqlsrv_num_rows($stmt);

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

// start tables logic.

echo "<tr>\n";
$i = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    $i++;
    if ($i % $ross == 0) {
        output_row_data($row);
        echo "</tr>\n";
        if (!($noof - 1 < $i)) {
            echo "<tr>\n";
        }
    } else {
        output_row_data($row);
    }
}
if (!($i % $ross == 0)) {
    echo "</tr>\n";
}

// end tables logic.

// make the output row data function.

function output_row_data(array $row)
{
    // out your row data.
}

?>

The way why I loop it so much is because the SQL table Available shows a number and with this number he need to follow the class color.
 

oxuwazet

New Member
Sep 17, 2019
5
2
I think i answered your question. You told me what it should look like and i have provided a block a code that does exactly that and i have explained why your code does not work so it should not be hard to fix it yourself. If any other errors occur open a new thread.
 
Last edited:

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
If I understand correctly what you want to do is have a new table row (<tr>) every six rows in your database?
If so, just put your HTML in this

PHP:
<table>
    <tr>
        <?php
            $item = 0;
            while($row = SQLSRV_FETCH_ARRAY($stmt, SQLSRV_FETCH_ASSOC)) {
        ?>
        // Whatever you want in the table
        // This is what would be split into the TRs every six
        <?php
                $item++;
                if ($item % 6 == 0) { echo '</tr><tr>'; }
            }
        ?>
</table>
 

Skythrust

Member
Jul 9, 2019
133
7
If I understand correctly what you want to do is have a new table row (<tr>) every six rows in your database?
If so, just put your HTML in this

PHP:
<table>
    <tr>
        <?php
            $item = 0;
            while($row = SQLSRV_FETCH_ARRAY($stmt, SQLSRV_FETCH_ASSOC)) {
        ?>
        // Whatever you want in the table
        // This is what would be split into the TRs every six
        <?php
                $item++;
                if ($item % 6 == 0) { echo '</tr><tr>'; }
            }
        ?>
</table>

Awesome!! This is where I was looking for.
 

Users who are viewing this thread

Top