php if iterator divisible by statement for dynamic columns - php

Im trying to make dynamic column list that will be 4 columns total (PHP). Im echoing an array and after every time 4 array items are echod, I would like to wrap those 4 array items in a div called "column".
So basically, I thought I could do this with a self counting $i++ statement, but first off I'm having trouble starting the count from anything but zero (I tried setting the variable initially outside of the for each loop.)
Anyways, ya, if you could kindly show me how to check to see if the $++ is divisible by 4 in php, so that I can insert a if $i++ is divisible by 4 then echo "" , it would be much appreciated. But first I believe I need to figure out how to start the counting at 1 (so that the if $i++ is divisible by 4 would work... right??)

If you divide by 4 it will be integer division and the quotient will be the factor of 4. What you probably want is the modulus operator %. It will give you the remainder. So when $i is a multiple of 4 it will be 0.
if (($i % 4) == 0) {
// evenly divisible by 4 logic
}
Modulus can be inefficient. Since you are dividing by a multiple of 2, you could shift bits right by 2. It is the same as dividing by 4 and much more efficient. Check out bit shifting.

% is the modulus operator. It returns the remainder after a division, so 7 % 4 == 3.
You really should start from 0. This is because 0 % 4 == 0 and 4 % 4 == 0... AND you want the very first item to be a new row! So you want new rows on 0, 4, 8, etc... If you start at one, then 1, 2, and 3 would not be in a row.
Additionally, we have to remember to close the row on every item one before a new row.
Finally, if we exit our loop without closing the final row, we have to do that after we have exited the loop.
I'll show how to do this with tables, but you can use divs with classes just as easilly.
<table>
<?php
// Start at zero, so we start with a new row
// Since we start at 0, we have to use < number of items, not <=
for ($i = 0; $i < $numberOfItems; ++$i)
{
// if divisible by 0 start new row
if ($i % 4 == 0)
echo '<tr>';
// Always print out the current item
echo '<td>' . $item[$i] . '</td>';
// If the next $i is divisible by 4, we have to close the row
if ($i % 4 == 3)
echo '</tr>';
}
// If we didn't end on a row close, make sure to close the row
if ($i % 4 != 3)
echo '</tr>';
?>
</table>

Modulus!
$a % $b Remainder of $a divided by $b.
http://php.net/manual/en/language.operators.arithmetic.php

Related

PHP : Conditional based on the items hierarchy in array

I have this array, I want to make conditional action based on the items hierarchy in the array. I'm going to compare between items based on their hierarchy and it will result a 2 dimensional array like this.
The comparison is : Imagine that there are 3 stair, in the highest stair is 5, the second stair is 3 and the last stair is 1.
If a number compared with the 1st lower floor then it'll be 3 and if a number compared with the 2nd lower floor then it'll be 5. Example : if 5 is compared with 3 then it'll be 3, if 5 is compared with 1 then it'll be 5. If a number compared with the 1st upper floor then it'll be 1/3 and if a number compared with the 2nd upper floor then it'll be 1/5. Example if 1 is compared with 3 then it'll be 1/3 and if 1 is compared with 5 then it'll be 1/5. And if 3 is compared with 5 then it'll be 1/3 and if 3 is compared with 1 then it'll be 3.
So far, this is my code
$weightvalue = array(5,3,1);
$numbers = count($weightvalue);
for ($row = 0; $row < $numbers; $row++) {
for ($column = 0; $column < $numbers; $column++) {
echo "$weightvalue[$row],$weightvalue[$column] ";
if ($weightvalue[$row]==$weightvalue[$column]) {echo "1";}
elseif ($weightvalue[$row]<$weightvalue[$column]) {echo "1/3";}
elseif ($weightvalue[$row]<$weightvalue[$column]) {echo "1/5";}
elseif ($weightvalue[$row]>$weightvalue[$column]) {echo "3";}
elseif ($weightvalue[$row]>$weightvalue[$column]) {echo "5";}
else {echo "false";};
echo ("\n");
}
}
So far this is the result, but there're some mistakes, I know what causes this but I don't know how to fix it. Please help, any suggestion will very much appreciated.

How to keep looping in while loop?

I have a table with five rows for example, and my while loop will display the five rows for me as per normal.
Iā€˜m trying to get the loop to keep looping records until let's say $count = 12. I want to start the counter at 0 and it will end at 12. So, I pretty much want the loop to keep showing the same records randomly until it shows me 12 or so results and then stops.
Simple as while ($a != 12)
$counter = 0;
while ($counter != 12) {
// Do stuff
$counter = rand(0, 24);
}

How does for loop works actually in php?

I'm a newbie to php and I've understood all other loops in php but the question is I cannot understand how the for loops works for ex:
Here is the code;
$a = 0;
$b = 0;
for ($i=0; $i < 5; $i++) {
$a += 10;
$b += 5;
}
echo("At the end of the loop a=$a and b=$b");
When I execute this script the value of a = 50 and b = 25!
Is it multiplying the a value with i's increment value? like 10 * 5 = 50.
You start with $i=0, then you do $a+10 and $b+5 as long as $i <5
$i=0, $a=10, $b=5
$i=1, $a=20, $b=10
$i=2, $a=30, $b=15
$i=3, $a=40, $b=20
$i=4, $a=50, $b=25
$i=5, now the loop stops because $i is no longer <5
Your loop runs five times. Each time through the loop you add 10 to the value of $a. Doing that five times gives you 50.
This is the way it works.
Let's say you have a no dollars. And I tell you that I will give you a dollar everytime you do 5 chores. However, I will only give you 5 dollars. At first you have no dollars and after one chore I give you 5 dollars. Now you have 5 dollars. You do another chore and I give you another 5, bringing you to ten. I have now given you two dollars. I give you another 5 - you have 15. I give you another - 20 - and one more; bringing you to 25 dollars. Now I have given you my limit of dollars, and our loop is complete.
In this story, my dollars are your $i value. Beginning at 0, working up to 5. Your chores are your $b values, which are added to every time.
Code example:
for ($dollars=0; $dollars < 5; $dollars++) {
$chores += 5;
}
+= is not increment its an assignment operator.
http://php.net/manual/en/language.operators.assignment.php
As I mentioned in the comments, and others in their answers. += will add the value to on the right to the value on the left. so in a loop its like this
step 0 $a = 5 ( 0+5)
step 1 $a = 10 (5[previous iteration exit value]+5 )
step 2 $a = 15 (10[previous iteration exit value] + 5)
so on....
Of note is you can also do -= *= etc. and .=or append all the same kind of operation.
The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax:
for (initialization; condition; increment){
code to be executed;
}
The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.
Example
The following example makes five iterations and changes the assigned value of two variables on each pass of the loop āˆ’
<html>
<body>
<?php
$a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ ) {
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>
</body>
</html>
This will produce the following result āˆ’
At the end of the loop a = 50 and b = 25
For loop is entry condition loop. It evaluate condition first, so the statement block associated with the loop won't run even once if the condition fails to meet
The statements inside this for loop block will run 5 times, the value of $i will be 0 to 4;
Imagine the loops are running separately.
$a=0;
$b=0;
for ($i = 0; $i < 5; $i++){
echo $a += 10;
echo '<br>';
}
And the output like this.
10
20
30
40
50
Now another Loop
for ($i=0; $i < 5; $i++) {
echo $b += 5;
echo '<br>';
}
And the output like this
5
10
15
20
25
In each iteration it adds 10 and 5 to previous number of iteration using the assignment operator x += y.

condition not working in nested loop in php

for ($x = 0; $x <= 3; $x++) {
for ($y = 0; $y <= 4; $y++) {
if ($y == 0) {
mysql_query("insert into tb_weight_rate_management
(nation,zone_id,rate) values ('Domestic',1,'$del_100')");
}
}
}
hello i am little troubled using for loop....in the code above i have two loops ...1st loop will work 3 times and the the inside loop will work 4 times....
now when i click on submit button then it checks loop one and then enter second loop and inserts data 4 times in the database....which is wrong...i want if $y=0;
then it should insert data only once but it is inserting data 4 times can anyone please correct the above condition
You should use == instead of =. Like this:
if ($y == 0) {
the first loop runs 4 times, hence 4 inserts
In the inner loop you declare $y = 0 on every iteration of the outer loop
for each x iteration, there are one time y=0. and since x executes 4 times (0, 1, 2 and 3) y gets 0 value 4 times as well. if you want to do the insertion only once then you must add x value as well like if($y == 0 and x == 0).

PHP + Echo Data Routinely

I have the following PHP code working 'successfully' to display URL's:
<?php
for ($i = 0; $i <= $json->domaincount; $i++) {
echo '<td class="domainList">'.$json->$i.'</td>';
}
?>
Every forth echo I want to also
echo </tr><tr>
to start a new line in the table.
Is there an easy way to know which is every forth count?
I have $i which increments from 0 up so when it gets to 3, 7, 11 etc I need to change table line.
thx
Try this:
if ( ( $i + 1 ) % 4 == 0 ) { echo '</tr><tr>'; }
This is using the modulus operator. It divides a number and returns the remainder, so 7 % 4 = 3 (because 4 fits in once, and three is left over) and 8 % 4 = 0 (because 4 fits in evenly and there are no left overs)
Look into modulo operands (% in PHP). So the check that i % 4 == 0 would give you every fourth row.
<?php
for ($i = 0; $i <= $json->domaincount;) {
echo '<td class="domainList">'.$json->$i.'</td>';
if (!(++$i & 3))
echo '</tr><tr>';
}
?>
!($i & 3) is just a quick way of writing $i % 4 === 0 without having to use modulus. You can only use that trick for modding by powers of 2.

Categories