Skip to content Skip to sidebar Skip to footer

Php Add Html Break After Every 10th Mysql Result

I am having a trouble with my code. What I am trying to achieve: I have a database table called 'matches', containing info about football fixtures. For now i have around 100 matche

Solution 1:

Try this ;)

<?php$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) ordie (mysqli_error($sql));
$fixtures = '';
$i = 1;

while($row = mysqli_fetch_array($query)) {
    $fixturetime = $row["time"];
    $fixturetime2 = $row["time"];
    $fixtureid = $row["matchid"];
    $fixtureround = $row["round"];
    $hometeamid = $row["hometeamid"];
    $awayteamid = $row["awayteamid"];
    $fixturetime = strtotime($fixturetime);
    $fixturetime = date('d.m.Y', $fixturetime);
    $fixturetime2 = strtotime($fixturetime2);
    $fixturetime2 = date('H:i', $fixturetime2);

    $fixtures .= '<tr><td><strong>' . $hometeamname . '</strong> v. <strong>' . $awayteamname . '</strong></td><td>' . $fixturetime . '</td><td>' . $fixturetime2 . '</td><td style="text-align: right;">' . $fixtureid . '</td><td>' . $i . '</td></tr>';

    if(!($i % 10)){
        $fixtures .= '<!-- break -->';
    }

    $i++;
}

Solution 2:

I suggest using mod or the % sign, this returns the remainder from division ( so the remainder of n / 10 will be 0 every 10 rows ).

So you'll want to have a counter

$count = 1;
while( $row = ..... ){
    if( $count % 10 == 0 ){
     //do line break;
   }


   ++$count;
}

php Documention on mod http://www.php.net/manual/en/internals2.opcodes.mod.php

you might have to play with this a bit but that's the basics.

Solution 3:

I'd suggest the simplest thing I can think of.

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures='';
$counter = 0;
while($row = mysqli_fetch_array( $query )) { 
$counter++;
$fixturetime = $row["time"];
$fixturetime2 = $row["time"];
$fixtureid = $row["matchid"];
$fixtureround = $row["round"];
$hometeamid = $row["hometeamid"];
$awayteamid = $row["awayteamid"];
$fixturetime = strtotime( $fixturetime );
$fixturetime = date( 'd.m.Y', $fixturetime );
$fixturetime2 = strtotime( $fixturetime2 );
$fixturetime2 = date( 'H:i', $fixturetime2 );

$fixtures.='<tr><td><strong>' .$hometeamname. '</strong> v. <strong>' .$awayteamname. '</strong></td><td>' .$fixturetime. '</td><td>' .$fixturetime2. '</td><td style="text-align: right;">' .$fixtureid. '</td><td>' .$i. '</td></tr>';

if (($counter % 10) == 0){ $fixtures .= '<br>'; }
}

This will add 1 to the counter each time that the while iterates. The if statement will check to see whether there's a remainder after checking $counter against 10. If there is one it'll keep running without a line break. If there Is no remainder, meaning it's multiple of 10, it will append a to the end.

Hope this is what you want!

Solution 4:

You will need to count each row. Using a new variable $i starting at 0, this variable will get increased by one at every loop. Then check if that variable is above 10. If it is add more html to the $fixtures string that creates a "blank" row.

If a blank row is found also reset the $i variable back to 0

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) ordie (mysqli_error($sql));
$fixtures='';
$i = 0; // <-- New variable

while($row = mysqli_fetch_array( $query )) { 

    $fixturetime = $row["time"];
    $fixturetime2 = $row["time"];
    $fixtureid = $row["matchid"];
    $fixtureround = $row["round"];
    $hometeamid = $row["hometeamid"];
    $awayteamid = $row["awayteamid"];
    $fixturetime = strtotime( $fixturetime );
    $fixturetime = date( 'd.m.Y', $fixturetime );
    $fixturetime2 = strtotime( $fixturetime2 );
    $fixturetime2 = date( 'H:i', $fixturetime2 );

    $fixtures.='<tr><td><strong>' .$hometeamname. '</strong> v. <strong>' .$awayteamname. '</strong></td><td>' .$fixturetime. '</td><td>' .$fixturetime2. '</td><td style="text-align: right;">' .$fixtureid. '</td><td>' .$i. '</td></tr>';

    $i ++; // <-- add one

    if ($i > 10) { // <-- If larger than 10 then do this statement

        $fixtures.='<tr><td colspan=4>BREAK</td></tr>';
        $i = 0; // <-- Oh andreset $i
    }

}

Solution 5:

I would look up pagination solutions that use OFFSET with the LIMIT clause. Outside of that though, you can actually store your results in groups of 10 and then apply output logic as you loop through the groups:

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) ordie (mysqli_error($sql));
$fixtures = array(); // Each element will be an array of fixtures$fixturesPerGroup = 10; // How many fixtures per group$fixtureCount = 0; // Counter during the while loop iterating$fixtureGroup = 0; // Which group to put the fixtures intowhile($row = mysqli_fetch_array( $query )) { 
    $fixturetime = $row["time"];
    $fixturetime2 = $row["time"];
    $fixtureid = $row["matchid"];
    $fixtureround = $row["round"];
    $hometeamid = $row["hometeamid"];
    $awayteamid = $row["awayteamid"];
    $fixturetime = strtotime( $fixturetime );
    $fixturetime = date( 'd.m.Y', $fixturetime );
    $fixturetime2 = strtotime( $fixturetime2 );
    $fixturetime2 = date( 'H:i', $fixturetime2 );

    $fixtures[$fixtureGroup][] ='<tr><td><strong>' .$hometeamname. '</strong> v. <strong>' .$awayteamname. '</strong></td><td>' .$fixturetime. '</td><td>' .$fixturetime2. '</td><td style="text-align: right;">' .$fixtureid. '</td><td>' .$i. '</td></tr>';

    $fixtureCount++;
    // Once we reach the fixturesPerGroup count, go to the next groupif (($fixtureCount % $fixturesPerGroup) == 0) {
        $fixtureGroup++;
    }
}

Then you can loop through the groups:

<table><?phpforeach ($fixturesas$fixturesInGroup) {
    // Do logic here for displaying each groupecho implode('<br />', $fixturesInGroup);
}
?></table>

Post a Comment for "Php Add Html Break After Every 10th Mysql Result"