I'd like to add page like this :
foreach($list->result() as $row){
if($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
I want if this data > 12 page break , if this data < 12 not add page but if data > 6 add more space
My problem is, it's always like this :
1
2
3
4
5
space
space
space
6
7
8
9
10
11
break
space
space
space
12
This can be easily fixed by:
foreach ($list->result() as $row)
{
if ($row['SOMETHING??'] >= 12)
{
/// Add Page Break Here
echo "<p>break</p>";
}
if ($row['SOMETHING??'] >= 6 && $row['SOMETHING??'] < 12)
{
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
Look carefully at the comparison operators which are being used.
Documentation:
http://php.net/manual/en/language.operators.comparison.php
You just need to re-arrange the order of what you are doing, you are outputting the breaks Before you output the line they relate to
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}
You may also need to stop the %6 test doing something when the %12 line will have done something like so
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 && $no % 12 != 0) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}
Related
I'm just a little stumped, I have a simple range of 1-20 listed out, and displays as follows, showing multiples of 3 within a range, and multiples of 5 within a range. These were displayed with echoes, is there a possible way that I can just count the number of times a specific multiple was displayed/echoed? for instance this is what I have:
1, 2, 3foo, 4, 5bar, 6foo, 7, 8, 9foo, 10bar, 11, 12foo, 13, 14, 15bar, 16, 17, 18foo, 19, 20bar
but I would like it to show a count like
foo: 6 times listed
bar: 4 times listed
Does anyone know of a possible way to use echo substr_count for just words "foo" and "bar" that have been echoed within a range of 1-20?
Let me know if I can clarify. Any guidance would be greatly appreciated!
This is my code:
<?php
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
} elseif ($number % 3 == 0) {
echo "foo ";
} elseif ($number % 5 == 0) {
echo "bar ";
}
}
echo "<br>";
ob_start();
// code that prints all the numbers
$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");
echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";
?>
Use the output buffering functions to capture echoed output into a variable.
ob_start();
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
} elseif ($number % 3 == 0) {
echo "foo ";
} elseif ($number % 5 == 0) {
echo "bar ";
}
}
$output = ob_get_flush();
$foo_count = substr_count($output, "foo");
$bar_count = substr_count($output, "bar");
echo "foo: $foo_count times listed<br>";
echo "bar: $bar_count times listed<br>";
But doing it this way is silly, you can just increment the counters in the loop:
$foo_count = $bar_count = 0;
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ($number % 3 == 0 && $number %5 == 0) {
echo "foobar ";
$foo_count++;
$bar_count++;
} elseif ($number % 3 == 0) {
echo "foo ";
$foo_count++;
} elseif ($number % 5 == 0) {
echo "bar ";
$bar_count++;
}
}
DEMO
<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.
Suppose I have 56 records in my database. I'm using a FOR LOOP to display them.
I want to append div after 10 records every time.
<?php
$a = 56;
for($i=0;$i<$a;$i++){
echo "<div>";
echo $i."</br>";
echo "</div>";
}
?>
output:
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
...
<div>56</div>
I want to append <div> after 10 records each time
<div>
0
1
2
.
9
</div>
<div>
10
11
.
19
</div>
.
.
<div>
51
52
.
.
56
</div>
You can use the modulus operator for that.
<?php
$a = 56;
echo "<div>";
for($i=0;$i<$a;$i++){
echo $i . "<br />";
if (($i+1) % 10 == 0) {
echo "</div><div>";
}
}
echo "</div>";
?>
Note that if the number of loops is unknown - it's possible that $a will be 9, and your output will be:
<div>
0
...
9
</div><div>
</div>
If you want to prevent the duplicate of the close-open div in case of exact division, you can change your code to:
<?php
$a = 56;
echo "<div>";
for($i=0;$i<$a;$i++){
echo $i . "<br />";
if (($i+1) != $a && ($i+1) % 10 == 0) {
echo "</div><div>";
}
}
echo "</div>";
?>
You need to append the opening and the closing divs based on some condition which primarily are
If it is the first iteration
If it is the tenth iteration
If it is the last iteration
Here is a sample code:
$a = 56;
for ($i = 0; $i < $a; $i++) {
// If it is the first iteration, echo a opening div
if ($i == 0)
echo "<div>";
// If it is a tenth iteration but not the last one then append a closing and a opening div
if ($i != 0 && $i % 10 == 0 && $i != $a - 1)
echo "</div><div>";
echo $i.
"<br>";
// If it is the last iteration, append a closing div
if ($i == $a - 1)
echo "</div>";
}
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.
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>