user1183013
user1183013

Reputation:

breaking long echoed array content at interval

please i need you help with my script.. I'm trying to echo some content of query into an html table, but i want to break it at every 7th parameter. The script below only breaks the first seven, while the rest are not broken up at interval and they were also echoed out of the html table.

How can I do this please. Thank you for your time and help.

echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;

echo "<tr align= \"center\">\n";

$carry_over = array();
$score_count = mysql_numrows($query8);

echo "<td>" . "Failed: ";
if ($score_count !== 0) {
    while ($row8 = mysql_fetch_assoc($query8)) {
        echo "<th>" . $row8['course_code'] . "</th>";
        if ($count == 7) {
            echo "</tr>\n";
            echo "</table>";

        }
    }
}

Upvotes: 0

Views: 51

Answers (2)

Andreas Wong
Andreas Wong

Reputation: 60584

Use modulo operator instead of equality

if ( ($count+1) % 7 ){

The +1 is there so that it doesn't break immediately on $count == 0 because 0%n is 0

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270727

You need to use the modulus operator %, which returns the remainder after division. $count % 7 ==0 means the current count is on a multiple of 7, and you should break. You also need to increment $count.

echo "<table class=\"altrowstable\" bgcolor = gold >\n";
 $count = 0;
echo "<tr align= \"center\">\n"; 
$carry_over = array(); 
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
    while ($row8 = mysql_fetch_assoc($query8)) { 
        echo "<th>".$row8['course_code']."</th>";

        // Increment $coutn
        $count++;
        // Check the modulus
        if ( $count % 7 == 0 ){
           echo "</tr>\n"; 
           echo "</table>"; 

        }
    }
 }

Upvotes: 0

Related Questions