I programmed this code but I have a problem that in every row show the same value "1". I would a counter for positioning or number for the row
Position
1
2
3
4
Here the code: what I need to change or what I doing wrong?
case "18":
$html.='<td class="product_tax_name product_list_content product_list_content_'.($j+1)
.' product_list_col_'.($j+1).'">';
for($i=1; $i<=1; $i=$i+1)
{
$html.= $i;
$i++;
}
$html.='</td>';
break;
}
return $html;
You have to increment $j with ++$j
++$j;
$html.='<td class="product_tax_name product_list_content product_list_content_'.$j.' product_list_col_'.$j.'">';
For example purposes
$j = 0;
foreach(range(1,5) as $blah){
echo (++$j)."\n"; //you can ignore the line return "\n"
}
See the example here
Outputs:
1
2
3
4
5
-Note- in order to increment without warning messages you have to first define So like I did in the above example.
Another way to do this is:
$j += 1;
Which is the same as saying
$j = $j+1;
The Crux of your issue is that you are not assigning anything, your just takeing the value of $j adding 1 and outputting it without changing the value of $j. What I showed above are ways to do both the assignment and the modification of the variable.
So if $j was equal to 4 you would get 5 every time. You either set it to 0 or did not define it.
Also we have this mess to contend with
for($i=1; $i<=1; $i=$i+1){
$html.= $i;
$i++;
}
This loops one time, and you wind up with $i=3. Also it's bad form.
for($i=1; $i<=1; ++$i){
$html.= $i;
}
//outputs 1
Now it loops still 1 time but you end with $i=2
UPDATE
If the above for loop is what you mean when you say
$html.= "put here the counter(1,2,3,4,5)"
Then well yea you're only looping 1 time so you will get only one number. Try this,
for($i=1; $i<=10; ++$i){
$html.= $i;
}
//outputs 12345678910 as there is no separator here.
Or even better
$html .= implode(",", range(1,10));
//Outputs: 1,2,3,4,5,6,7,8,10
So to put it all together ( literally )
$html.= "put here the counter(".implode(",", range(1,4)).")";
As for what the "range" is on this, what it counts. Well tell me what this means and I will let you know:
I would a counter for positioning or number for the row
Position 1 2 3 4
Related
i have a homework like this
Imaginative Tree
An imaginative tree grows in a garden. The tree only grows 1 m long in autumn and 2 times the height of the tree in spring. Make a program that determines the highest tree after Y years with the initial height X m.
in 1 year spring come first
i've tried to put multiple symbol inside the loop,
<?php
$holddata =0;
$length =0
function randomtree($year,$firstlength){
for ($i=0; $i < $year ; $i++) {
$holddata= $firstlength* 2;
$length= $holddata+1;
}
echo $length;
}
echo randomtree(2,3);
but the value not increasing ,i expected the $firstlength*2 and add 1 on it then loop them 2 times so the result will be 15 ,( 3*2+1=7 ,7*2+1=15)
You need to use the value of $firstlength as the basis of the start of each calculation and assign the result to it for the next iteration. This code also returns the result rather than echoing out out.
So to simplify your code you can use...
function randomtree($year,$firstlength){
for ($i=0; $i < $year ; $i++) {
$firstlength = ($firstlength* 2)+1;
}
return $firstlength;
}
echo randomtree(2,3);
I have a large form, which when submitted to the database, it needs splitting into odd and even (based on their HTML name), so I can perform a calculation on them.
There are 120 total HTML Input fields, so 60 odd and 60 even.
The for loops that iterates through them are:
$h=0; $o=0;
for($i=1; $i<=119; $i+=2)
{
$h = $h + Input::get($i);
}
for($i=2; $i<=120; $i+=2)
{
$o = $o + Input::get($i);
}
What I am finding is that the odd number for loop is working correctly, but even though the second loop begins at 2, it is skipping adding that Input::get($i); and moving onto the 4th input.
If I echo the odd for loop, it outputs (with all the input values at 1):
for($i=2; $i<=120; $i+=2)
{
echo $i;
echo (",");
$o = $o + Input::get($i);
echo (Input::get($i));
}
2,14,16,18,110,112,114,116,118,
So as you can see, it isn't picking up the '1' value from the 2nd input field.
Any help as to why this is would be greatly appreciated.
You do not need two loops to accomplish this, use the modulo math function to determine if there is a remainder of 0 when dividing by 2 (indicating an even number), try this out:
for($i=0; $i<=120; $i++)
{
if($i%2 == 0) //even
$o = $o + Input::get($i);
else //odd
$h = $h + Input::get($i);
}
You should make a single loop that iterates from 1 to 120. Then test if the counter is odd/even using the modulus operator ($a % $b).
i.e. if $a % 2 = 0 then the value is even else it is odd.
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.
I currently have:
$i = 1;
while {
echo $i;
$i++;
}
And it shows:
1
2
3
4 etc..
How would I make it display backwards?
For example
4
3
2
1 etc..
I basically want to do the exact same thing but flip it around.
$i = 10;
while($i>0) {
echo $i;
$i--;
}
Example - Print number through 0 to 5 with PHP For Loop
for($i=0; $i<=5; $i=$i+1)
{
echo $i." ";
}
In the above example, we set a counter variable $i to 0. In the second statement of our for loop, we set the condition value to our counter variable $i to 5, i.e. the loop will execute until $i reaches 5. In the third statement, we set $i to increment by 1.
The above code will output numbers through 0 to 5 as 0 1 2 3 4 5.
Note: The third increment statement can be set to increment by any number. In our above example, we can set $i to increment by 2, i.e., $i=$i+2. In this case the code will produce 0 2 4.
Example - Print number through 5 to 0 with PHP For Loop
What if we want to go backwards, that is, print number though 0 to 5 in reverse order? We simple initialize the counter variable $i to 5, set its condition to 0 and decrement $i by 1.
for($i=5; $i>=0; $i=$i-1)
{
echo $i." ";
}
The above code will output number from 5 to 0 as 5 4 3 2 1 0 looping backwards.
Good luck! :)
If you want to back-word sr number as per your count of rows in result then use this.
$num_rows = mysqli_num_rows($query);
$x = $num_rows;
$x--;
$i = 4;
while($i > 0) {
echo $i--;
}
I have simple table that has about 80 rows, which I populate dynamically using PHP. What I am trying to do is to layout those rows in chunks for each column. So if I have 80 rows, I would like 4 columns of 20 rows or so, maybe the last column has less or more depending on the total number of rows. The total number of rows can change!
I am having trouble coming up with an implementation method that will not get messy! Anyone know of a simple way that I can implement this.
I have tried using a counter as I loop the data to populate the table and when a multiple of of 20 is reached move to the next block but that didn't work for me as I had extra rows left over.
foreach($indexes as $index){
$counter++;
echo '<tr>';
if($counter > 20){
$multiplier = $counter / 20;
$head = '<td></td>';
for($i=1; $i<$multiplier; $i++){
$head .= '<td></td>';
}
}
if($counter < 20){
$head = '';
}
echo "$head<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
echo '</tr>';
}
Thanks all for any help
I would do :
$nbCols = 4;
$nbRows = count($indexes)/$nbCols;
for($row=0; $row<$nbRows; $row++) {
echo "<tr>";
for($i=0; $i<$nbCols; $i++) {
$index = $indexes[$row + ($i*$nbRows)];
echo "<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
}
echo "</tr>";
}
Wouldn't you want to see the remainder of your division and deal with that also?
if($counter % 20 == 0){
// You've no remainder
}else{
// Do another loop to output the odd rows
}
Or you could % 2 == 0 to see if it's even, and then just multiply the whole result by 10.
Be sure to look at ceil() and floor() also for ensuring your number of rows is a round number.
if you dont mind to have this kind of cell order:
1 2 3 4
5 6 7 8
you can use <div style='float:left'>$cellValue</div> in the loop without use of table.