Insert line break after every two rows of database - php

I have a little script that prints a certain amount of rows in a mysql database.
Is there any way to make it so that after every second row it prints, there is a line break inserted?
Adding a line break after every row is simple, but I don't know how to add one after every other row. Is that possible?

You write "script" but in tags you have PHP, so I suppose you need PHP code:
foreach ($rows as $row) {
if ($i++ % 2) {
// this code will only run for every even row
}
...
}

$i=1;
while ($row = mysql_fetch_array($query))
{
//your code
if ($i % 2 == 0)
echo '<br>';
$i++;
}

add new variable before the loop
$i = 0;
then in your loop add
if ($i != 0 && $i%2 == 0)
echo '<br/>';

Depending on the language, something like this should do it: (in php) (where $arr is an array of results)
$str = '';
$i = 0;
for ($i=0; $i<count( $arr ); $i++)
{
if ( ( $i + 1 ) % 2 === 0 )
{
$str .= $arr[$i] . '<br />';
}
else
{
$str .= $arr[$i];
}
}
echo $str;

Use php and modulo.
such as
if($i % 3)
{
echo '<br />'..

If you need to do this inside the query for some reason, you could use something like
SELECT
<your fields>,
IF (((#rn:=#rn+1) % 3)=0,'<br>','') as brornot
FROM
<your tables and joins>,
(#rn:=0)

Related

PHP even numbers generating

I'm somewhat new to PHP, been reading a few books and I've never seen a loop where it gets you all the even numbers(for example from 1 to 10), so I decided to try it myself:
for($i=0;$i<10 && $i % 2===0;$i++)
echo $i;
Tried with only double == as well.
And this,
$i=0;
do echo $i; while($i++<10 && $i % 2 ==0);
Can't seem to figure out how to use 2 conditions in the same statement.
Would appreciate the help!
Thanks.
Try to use this code
for( $i=0; $i<=10; $i++ )
{
if( $i%2 == 0 ){
echo $i;
}
}
The loop is breaking entirely when the second condition fails the first time. On the first iteration: 0 is less than 10, and it is even, so the loop iterates. On the second iteration: 1 is less than 10, but is odd, so the loop breaks.
Your code is the equivalent of this:
for($i=0; $i<10; $i++) {
if ($i % 2 !==0 ) {
break;
}
echo $i;
}
0
You can eliminate the second condition of your for loop to prevent the breakage and rely exclusive on a third expression to increment $i by two each iteration.
for($i=0; $i<10; $i = $i + 2) {
echo $i;
}
02468
The second statement in a for-loop is/are the condition(s) which gets checked every loop. so if it fails your loop stops. what you need will look somewhat like this:
for ($i = 0; $i < 10; $i++)
if ($i % 2 == 0)
echo $i;
So the loop will run over every number but only print out the even ones.
You don't need to loop.
Range can create a range with third parameter step 2.
$arr = range(0,20,2);
Echo implode(" ", $arr);
https://3v4l.org/S3JWV
you can use also regular loop and get the evens by formula:
for($i=0; $i<10 ;$i++) {
$j = $i * 2;
// do somthing with $j witch loop over 10 first evens...
}

PHP Explode function. If function

Could someone help suggest in below php explode function, we are displaying script after 5th listing. How is it possible to display script exactly after 5th listing and 10th listing on a page which has more than 10 listings
We tried using
if ($i == 5 & $i== 10)
but it does not work
Below is original code - which displays script after 5th listing
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 0; $i < $numberOfListings; ++$i)
{
if ($i == 5)
{ ?>
<script> </script>
<?php }
echo $listings[$i] . "<hr/>";
}
?>
Edit
How is it like - if have to display a separate script on $i==9, could you advise.
Because $i starts at 0 (0 to 9 is 10, whilst 0 to 10 is 11). Try if ($i == 4 || $i== 9), with an or operator.
Also I would not use the && (the and operator), because it is unlikely $i will ever equal both 4 and 9. I'd suggest you read into Truth Tables (and maybe Propositional Calculus) because from seeing what you had tried originally, it would be helpful to understand how a truth table works.
(source: wlc.edu)
You can use the contine, continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
$arr = range(0,9);
foreach($arr as $number) {
if($number < 5) {
continue;
}
print $number;
}
Ref: http://php.net/manual/en/control-structures.continue.php
Try using modulus operator
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 1; $i < $numberOfListings; ++$i)
{
if ($i%5 == 0)
{
echo "in";
?>
<script> </script>
<?php
}
echo $listings[$i-1] . "<hr/>";
}
Here we are looping from 1 and there for $i <= $numberOfListings
and while listing we will use $listings[$i-1]
DEMO CODE AT http://codepad.viper-7.com/lrTOgP

Different css styling for last element of a php array

When looping through an array how can I create a different css div style for the last element to be output in my array.
for($i=0;$i<=count($productid);$i++){if($productrank[$i]>0 ){
<? if (($productid[$i] % 2 ) && !( last element of array){ echo '<div class="centerBoxContentsFeatured centeredContent back vLine " style="width:50%;">';}
else { echo '<div class="centerBoxContentsFeatured centeredContent back " style="width:50%;">';} ?>
Just check if it is the last $productid
for(...)
{
if ($i === (count ($productid) - 1))
// Last one -> special CSS
}
}
Also, DO NOT use count() in a FOR loop if you don't really have to.
Just assign a value BEFORE and use it :
$count_temp = count ($productid);
for ($i = 0; $i < $count_temp; ++$i)
And use this $count_temp again if the IF statement to check if it's the last element
Answer to comment :
How would this same method get the first element?
if ($i === 0)
Or
// Special CSS for $i = 0
// Start loop at 1 instead of 0
for ($i = 1; $i < $count_temp; ++$i)
You can do this purely with CSS using :last-child. It is supported by all modern browsers.
div.centerBoxContentsFeatured:last-child {
/* special styles for last */
}
See it in action: http://jsfiddle.net/9y93j/1/
Like this:
if($productid[$i] == $productid[count($productid)-1]){
//last element code here
} elseif (($productid[$i] % 2 ){
//even row code here
} else {
//odd row code here
}

Is there any operand type that could be used for this?

Look at this table:
<?php
echo '<table>';
for($i = 1;$i<99;$i++)
{
$i = intval($i);
if ($i / 3 == intval($i / 3))
{
echo "<tr><td>this is third tr</td></tr>";
}
else
{
echo "<tr><td>this is first or second</td></tr>";
}
}
output is something like
this is first or second
this is first or second
this is third tr
this is first or second
this is first or second
this is third tr
this is first or second
this is first or second
this is third tr
is there more elegant way to detect ALWAYS third tr?
are you looking for something like:
<?php
echo '<table>';
for($i = 1;$i<99;$i++){
if ($i % 3 == 0){
echo "<tr><td>this is third tr</td></tr>";
}else{
echo "<tr><td>this is first or second</td></tr>";
};
};
echo "</table>";
?>
http://php.net/manual/en/language.operators.arithmetic.php
Use the modulus operator...
if ( ! (i % 3))
It will return the remainder of the division. You need the parenthesis because of the order of precedence.
CodePad.
Also $i = intval($i) is redundant. $i is already an integer.

PHP echo in rows of three?

I am attempting to show data in rows of three like this (notice the number of items will not always be even):
abcd defg hijk
lmno pqrs tuvw
xyz1 2345 6789
1011 1213
I am struggling to get the logic right to do this (this is in a foreach() loop).
I know I have to have some if($i %3 == 0) logic in there.. But I'm a bit stuck.
Can anyone help me out?
$a = array('abcd','defg','hijk','lmno');
for ($i = 0; $i < count($a); $i++) {
if ($i && $i % 3 == 0)
echo '<br />';
echo $a[$i].' ';
}
It's better to use a for loop as:
// run $i for each index in the array.
for($i=0 ; $i<count($arr) ; $i++) {
// if $i is non-zero and is divisible by 3 print a line break.
if ($i && $i % 3 == 0) {
echo "<br />";
}
// print the element at index $i.
echo $arr[$i].' ';
}
Code in action
Pseudo-code since I don't know PHP (and you asked for the logic which tends to be the same across all procedural languages):
perline = 3
i = 0
foreach item in list:
if i > 0 and (i % perline) == 0:
print newline
if (i % perline) != 0:
print space
print item
i = i + 1
This will both output a line separator before elements 3, 6, 9 and so on (first element being 0) and place whatever desired spacing you want before the second and third elements on each line. You can just use a different value for perline to change the number output on each line.

Categories