Repeating an HTML element with incrementing variables - php

I currently have a simple html list. What I want is to repeat this list item with each repeated element having a variable that will increase by one, each time it is repeated.
The php calls that i have included in the above list item is from the Advanced Custom Field plugin on WordPress. The variable I want to change is the number inside of those php calls.
<ul>
<li>
<?php the_field('vendor_1_name'); ?> <!--the number is the variable that i want to increase by one with each repition-->
<a href="http://<?php the_field('vendor_1_url'); ?>" target="_blank"/>
<?php the_field('vendor_1_url'); ?>
</a>
</li>
</ul>
I have an idea of how to do this using an array, but I would love to hear other methods on how to do something like this. Thanks!

<ul>
<?php for ($z = 0; $z < $n; $z++) : ?>
<li>
<?php the_field('vendor_'.$z.'_name'); ?>
<a href="http://<?php the_field('vendor_'.$z.'_url'); ?>" target="_blank">
<?php the_field('vendor_'.$z.'_url'); ?>
</a>
</li>
<?php endfor; ?>
</ul>
Where $n is the number of times it should iterate.

Use a for loop. If you have an array of items (are you grabbing vendors from a database?), just iterate through the array:
<?php
// fetch values and define $vendors
// using sql is highly recommended
echo "<ul>\n";
for ($i = 0; $i < count($vendors); $i++) {
echo "<li><a href='".$vendors[$i]['vendor_url']."'>".$vendors[$i]['vendor_name']."</a></li>\n";
}
echo "</ul>\n";
?>
Note that the array would say vendor_name instead of vendor_1_name because the field is not going to have the iteration in its name.

Related

how to put div in a for loop

For parsing a list of articles, i have this code to parse all the articles:
while($article = $articles->fetch())
{
$date = strtotime($article['createdAt']);
$formatted_date = date("F Y",$date);
?>
<br />
<div class="news-content">
<div class="news-image">
<?php echo $article['title']; ?>
</div>
<div class="news-article">
<h3>
<span><?php $date = strtotime($article['createdAt']); echo /*date("F j",$date);*/ strftime('%e %B',$date) ?></span>
<br />
<?php echo $article['title']; ?>
</h3>
</div>
</div>
<?php
} //end while loop
?>
What i want to achieve: only the first 5 <div class="news-content">...</div> should be shown.
I know i have to do something with a for loop
but i do not know exactly how to use the for loop for this situation...
Can someone help me with that?
There are a lot of different ways to limit a loop. One possibility is to use a for loop instead of a while loop. for is often a good option if you want something to happen a specific number of times. Adding something else like fetch into the continuation condition will mean it happens up to a specific number of times.
for ($i = 0; $i < 5 && $article = $articles->fetch(); $i++) {
// output article
}

How to control foreach loop iteration in php

I want to fetch some data from database in the form of list. Following is my loop:
<div class="col-sm-4">
<ul>
<?php
foreach($subjects as $s)
{
echo "<li>$s->subject_title</li>";
}
?>
</u>
</div>
Now this loop prints data in one column and the user have to scroll down to view the last elements. I want to control the loop in such a way that after printing first 10 elements, it must start again from the div tag and print the next 10 elements and so on. I want to show the data in such a way that the user don't have to scroll down.
For example, I want to print the data in the following way:
I write the following code but it is not working. The div tag is only applied to the first 10 elements, and not on the rest of the data.
<div class="col-sm-6">
<ul class="filter-list">
<?php
$i = 0;
foreach($main_subjs as $i=>$ms)
{
if($i == 10) {
echo "</ul></div><div class='col-sm-6'><ul>";
$i++;
continue;
}
echo "<li><a href='#'>$ms->mains_title</a></li>";
}
?>
</ul>
</div>
Please Help.
As an arugment for a loop you can also use the key of array
foreach($subjects as $i=>$s)
When you will be able to "control" your loop, e.g.:
<div>
<?php
foreach($subjects as $i=>$s) {
if($i> 0 && $i % 5 == 0) {
echo "</div><div>";
}
echo "<li>$s->subject_title</li>";
}
?>
</div>

How can I replicate this functionality with PDO?

I'm switching a site that used to use mysqli to PDO. I have page that generates a <li> menu, split into two columns (ie two container divs).
In the mysqli version, I have the following code - $num is the number of rows returned by my query, $recsToOutput is half that number:
<div class="6u">
<ul>
<?php //Loop through first half of table and output name and link...
for($i=1;$i <= $recsToOutput; $i++){ // for the first record to the half the number of rows...
echo '<li><a href="#">';
echo '<h3>'.$row['menu_item'].'</h3>';
echo '</a></li>';
$row=mysqli_fetch_array($r,MYSQLI_ASSOC);
}
?>
</ul>
</div>
<div class="6u">
<ul>
<?php //.. loop through remaining entries and output
for($i=$recsToOutput; $i < $num; $i++){ // for half the number of rows to the full number of rows...
echo '<li><a href="#">';
echo '<h3>'.$row['menu_item'].'</h3>';
echo '</a></li>';
$row=mysqli_fetch_array($r,MYSQLI_ASSOC);
}
?>
</ul>
</div>
Here, the mysqli_fetch_array calls moved the array pointer of $row on one in order to grab the next menu item. However, I obviously can't use that with PDO: how can I replicate the functionality (or do it better?)

why am I getting unexpectedly repeated results from my php file?

Yes, I've looked around and yes I've used mysql_fetch_array hundreds of times successfully before. I want a php file that will output an html file with some constants and a few interpreted php variables. After an hour, this is where I'm at.
<?php
require "(this is sql statement to get all rows from the db).php";
$i = 1;
while($row = mysqli_fetch_array($results_getpieces) && $i < 11 ){ ?>
<li>
<a href="#image-1">
<img src="<?php echo $row['content']; ?>" alt="image01">
<span><?php echo $row['title']; ?></span>
</a>
<div class="lb-overlay" id="image-1">
<img src="<?php echo $row['thumb']; ?>" alt="image01" />
<div>
<h3>blahblah <span>bluelue</h3>
<p>sherpas don't dance</p>
Prev
Next
</div>
x Close
</div>
</li>
<?php
$i++;
}
?>
When I display the $row results in a table, there's no problem. Before I added that i++ business, I could get the first record from my results, but it would just repeat those results for all ten instances.
Help almighty stackers, and overflow me with your knowledge. Please. I can't go to sleep until I get this. Where am I going wrong?
You need to parenthesize the condition appropriately, so that the array that you get back from the function call doesn't get "and"-ed with the condition "$i < 11":
($row = mysqli_fetch_array($results_getpieces)) && $i < 11

Looping through 2 different arrays to check if a value exists

Ok well i have this array that consists of 3 arrays of objects so its looks like
$invoice
$invoice->foo
$invoice->foo->bars
$invoice->bars
$invoice->foobars
i have all the foo bars displayed in a table like
<?php foreach($invoice->foo->bars as $bar) { ?>
<tr>
<td>
<?php echo $bar->some_field;
</td>
<td>
<?php echo $bar->another_field;
</td>
</tr>
and i have a dropdown button at that looks like
<ul>
<li>edit</li>
<li>Delete</li>
<li>Add foobar</li>
</ul>
Would i am currently checking to see if the bar has a foobar by doing this
for ($i=0; $i < count($invoice->foobars); $i++) {
$foobar_bars[] = $invoice->foobars[$i]->foobar_bar_id;
}
and inside the $invoice->foo->bars loop i check to show the button like
<ul>
<li>edit</li>
<li>Delete</li>
<?php if(!in_array($bar->dd_id, $companion_bars)){ ?>
<li>Add Foobar</li>
<?php } ?>
</ul>
I was wondering if this is the most efficient way to do this or is their better ways to check whether the bar has a foobar
I think the most efficient way would be to actually have a method that tells you whether or not this is the case:
if ($bar->hasCompanionBar()) {
// the condition is TRUE
}
That way you can defer the decision what is most efficient to later. That will allow you to create the most efficient code over the lifetime of the codebase.

Categories