nested loop with jquery image slider - php

I'm using a jquery image slider that, display images 3 by 3. All the images are coming from database. This is my code:
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
$y=0;
while($y < 3) {
if($data[$y] != '') {
$imgNew = $data[$y];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
$y++;
}
?>
</div>
</li>
<?php
}
?>
</ul>
And this code is displaying just 1 image, and it's repeating.
plz help me guys. thanks.

Here I got this working... displayed 3x3 and you can check source... each img src increments by one each time. so that will give you 3 X 3.
heres my code, just need to adapt it a little (remove the array I made and replace it with yours.
<?php
$datas = array(1,2,3,4,5,6,7,8,9);
$y=0;
echo "<li>";
foreach ($datas as $data)
{
if($data != "")
{
$y++;
echo "<img src=" . $data . " alt='' />";
if($y % 3 == 0)
{
if($y < count($datas))
{
echo"</li>";
echo "<li>";
}
else
{
echo"</li>";
}
}
}
}
?>
</ul>
if you need any help, please comment.

Try this,
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
if($data[$x] != '') {
$imgNew = $data[$x];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
?>
</div>
</li>
<?php
}
?>
</ul>

Related

Is it possible to close </ul> and add new <ul> in the loop?

I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>

Manipulating a foreach loop

I have the following foreach loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php endforeach; ?>
The first 2 items are fine but the items 3-14 I need to wrap in a div so I can control the layout. I am wondering how I would do this? The problem is not all of the 3-14 items will be populated.
Any advice would be welcome ... thanks
You need a control variable to determine which iteration you are at, from there you can add divs or whatever you want to do from the third iteration onward. You could try something like this:
<?php $i = 0;
foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php $i++;
if ($i < 3) { ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php } else { ?>
Add divs, or whatever you want to do here.
<?php }
endforeach; ?>
So with your additions, since the original formula should work, don't mess with that part. Instead make an addition via a $style variable like so:
$i = 1;
foreach ($this->item->extra_fields as $key=>$extraField):
if($i == 1)
$style = 'id="largeImageWrap" class="pull-left"';
elseif($i == 2)
$style = 'id="sidePanelWrap"';
else
$style = 'class="row"';
// If less than or equal to 3, add <div>
if(($i <= 3))
$front = true;
// If greater than 14 <div>
elseif(($i > 14))
$front = true;
// Else no <div>
else
$front = false;
if($front == true) echo "<div $style>"; ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php
// If greater than 2 and less than 14 echo blank or </div>
echo (($i > 2) && ($i < 14))? "":"
</div>";
$i++;
endforeach;

Order data in three columns instead of one

At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.

Is there an easy way to do 4 at a time in a php foreach loop

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.

Divide php for cycle in 2 columns

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;
}

Categories