Reputation: 13
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 matches in this table, but soon to be 500+, so to not to blow my viewers minds, i want my code to perform a line break (or maybe a new table row, thats not important now, just some HTML tag) after every 10th result is shown(because every week there are 10 matches)
Here is my PHP code:
$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures='';
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>';
}
And here is HTML output:
<table>
<?php echo $fixtures; ?>
</table>
I have tried some methods shown on stackoverflow, but I was not able to implement them properly to my solution. Because I am kinda new to PHP I am asking for specific solution.
Thank you.
Upvotes: 0
Views: 4055
Reputation: 114
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) or die (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 into
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[$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 group
if (($fixtureCount % $fixturesPerGroup) == 0) {
$fixtureGroup++;
}
}
Then you can loop through the groups:
<table>
<?php
foreach ($fixtures as $fixturesInGroup) {
// Do logic here for displaying each group
echo implode('<br />', $fixturesInGroup);
}
?>
</table>
Upvotes: 0
Reputation: 2476
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) or die (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 and reset $i
}
}
Upvotes: 1
Reputation: 1049
Try this ;)
<?php
$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (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++;
}
Upvotes: 4
Reputation: 21661
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.
Upvotes: 4
Reputation: 817
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!
Upvotes: 2