I have trouble to concatenate img src string.
Instead of $row['img1'] I need $row['img$i']
How would I do correct concatenation using $i?
<?php $i = 1; ?>
<?php foreach ($rows as $row): ?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<img src="<?php echo '/img/post/' . $row['img1'].'.jpg'; ?>" alt="<?php echo $myrow['title']; ?>" />
</div>
<?php $i++; ?>
<?php endforeach; ?>
You can just use $row['img'.$i] or $row["img$i"] . String concatnation can be still used.
Related
I have a html code , when i convert into php some issues happended . Actually i need a foreach or while loop for the below div.
But all images are different (never mind the names of images, it can be any name).
<div class='img'>
<img src='img/1.jpg'>
<img src='img/2.jpg'>
</div>
<div class='img'>
<img src='img/3.jpg'>
<img src='img/4.jpg'>
</div>
//....more divs like this
Here my try
$stmt=$db->query("Select * from photo");
foreach ($stmt as $row)
{
?>
<div class='img'>
<img src='img/<?php echo $row['image']; ?>'>
<img src='img/<?php echo $row['image']; ?>'> // how to get this image diffent from above images
</div>
<?php }
Any experts?
Try that:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM photo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php $i = 0; ?>
<?php $response = array(); ?>
<?php foreach ($data as $dataIndex => $dataValue): ?>
<?php if ($i == 1): ?>
<?php $response[] = '<img src="img' . $dataValue["image"] . '"/>'; ?>
<?php $response[] = "</div>"; ?>
<?php $i = 0; ?>
<?php else: ?>
<?php $response[] = "<div class='img'>"; ?>
<?php $response[] = '<img src="img' . $dataValue["image"] . '"/>'; ?>
<?php $i++; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if(count($data)%2): ?>
<?php $response[] = "</div>"; ?>
<?php endif; ?>
<?php echo implode("", $response); ?>
output:
<div class="img">
<img src="img1">
<img src="img2">
</div>
<div class="img">
<img src="img3">
<img src="img4">
</div>
<div class="img">
<img src="img5">
<img src="img6">
</div>
Assuming $stmt is an array, you can do this.
<?php
$count = 0
while(sizeof($stmt) > $count) {
?>
<img src="<?php $stmt[$count]['image']?>"/>
<?php
$count++;
?>
<img src="<?php $stmt[$count]['image']?>"/>
<?php $count++; ?>
}
?>
I've got a forech loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php foreach($this->entries as $entry): ?>
<figure class="image_container">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php endforeach; ?>
</div>
</div>
I hope you guys can help me.
A simple counter could help -
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php
$i = $j = $k = 0;
foreach($this->entries as $entry):
$i++;
$class = '';
if($j === 0) {
$class = 'first';
}
$j++;
$html = '';
if($i % 5 === 0) {
$k++;
$j = ($i - (5 * $k));
$class = 'last';
$html = "</div></div>
<div class='autogrid_wrapper cte block'><div class='inner'>";
}
?>
<figure class="image_container <?php echo $class; ?>">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php
echo $html;
endforeach;
?>
</div>
</div>
I have the follow foreach loop:
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=1;
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count++; }; ?>
I need help getting the count to reverse. The output I am looking for is as follows:
<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>
Any help with this would be great.
Change $count=1; to $count = sizeof($arrayreversed);
and $count++; to $count--;
First of all get the amount of keys in the array and assign it as $count, like this:
$count = count($arrayreversed);
Then replace the $count++ with $count-- and you're all set.
How about using the size of the array as your initial count and then subtracting 1 each iteration?
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=count($arrayreversed);
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count--; }; ?>
I have this
<?php
foreach ($results as $row):
if ($row['title'] == "") $row['title'] = date('d-m-Y', strtotime($row['date']));
if (strlen($row['text']) > 100) $row['text'] = substr($row['text'], 0, 100) . "...";
?>
<div>
<a href="<?php echo $row['url'] ?>">
<img src="<?php echo $row['image'] ?>" alt="<?php echo $row['title'] ?>" />
<h1><?php echo $row['title'] ?></h1>
<p><?php echo $row['text']; ?></p>
</a>
</div>
<?php endforeach ?>
Right after the foreach starts I do some "house cleaning" where I substitute the date if there is no title and reduce the text to 100 characters etc.
Repeating this over and over is not very efficient, so it would be better to create a function right?
My question is how do I do this?
Thanks for your help
Try rewriting your code like this. Just add more of your required functionality to the processRowData() function.
<?php
function processRowData($row) {
if ($row['title'] == "") {
$row['title'] = date('d-m-Y', strtotime($row['date']));
}
// Do more with other elements from $row ...
// when done, return the modified $row array
return $row;
}
?>
<?php
foreach ($results as $row) {
// Alter the row data with your function
$row = processRowData($row);
?>
<div>
<a href="<?php echo $row['url'] ?>">
<img src="<?php echo $row['image'] ?>" alt="<?php echo $row['title'] ?>" />
<h1><?php echo $row['title'] ?></h1>
<p><?php echo $row['text']; ?></p>
</a>
</div>
<?php } ?>
Im using this to display a list of thumnails called from my cms:
<?php if($gallery_images) { ?>
<?php
$slide_page = 1;
foreach($gallery_images as $count => $image) { ?>
<li><a href="<?php echo $image->getResizedImage(); ?>" rel="example1" title="********"><img width="125" height="80" src="<?php echo $image->getThumbnailImage()
?>" /></a></li>
<?php if(($count+1) % 3 == 0) {
$slide_page += 1;
?>
It calls images from within my CMS and displays them in groups of 3, with some added jquery to scroll through the sets.
What im trying to do is merge this with my videos within the same list.
The video code is as follows:
<?php foreach($videos as $count => $video) { ?>
<img src="{thumbnail}" />Video A
<?php } ?>
Ive tried using the array_merge function but seem to be having difficulties any help would be greatly appreciated
It's easy:
foreach (array_merge($gallery_images, $videos) as $count => $value) { }
You may also want to look at array_chunk().
Update:
<? foreach (array_merge($images, $videos) as $key => $value): ?>
<? if (is_object($value) === true): ?>
<? if (method_exists($value, 'getLocation') === true): ?>
<img src="{thumbnail}" />Video A
<? elseif (method_exists($value, 'getResizedImage') === true): ?>
<img width="125" height="80" src="<?= $image->getThumbnailImage(); ?>" />
<? endif; ?>
<? endif; ?>
<? endforeach; ?>