I want to create this loop
<li class="royalSlide">
<div class="celebFixSlider">
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
</li>
<li class="royalSlide">
<div class="celebFixSlider">
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
</li>
The LI and DIV elements are the first loop, the links are 6 row blocks per the Array.
$imagePath="//site.com/images/celeb1208/";
$productPath="//site.com/product.php?prodref=";
$array = array(
"632_white" => "celeb1208_317_large.jpg",
"631_white" => "celeb1208_316_large.jpg",
"630_white" => "celeb1208_315_large.jpg",
"629_white" => "celeb1208_314_large.jpg",
"628_white" => "celeb1208_313_large.jpg",
"627_white" => "celeb1208_312_large.jpg",
"532_white" => "celeb1208_311_large.jpg",
"531_white" => "celeb1208_310_large.jpg",
"530_white" => "celeb1208_309_large.jpg",
"529_white" => "celeb1208_308_large.jpg",
"528_white" => "celeb1208_307_large.jpg",
"527_white" => "celeb1208_306_large.jpg"
);
$i=0;
foreach ($array as $key => $val)
{
$i++;
echo '
<li class="royalSlide">
<div class="celebFixSlider">
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li>';
break;
}
}
After 6 items, the break just stops the code, it does not pause and recreate the loop within side?? Sorry if I am not clear.
// getting data ready
foreach ($array as $key => $val) {
$array[$key] = array(
"href" => $productPath.$key,
"img" => $imagePath.$val
);
}
$data = array_chunk($array,2);
// printing it out
?>
<?php foreach ($data as $array): ?>
<li class="royalSlide">
<div class="celebFixSlider">
<?php foreach ($array as $row): ?>
<img src="<?=$row['img']?>" />
<?php endforeach ?>
</div>
</li>
<?php endforeach ?>
The break; command will stop the loop completly. Maybe you want continue; that will skip what is after the continue keyword and loop again from where it left off.
What you need is no break and no continue.:
$i=0;
echo '<li class="royalSlide"><div class="celebFixSlider">';
foreach ($array as $key => $val)
{
$i++;
echo '
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li><li class="royalSlide"><div class="celebFixSlider">';
}
}
echo '</div></li>';
Use "continue" instead of "break";
Break will stop the loop
Thanks
So you should use this:
echo '<li class="royalSlide">
<div class="celebFixSlider">';
$i=0;
foreach ($array as $key => $val)
{
$i++;
echo '
<a href="'.$productPath.''.$key.'">
<img src="'.$val.'" />
</a>';
if($i==6){
echo '</div>
</li>
<li class="royalSlide">
<div class="celebFixSlider">';
}
}
Try this for your loop instead
$i=0;
foreach ($array as $key => $val)
{
$i++;
if(($i % 6) == 0){
echo '<li class="royalSlide"><div class="celebFixSlider">';
}
echo '<img src="'.$val.'" />';
if(($i % 6) == 0 && $i!=0){
echo '</div></li>';
}
}
Related
How I can foreach array properly?
Code:
while ($row = mysqli_fetch_assoc($result))
{
if ($use_sef_links == true)
{
$sef_link = "{$row['title']} ({$row['home_team_name']} vs {$row['away_team_name']}) {$row['dt']}";
$sef_link = str_replace("-", " ", $sef_link);
$sef_link = preg_replace('#\s{1,}#', " ", $sef_link);
$sef_link = str_replace(" ", "-", $sef_link);
$sef_link = preg_replace('#[^a-zA-Z0-9-]#', "", $sef_link);
$sef_link .= '_m'. $row['id'] . '.html';
$row['link'] = $sef_link;
}
else
{
$row['link'] = 'match.php?id=' . $row['id'];
}
$matches[$row['season_name']][$row['game_day']][] = $row;
}
foreach ($matches as $match) { ?>
<section class="kode-pagesection margin-bottom-40" style="padding-top: 120px;">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="kode-section-title"> <h2>Coming <?php echo $match['season_name']; ?> season game - <?php echo $match['game_day']; ?> gameday</h2> </div>
<div class="kode-fixer-list">
<ul class="table-head thbg-color">
<li> <h5>Coming games</h5></li>
<li> <h5>Game starts</h5> </li>
<li> <h5>Stadium</h5> </li>
<li> </li>
</ul>
<ul class="table-body">
<li>
<img src="<?php echo $match['home_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['home_team_name']; ?>"> <?php echo $match['home_team_name']; ?>
<span>VS</span>
<img src="<?php echo $match['away_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['away_team_name']; ?>"> <?php echo $match['away_team_name']; ?>
</li>
<li><small><?php echo $match['dt']; ?></small></li>
<li><small><?php echo $match['stadium']; ?></small></li>
<li class="fixer-btn">
<?php echo $label_array[103]; ?>
</li>
</ul>
</div>
</div>
</div>
</div>
</section>
<?php } ?>
If I just print_r($matches); then I can see all seasons and games..
But I cant foreach matches as match..
I put image here to show what it shows if I print matches string.
Image:
https://gyazo.com/ad2ebed217e0507cf315103b52273225
I'd have written this as a comment, but there's not enough space....
Given the structure of your array $matches['season_name']['game_day'][]
I guess you want:
foreach($matches as $seasonName => $season) {
// print the season-header
echo $seasonName;
foreach($season as $gamedayID => $gameday) {
// print the gameday header
echo $gamedayID;
foreach($gameday as $match) {
// print the details from the match
echo $match['title'];
}
}
}
This is the version for grouping by season.
See my comments for explanations!
Too many unnecessary nesting though!
Btw you just need to use foreach (foreach array_expression as $key => $value) and array selector properly. For the inner arrays there'll be no advantage via using foreach, since you've got only one index array_expression as :
(foreach $matches as $matchIndex => $matchDetails){
$currentMatch = $matchDetails[0];
print_r($currentMatch);
// or whatever you want to do with that
}
I have to modify the first item of the array, however I failed, but I manage to do a code:
<div class="carousel-inner">
<?php if($results) {
$x= 1;
foreach ($results as $data) { $x++; ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
</div>
Assuming that I will get these result:
<div class="item active">
<img alt="" src="http://localhost/ideal_visa/uploads/fd5fa6cfbe64c8b68664ddbf0546d81b.jpg">
</div>
<div class="item">
<img alt="" src="http://localhost/ideal_visa/uploads/c43c064de5cb9e751c723eb9791f2107.jpg">
</div>
<div class="item">
<img alt="" src="http://localhost/ideal_visa/uploads/e3137475f6330d52e9cd9c6fc5efba1e.jpg">
</div>
I only have to add active on the first item of the array.
You don't need to do extra $x for this.You can use foreach with key => value and check if it's first index
<?php if($results) {
foreach ($results as $key=>$data) { ?>
<div class="item <?php echo ($key == 0)? 'active':''; ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
Problem
You have put $x=1 before the foreach, then increment it at the start.
That means when you show the first image, $x==2.
Solution
You can either start with $x=0 or move the line $x++ to the end of the loop:
foreach ($results as $data) { ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php $x++; }
You need to increment $x after the iteration
<?php
if($results) {
$x= 1;
foreach ($results as $data) {?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php
$x++;
}
} ?>
<div class="carousel-inner">
<?php if($results) {
$x= 1;
foreach ($results as $data) { ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php $x++; }
} ?>
</div>
you need to increment the x value after the iteration
OR
<div class="carousel-inner">
<?php if($results) {
foreach ($results as $x=>$data) { ?>
<div class="item <?php if($x==0) echo 'active'; ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
</div>
you can just utilize associative key of the array $data and change the condition to $x==0
image folder has 3 files
<img src='images/AAA_1.jpg'>
<img src='images/AAA_2.jpg'>
<img src='images/AAA_3.jpg'>
<img src='images/BBB_1.jpg'>
<img src='images/BBB_2.jpg'>
<img src='images/BBB_3.jpg'>
<img src='images/CCC_3.jpg'>
PHP is
foreach ($carousel as $image) {
if(strpos($image, 'AAA_') === 0){
echo "<div class='box'><img src='images/carousel/$image'/></div>";
}
}
so the output is
<div class='box'>
<img src='images/AAA_1.jpg'>
</div>
<div class='box'>
<img src='images/AAA_2.jpg'>
</div>
<div class='box'>
<img src='images/AAA_3.jpg'>
</div>
How can I add a first class on the fist box, looks like
<div class='box first'>
<img src='images/AAA_1.jpg'>
</div>
<div class='box'>
<img src='images/AAA_2.jpg'>
</div>
<div class='box'>
<img src='images/AAA_3.jpg'>
</div>
Use the key:
foreach ($carousel as $key=>$image) {
$first = $key == 0 ? ' first' : '';
if(strpos($image,'AAA_') === 0){
echo <<<EOD
<div class='box{$first}'>
<img src='images/carousel/{$image}.jpg'>
</div>
EOD;
}
}
Note: I used a heredoc statement for the echo as I prefer to echo multiple tags of HTML over multiple lines.
Simple: Use a flag.
$firstEchoed = FALSE;
foreach ($carousel as $image) {
if(strpos($image,'AAA_') === 0){
echo "<div class='box";
if ( ! $firstEchoed)
echo " first";
echo "'><img src='images/carousel/$image'/></div>";
$firstEchoed = TRUE;
}
}
You can use a boolean flag, or add count param to foreach:
foreach ($carousel as $i => $image)
currently i have this structure
<div class="col">
img1
img2
img3
img4
</div>
<div class="col">
img1
img2
img3
img4
</div>
then when i tried to loop it
i got this results
img[0]
img[1]
img[2]
how can i achieve this structure
<div class ="col"[0]>
img[0]
img[1]
img[2]
img[3]
</div>
<div class ="col"[1]>
img[0]
img[1]
img[2]
img[3]
</div>
Loop code:
<?php
foreach ($images as $k => $v){
$imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture';;
?>
<div class="col" >
<img [<?php echo $k; ?>] src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name">
</div>
<?php
}
?>
In the loop you want to add a <br> or <p> after each image.
Example inside loop
$counter = 0;
$firstTime = true;
$column = 0;
<?php foreach ($images as $k => $v):
$imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture';
if ($firstTime) {
echo '<div class="col'.$column.'" >';
$firstTime = false;
}
if ($counter > 3) {
echo '</div>';
$column++;
echo '<div class="col'.$column.'" >';
$counter = 0;
} ?>
<img src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name">
<?php $counter++; ?>
<?php endforeach; ?>
</div>
I am using a foreach to loop through image. There are maximum four images and minimum 1 image.
For example if there are two image (= two loops) i want to tell the foreach he needs to loop two times again and echo some placeholder pictures.
Heres my foreach:
<?php foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php } ?>
Outputs (two loops):
<img src="/images/user_0.jpg" alt="" title="" />
<img src="/images/user_1.jpg" alt="" title="" />
but the new script should output:
<img src="/images/user_0.jpg" alt="" title="" />
<img src="/images/user_1.jpg" alt="" title="" />
<img src="/images/user_placeholder.jpg" alt="" title="" />
<img src="/images/user_placeholder.jpg" alt="" title="" />
dont forget its possible that $users can have x entries (0-4)
Use array_fill to fill an array with as many items as needed (since they are all going to be identical) and then print them out.
<?php foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php } ?>
<?php
echo implode('', array_fill(0, count($users), 'placeholder image HTML'));
Of course instead of this cuteness you could also use another foreach that prints placeholder image HTML in each iteration.
Update: It turns out there's an even better method:
echo str_repeat('placeholder image HTML', count($users));
PHP really has too many functions to remember. :)
Use a counter...
<?php
$counter = 0;
foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php $counter++;
}
while{$counter < 4)
{?>
<img src="/images/user_placeholder.jpg" alt="" title="" />
<?php } ?>
this should work
$count = 1;
foreach($users as $k => $v) {
?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php
$count++;
}
for ($i = $count; $i <= 4; $i++) {
?>
<img src="/images/user_placeholder.jpg" alt="" title="" />
<?php
}
?>
<?php
$placeholders = array();
foreach($users as $k => $v) {?>
<img src="/images/user_<?php echo $k; ?>.jpg" alt="" title="" />
<?php
$placeholders[] = '<img src="/images/user_placeholder.jpg" alt="" title="" />';
}
foreach ($placeholders as $placeholder){
echo $placeholder;
} ?>
As you can see, there are a dozen ways to skin this particular cat.