Trying to create DIV containers out of a mysql results which each hold chunks of 10.
$balloon_count = the amount of records each div should hold.
$ui = the loop counter.
Functionality is simple, don't want to a template engine.
Trying to use the MODULUS operator to simplify the div cuts.
Doesn't work. Any direction is greatly appreciated.
Sample Code
$ui=1;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
if($ui==10||$ui%$balloon_holds != 0){
echo '</td></tr></table></div>';
}
$ui++;
}
Sample Expected "HTML" Output
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
Record10
</td></tr></table></div>
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record11
Record12
Record13
Record14
Record15
Record16
Record17
Record18
Record19
Record20
</td></tr></table></div>
If I understand the question correctly, you want the <div> opener on $ui = 1,11,21,31,... and the </div> closer on 10,20,30,40,...
If this is correct, your modulus operators should be changed as such:
if($ui%$balloon_holds == 1)
{
...
}
...
if($ui%$balloon_holds == 0)
{
...
}
if($ui==1||$ui%$balloon_holds != 0)
This will always be true except for when $ui is a multiple of 10, so you'd be outputting your header/footer blocks for ALL rows except 0, 10, 20, etc... and the one case where $ui is 1.
Most like likely this would be easier to understand:
$row_cnt = 0;
$max_rows = 10;
while($row = ...) {
if ($row_cnt == 0) {
// output header
}
// output row data
if ($row_cnt == 9) {
// output row footer
}
$row_cnt++;
$row_cnt %= $max_rows; // ($row_cnt resets to 0 when it reaches 10)
}
Here is my suggestion, it's also a little more readable / maintainable.
$ui=0;
$balloon_holds = 10;
while ($row = mysql_fetch_array($result))
{
if ($ui % $balloon_holds)
{
if ($ui >= 10)
{
echo '</td></tr></table></div>';
}
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"';
$this->ischecked($uid,$row[id]);
echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
}
if ($ui > 0)
{
echo '</td></tr></table></div>';
}
$ui=0;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
$exit = 0;
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
if($ui%$balloon_holds == 0){
echo '</td></tr></table></div>';
$exit = 1;
}
}
if($exit == false){
echo '</td></tr></table></div>';
}
Related
<div>
<?php if ($result->num_rows > 0) {
$i=1;
while($row = $result->fetch_assoc()) {
if( $i % 6 == 0 )
{ ?>
</div>
<div>
<?php } ?>
<h4><?php echo $row["city"] ?></h4>
<h6><?php echo $row["info"] ?></h6>
<?php $i++;
}
} else {
echo "0 results";
}
?>
</div>
Goal: div's with each 6 rows in it.
When I use $i=1, the first gets 5 results and the other ones get 6.
When I use $i=0 the first one is empty and the other ones get 6.
How to get the first div also filled with 6 results?
Try using array_chunk. That way you don't have to worry where to put your div ends and it's more readable:
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
foreach (array_chunk($rows, 6) as $subset) {
echo "<div>";
foreach ($subset as $row) {
echo "<h4>{$row["city"]}</h4>"
echo "<h6>{$row["info"]}</h6>"
}
echo "</div>";
}
Using array_chunk as proposed by #Justinas is a good way to refactor code. Yet, taking your original code, the issue is about where you check printed amount. It is wrong to first check output amount as it breaks the logic for the first iteration. Please, refer to the code below.
<div>
<?php
if ($result->num_rows > 0) {
$i = 1;
while ($row = $result->fetch_assoc()) {
# if ($i % 6 == 0) {
# echo '</div><div>';
# }
# move commented code from above to ...
echo "<h4>{$row["city"]}</h4>";
echo "<h6>{$row["info"]}</h6>";
# ... here
if ($i % 6 == 0) {
echo '</div><div>';
}
$i++;
}
} else {
echo "0 results";
}
?>
</div>
You can try setting $i = 0 then excluding it in your if statement with
if ($i > 0 && $i % 6 == 0)
since 0 % 6 == 0.
My code is below:
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
$i++;
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
}
echo '</div>';
}
}
portfolio_gallery();
I wanted to echo </div><div> for every after 9th item of the loop but every time I executed the code, the first echo only happened after 8 items instead of 9, but the rest was every 9th.
You have to increment
$i
after
if($i % 9 == 0)
follow the syntax example i worked out its working
<?php
$j=0;
for($i=0;$i<=50;$i++)
{
if($j==9)
{
echo $j.'hioiiiiiii<br/>'; //echo "</div><div>";
$j=-1;
}
$j++;
}
?>
Please try this :)
<?php
function portfolio_gallery() {
global $conn;
$query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC");
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 0;
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 0){
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++;
}
echo '</div>';
}
}
declare $i = 1 and Write $i++ at the end of while loop.
if ($query->num_rows > 0) {
// output data of each row
echo '<div>';
$i = 1; // declare $i = 1 here
while($row = $query->fetch_assoc()) {
if ($row["showimage"]) {
if($i % 9 == 1){ // 10 , 19 ,28
echo '</div><div>';
}
echo '<a class="imgpop" href="images/portfolio/large/'.$row["codename"].'.jpg" rel="'.$row["namegroup"].'" title="'.$row["title"].' - '.$row["features"].'"><img src="images/portfolio/thumb/'.$row["codename"].'.jpg" alt="'.$row["title"].'" width="348"/><span class="imgpop-caption">'.$row["title"].'</span></a>';
}
$i++; // increment at end of the loop
}
echo '</div>';
}
I was able to solve it by modifying the condition:
So instead of: if($i % 9 == 0) {...}
I used: if($i!=0 && $i % 9 == 0) {...}
And also placing $i++ at the end of while loop.
I have problem with that the first row misses a <tr> and then there is one line and then there is a </tr> and then it works perfect with one <tr> and two lines and </tr> and so on. How to change the code to make that work?
Any ideas to use "div" instead of "table"?
<table width="300" border="0" cellspacing="2">
<tbody>
<?
$query = mysql_query("SELECT * FROM pages ORDER BY id ASC");
while($r = mysql_fetch_array($query))
{
if(++$s % 2 == 0) { echo '<tr>'."\r\n"; }
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if(++$i % 2 != 0) { echo '</tr>'."\r\n"; }
}
?>
</tbody>
</table>
Use $s++ instead of ++$s, so you test the value before you increment it. And there's no need to use a separate $i variable, you can just use the same $s variable without incrementing it.
$s = 0;
while ($r = mysql_fetch_array($query)) {
if ($s++ % 2 == 0) {
echo "<tr>\r\n";
}
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if ($s % 2 == 0) {
echo "</tr>\r\n";
}
}
// If we ended loop without closing the row, do it now
if ($s % 2 == 0) {
echo "</tr>\r\n";
}
Sounds like you're trying to build a 2-column table? You don't need two counters for that, one will do:
$cells = 0;
while(...) {
if (($cells % 2) == 0) {
echo '<tr>'; // start a new row for cells 0, 2, 4, etc...
}
echo '<td> ....';
if (($cells % 2) == 1) {
echo '</tr>'; // close the row after outputting cells 1,3,5,etc...
}
$cells++;
}
Heres the scenario, I have array with 7 items and I want to separate them in every fourth iteration.. just like this
$counter2 = 0;
$counter3 = 0;
$counter4 = 0;
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0)
{
echo $sample_array_value;
}
elseif(++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
}
The out put will be div AsoPusaDagaKuting /div div TutaBubwitBoom
The problem is when it ends in iteration that doesn't count 4 it doesn't give the separator ending..
I need it to output div AsoPusaDagaKuting /div div TutaBubwitBoom /div
Thanks in advance...
You can split it with array_chunk, implode the new subarrays of 4 with array_map, then echo it with implode.
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
echo "<div>", implode("</div><div>", array_map("implode", array_chunk($sample_array, 4))), "</div>";
Result:
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>
Try this:
$i = 0;
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0 || ++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
$i++;
}
if ($i % 4 != 0) {
echo "</div>";
}
You are printing the values inside every condition, then why not use echo once outside any condition? Also you want to close and open a div tag for the forth element, then only 1 counter would do the trick. Only this will work -
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
$i = 0;
echo "<div>";
foreach($sample_array as $sample_array_value)
{
if($i > 0 && $i % 4 == 0)
{
echo "</div><div>";
}
echo $sample_array_value;
$i++;
}
echo "</div>";
Output
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>
I'm not exactly sure how to explain this but basically I have an x variable equal to the number of rows in a database and each time it is equal to 4 or less, run a piece of code.
So say I have 5 rows, I want to execute the code twice because x is equal to 4 once and then equal to 1. Same if I had 6 rows (4+2) 7 rows (4+3) or 8 rows (4+4).
Then, when I reached 9 rows (4+4+1), it would execute the code thrice. Etc...
The code I'm trying to run is a piece of HTML with some more PHP inside it. I want to create a new <ul> with the said code inside it each time the number of rows is equal to 4 or less as explained above.
The code as it is right now:
<div class="row-fluid well">
<?php
$num_rows = mysql_num_rows($result);
if ($num_rows <= 4) {
?>
<ul class="minigames">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<li class='span3'>";
if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
}
?>
<img src="<?php echo $row['app_preview']; ?>">
<div class="minigames-caption">
<h4><?php echo $row['app_name']; ?></h4>
<p>By <?php echo $row['app_dev']; ?>.</p>
</div>
</a>
<?php
echo "</li>";
}
?>
<?php
}
?>
If the number of rows are limited, I would put the results into one array and then use array_chunk() to split them into portions.
$results = array();
while ($row = mysql_fetch_array($result)) {
$results[] = $row;
}
foreach (array_chunk($results, 4) as $chunk) {
echo '<ul>';
foreach ($chunk as $row) {
echo '<li>', /*....*/, '</li>';
}
echo '</ul>';
}
Divide x by 4 and round it up. Eg 4/4 = 1, 5/4 = 2, 9/4 = 3. Then, use a for loop.
var noOfTimes = //x/4 rounded up
for (var i = 0; i < noOfTimes; i++) {
//insert code here
}
Use the ceil function.
http://us3.php.net/manual/en/function.ceil.php
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < ceil($num_rows / 4); $i++) {
// do stuff
}