$arr = array('cat','dog','spider','snake');
$arrlength = count($arr);
$a = 1;
$i = 0;
while($a <= 3) :?>
<div class="col-sm-4">
<?php for($x=$i; $x < $arrlength;$x++):?>
<div class="checkbox">
<label>
<input type="checkbox" name="name[]" value="" <?php echo $name?>><?php echo $arr[$x]?><br/>
</label>
</div>
<?php endfor; ?>
</div>
<?php $a++; endwhile;?>
How do I increment the value of $i by 3 every increment of $a?
Thanks for your responce.
Try this:
$i = 0;
$i += 3;
or
$i = $i + 3;
I solved it. I added variable y with a value of 3.
then I change the variable arrlength to variable y.
then increment the variable y by 3.
for($x=$i; $x < $y;$x++)
Thank you for your help guys.
Related
I am trying to display last entry as first and tried to use it as array but not able to get it, here is the code I have:
<?php
$maxItem = 30;
for ($i=1; $i<$maxItem + 1; $i++) {
if (have_rows('content_block_'.$i)) {
while (have_rows('content_block_'.$i)) {
the_row();
?>
Current Output
<div class="content_block_1">1</div>
<div class="content_block_2">2</div>
<div class="content_block_3">3</div>
<div class="content_block_4">4</div>
Expected Output
<!-- if new entry -->
<div class="content_block_5">5</div>
<div class="content_block_1">1</div>
<div class="content_block_2">2</div>
<div class="content_block_3">3</div>
<div class="content_block_4">4</div>
<?php
$maxItem = 30;
for ($i = 0; $i < $maxItem; $i++) {
$block_item = $i == 0 ? $maxItem : $i;
while (have_rows('content_block_'.$block_item)) {
the_row();
}
}
You can simply check if value of $i is 0. If yes, go for the last entry, i.e,
$maxItem, otherwise, complete the rest of it sequentially.
I am having trouble figuring out how to write this code.
I have 1000 name db entries and I need to echo the results in sets of 4.
So the set # counts per each 4 entries until end of data.
How can I do this with php?
My result should look like this:
<div id="set-1">
<div>Name1</div>
<div>Name2</div>
<div>Name3</div>
<div>Name4</div>
</div>
<div id="set-2">
<div>Name5</div>
<div>Name6</div>
<div>Name7</div>
<div>Name8</div>
</div>
//and so on......
I would assume that your returned vlaues from your DB are in an array. I made a mock array just to mimick an array with 1000 entries.
<?php
$mockNameArray = array();
for ($i = 0; $i < 1000; $i++)
{
$mockNameArray[$i] = "Name" . $i;
}
?>
Since you have a set number returned from your DB it is easy to run a for loop instead of a foreach loop. I would do something like this.
<?php for ($i = 0, $x = 1; $i < 1000, $x < 251; $i++, $x++): ?>
<div id="set-<?php echo $x; ?>">
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; ?></div>
</div>
<p>
<?php endfor; ?>
How do I merge two (for) in one code?
e.g first code
for ($i = 0, $n = count($options); $i < $n; $i ++) {}
and second code
for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++)
here is oll code
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_('index.php');?>" method="post" name="poll_vote_<?php echo $poll->id;?>" id="poll_vote_<?php echo $poll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="mod_voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="mod_voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?> <?php for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++) { ?>
<a href="<?php echo $params->get($u); ?>" onClick="return popup(this, 'notes')">
<img src="/images/stories/add.png" alt="play"></a>
<?php
} ?
I want in one poll show onle play button
now shows this
here is my xml code:
You don't really need that second for. You already have an index with $i from your first loop. (Except that it starts from 0 instead of 1, but you can easily get it working)
Remove the second for, and try this :
// echo $params->get($u);
echo $params->get('rel_article' . ($i + 1));
I created the available seat with a for loop below:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And I want to add a booked class if the $i variable matches with the $k variables which taken from the booked seat array elements below:
$booked_seat = array('1','4','5','6','7');
the result:
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
So I did this:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo ($i == $booked_seat[$i-1]) ? 'booked' : '' ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And i get the offset Error because the $booked_seat is not much as the $seats loop, how to limit the loop so it will not offset?
Thank you before
Try this:
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo (in_array($i, $booked_seat)) ? "booked" : "" ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
warning: code is untested.
You could always flip the $booked_seat array, then test for the existence of the array element using isset(). Something like this:
<?php
$seats = 7;
$booked_seat = array('1','4','5','6','7');
$booked_a = array_flip($booked_seat);
for ($i=1; $i <= $seats; $i++) {
printf("seat: %d%s\n", $i, isset($booked_a[$i]) ? " booked" : "");
}
(This was my test, you can add your HTML to suit.)
Example: http://3v4l.org/5npsm
You should use the method sizeof().
So:
$num_seats = sizeof($seats);
Then a for loop with this.
for($i = 0; $i < $num_seats; $i++){
//code
}
I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?
<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically
I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}