Showing last entry as first PHP - php

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.

Related

Check If bootstrap last column is even or odd

I have bootstrap column in while loop, what I want is if bootstrap last column is odd then I want column to be 12 (col-12), I found the way to check number even or odd but want to check last number so if last number (last column) is odd I want column to be 12 else remain col-6
I have tried:
<div class"<?php echo ($i == (2 || 4 || 6) )?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
<div class"<?php echo ($i % 2 == 0)?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
but could get the job done...
You make a mistake in writing HTML. But version with modulo must work:
<div class="row">
<?php for ($i = 0; $i < 100; $i++) { ?>
<div class="<?php echo ($i%2 === 0) ? 'col-md-6' : 'col-md-12'; ?>">
display content there in while loop
</div>
<?php } ?>
</div>
solution By m_hutley
Where $i = counter;
$total = acf repeater count;
<div class="<?php echo ($i == $total && $total % 2 !== 0 )?'col-md-12':'col-md-6'; ?>" id="<?php echo $i; ?>">
<h1><?php echo "contain"; ?></h1>
</div>

PHP continues loop inside loop

I have a repeated row which have 5 columns. I want when every time row is looped column data is looped number but continues after every loop. Sample code:
$list = 0;
$list++;
for ($i = 0; $i < 5; $i++ ){
<div class="row">
<div class="col-2">$list</div>
<div class="col-2">$list</div>
<div class="col-2">$list</div>
<div class="col-2">$list</div>
<div class="col-2">$list</div>
</div>
}
Sample result what i want:
<div class="row">
<div class="col-2">1</div>
<div class="col-2">2</div>
...
<div class="col-2">5</div>
</div>
<div class="row">
<div class="col-2">6</div>
<div class="col-2">7</div>
...
<div class="col-2">10</div>
</div>
Any idea?
Use a nested loop to iterate through both your rows and columns while keeping a counter outside of the loop:
$counter = 1;
for ($rowCount = 1; $rowCount < 5; $rowCount++ ) {
echo '<div class="row">';
for ($colCount = 1; $colCount < 5; $colCount++ ) {
echo '<div class="col-2">', $counter, '</div>';
$counter++;
}
echo '</div>';
}
Fiddle: Live Demo
Simply increment the counter $list as part of the output
<?php
$list = 1;
for ($i = 0; $i < 5; $i++ ){
echo "<div class='row'>
<div class='col-2'>$list++</div>
<div class='col-2'>$list++</div>
<div class='col-2'>$list++</div>
<div class='col-2'>$list++</div>
<div class='col-2'>$list++</div>
</div>";
}
Using $list++ it will output the current value and then increment it by one ready for the next line etc etc
You can use two loops. One for how many rows($i) you have second one for iteration (1,2,3,4,5) ($list)
<html>
<body>
<?php
$list = 1;
for ($i = 0; $i < 5; $i++ ){
echo "<div class='row'>";
for ($list = 1; $list < 6 ; $list++) {
echo "<div class='col-2'>$list</div>";
}
echo "</div>";
}
?>
</body>
</html>

show group of sets using foreach

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 to select an specific custom field

im trying to select a custom field in this case the 10th field so i can change the input type from text to date, but i not quite sure how to accomplish this, here is my code.
<?php for ($i = 0; $i < 11; $i++)
{
?>
<?php
if($this->config->item('custom'.$i.'_name') != NULL)
{
$item_arr = (array)$item_info;
?>
<div class="field_row clearfix">
<?php
echo form_label($this->config->item('custom'.$i.'_name').':', 'custom'.$i,array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);?>
</div>
</div>
<?php
}
}
?>
i have 10 custom fields, i select 10th cause i need to set date picker (right now i got the form_password) , i tried this but well the 10th shows but doesn't work as desire, syntaxis errors and it outputs all 10 fields and not the ones i selected only as example
<?php for ($i = 0; $i < 11; $i++)
{
?>
<?php
if($i < "9")
{
$item_arr = (array)$item_info;
?>
<div class="field_row clearfix">
<?php
echo form_label($this->config->item('custom'.$i.'_name').':', 'custom'.$i,array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);?>
</div>
</div>
<?php
}
else {
echo form_password(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);
}
}

PHP limit the loop based on available array element

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
}

Categories