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
}
Related
$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.
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 have this php foreach loop
<?php $all_news = $this->db->query("Select * from news"); ?>
<div class="inner">
foreach($all_news as $key=>$news){?>
<div class="news <?php echo ($key%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
But the problem is the $all_news may have 20 results or so but the design only allows me to put for 4 news blocks in each inner div...is there a way make this happen so i have only 4 news divs in each inner div
<?php
$all_news = $this->db->query("Select * from news");
echo '<div class="inner">';
$c = count($all_news);
for($i = 0; $i < $c; $i++){
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
if($i % 4 == 3)
echo '</div><div class="inner">';
}
echo '</div>';
?>
Change your query to only return 4 rows:
SELECT * FROM news LIMIT 4
Alternatively you can change your for-loop.
for($i = 0; $i < min(4, count($all_news)); $i++)
{?>
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$i]['title'];
<?}
[edit]
See what you mean now. Create two loops:
<?
$index = 0;
while ($index < count($all_news))
{
$news = $all_news[$index];
?>Start outer div<?
for ($item = 0; $item < 5; $item++)
{
?>Inner div with news item <? echo $news['title'];
}
?>End outer div<?
$index++;
}
You could use two for loops:
<?php $all_news = $this->db->query("Select * from news"); ?>
<?php for($i = 0, $l = count($all_news); $i < $l; $i+=4): ?>
<div class="inner">
<?php for($j = $i; $j < $i+4; $j++): ?>
<div class="news <?php echo ($j%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$j]['title'] ?>
<?php endfor;?>
</div>
<?php endfor;?>
Another option would be array_chunk [docs].
The laziest way would be to just check whether or not you've already done four in the current div on the fly. If you have, close the current div and start a new one:
<div class="inner">
<?php
foreach ($all_news as $key => $news) {
if ($key % 2) {
$oddEven = 'odd';
} else {
$oddEven = 'even';
if ($key && $key % 4 === 0) {
echo '</div><div class="inner">';
}
}
echo "<div class=\"news $oddEven\">";
// ...
}
?>
</div>
Note that this assumes $all_news has an element at 0, so it makes sure that it doesn't close the first, empty div.
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;
}