This is probably a stupid gotcha that I'm overlooking but I'm hoping one of you can help me!
I've got a loop to list a grid of Products in my DB.
So far so good, everything is displaying roughly OK except this one little issue.
Within a list I'm doing the following:
<ul>
<?php $i=0; foreach ($products as $product) : $i++; ?>
<li <?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
// Then I've got the image thumbnail etc coming in....
All looks good except for the LAST row...
So for instance if I have 8 products... the first 7 will display on the page correctly, but then there is a gap at the end where the 8th product moves onto the next page.
At first I thought it was CSS widths or something but it's not. Even if I have 20 products...always the last row only shows 3 across and puts the last product on the next page.
Any ideas anyone?
Cheers M
<ul>
<?php $i=count($products); ?>
<li
<?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
Let's try it..
Related
This should be relatively straightforward but none of the solutions I've found here or elsewhere work at all.
I'm trying to display a table containing all the elements of a list of brands. I added two manually (Adidas and Nike) just to test the display, and these show correctly. I then tried to generate a list using an array of brands (the array brandList has two items, id and name). The output is garbage.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach ($brandList as $item) {
$text = '<li>'.$item[1].'</li>';
echo("$text\n");
}
?>
</ul>
expected output:
Adidas
Nike
Puma
Asics
actual output
Adidas
Nike
'.$item[1].''; echo("$text\n"); } ?>
I've tried formatting a dozen different ways (with single and double-quotes, backslashes etc.) but it always just displays half of the line of code instead of the variable. I know it's something simple, but I've spent too much time messing with it for no result.
One way to do this is to let PHP generate the HTML instead of echoing the string. I think that's more clear than echoing.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach ($brandList as $item) {
?>
<li><?=$item[1]?></li>
<?php
}
?>
</ul>
A lot is wrong with your snippet and frankly I am not surprise it doesn't work.
You are using the wrong syntax in your foreach loop.
The <li></li> you are creating has no opening tag just an anchor tag <a></a> and a closing list item tag </li>.
$test is a variable with quotes in it, using quotation marks around it during echo is just wrong as the " in your href will close the first quote around the variable outputting others as a string rather than HTML tags.
Though not so important but ditch the \n character. Your browser will render the list in block format with or without it.
Change your snippet to look like this instead
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach($brandList as $item) {
?>
<li><?=$item[1]?></li>
<?php
}
?>
</ul>
If you prefer your current syntax then change it to this
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
foreach($brandList as $item) {
$text = '<li>'.$item[1].'</li>';
echo($text);
}
?>
</ul>
Hi I think you have missed starting li tag in side the loop.
Please check this for your solution.
<ul class="brand-list">
<li>Adidas</li>
<li>Nike</li>
<?php
$text="";
foreach ($brandList as $item){
$text .= '<li>'.$item[1].'</li>';
}
echo($text);
?>
I have seen some solutions already for tables but for some reason the break also duplicated which only need to be shown once
i have the following code:
<?php
while($result=mysqli_fetch_array($query)){ echo "<div class='col-lg-12'><div class='panel panel-default'>".$result['brand']." ".$result['modelyear']." ".$result['type']."<img src='".$result['link_image']." heigth:'50px' class='img-responsive'></div></div>";}
?>
I would like to try to break it after 10 rows and than show a div with a banner in it.
i tried the following PHP add html break after every 10th mysql result
however the banner also multiplies it self.
and yes i am a noob
Regards
Bas
You can make a variable that increment every time the loop execute. After the condition met you can display your div and end the loop. Something like this.
$i=0;
while($result=mysqli_fetch_array($query)){
if($i % 10 == 0){
echo "<div>Banner Div</div>";
} else {
echo "<div>Normal Div</div>";
}
$i++;
}
I'm trying to pull information from instagram and twitter, and display the information as a collection of square and rectangle boxes arranged on screen.
I was using a foreach statement to display the content, but because the containing divs are not consistent in size, I have to end the foreach statement and start a new one. The content of the new foreach is exactly the same as the content of the previous. I'm not sure if I'm going about this the right way and would appreciate any push in the right direction.
The block of code below displays the first 4 most recent instagram photos.
<?php $i = 0; foreach ($instagram_data->data as $latest_post): if (++$i == 5) break; ?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php endforeach ?>
After that, I display the latest twitter content.
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
And then I repeat another foreach similar to the first to display more instagram content. This however repeats the exact same content from the code above (shows the latest 4 photos instead of the 4 photos after the original photos).
You could put a variable in the foreach to make the twitter div if it's the fifth, like this
<?php
$i = 0; foreach ($instagram_data->data as $latest_post){
?>
if ($i==5){?>
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
$i=0;
}
else{?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php
}
$i++;
} ?>
I have created a newsfeed in my magento site using Zend Framework. This works almost perfectly except, I want to retrieve only the first 3 entries on the rss. If I try to do this, the first 3 items are displayed on my site but the foreach loop continues to execute so excess spaces and html elements are added in my site. How can I retrieve only the first 3 entries of the rss? Here's how my code looks:
<?php $i = 0;
<?php $channel = new Zend_Feed_Rss('http://mydomain/newsfeed'); ?>
<?php foreach ($channel as $item):
<div>
if($i<3): ?>
<label>My feed title is: <?php echo $item->title; ?>
<?php endif; $i = $i + 1; ?>
</div>
<?php endforeach; ?>
I have about 10 entries on the newsfeed so if I execute something like this, I get the first 3 properly, then I get 7 excess labels with My feed title is:. I tried, using break but this broke my entire page so I can't use that. Can someone please guide me to the right direction?
you can try with for loop instead of foreach
for($i=0; $i<=min(3, count($channel->title)); $i++) {
$feed_title = $channel->title[$i];
// do something
}
hope this will sure solve your issue.
I have my data being output to a span currently... this is how it looks:
Now, when i remove the span and place a div there i am given this output:
This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. How would I do this? I have searched everywhere online but can't seem to find anything that shows a solution.
I did read that some use javascript for the format but i am still clueless on even this option.
My desired output would look like this:
If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other.
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
etc.
If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column.
Also be sure to clear the floats afterwards, so like this:
<div style="clear:both"></div>
Sometimes this is necessary as there will be random issues if this is not put there.
What you are describing can be solved with styling only. You have several divs that must be displayed in columns. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
Also as other answers mentioned, don't use duplicated ids. Always use classes. If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on.
This should work
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<?php echo $key; ?>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>