We are bulding a simple module in php, but we have a error that produces a bad encoding for html page :
<?php
$ids = array('363', '367', '366', '365','364','371','370','456','461');
?>
<div class="Staff">
<?php foreach ($ids as $ids) {
$user = Foundry::user($ids);
}?>
<?php foreach ($ids as $user => $ids) { ?>
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php}?>
</div>
Any Idea? , thanks as always for the help.
It should like below:
<?php foreach ($ids as $id) {
$user = Foundry::user($id);
?>
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php } ?>
have you tried something like this:
<?php
$ids = array('363', '367', '366', '365','364','371','370','456','461');
?>
<div class="Staff">
<?php foreach ($ids as $id) { ?>
$user = Foundry::user($id);
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php}?>
</div>
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'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev
I have a question:
I make a pre for news:
Array
(
[0] => Array
(
[name] => tag
[id] => 57
[title] => Article1,Article2,Article3
[views] => 53,54,58
[smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
[date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
)
)
How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates;
My foreach:
<?php if ($news):?>
<?php foreach($news as $n):?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
<?php echo $n['title'] ?><br/>
<?php echo $n['views'] ?>
<?php endforeach ?>
<?php endif; ?>
With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please
Try this:
<?php
$news = array(array( "tag",
57,
"Article1,Article2,Article3",
"53,54,58",
"Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
"2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
) );
?>
<?php if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
<?php $views = explode(',',$news[0][3]);?>
<?php $article = explode(',',$news[0][2]);?>
<?php for($i=0;$i<count($article);$i++){
echo $article[$i].'-'.$views[$i];
} ?>
</div>
<?php }?>
I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.
<?php foreach($news as $n):?>
<?php $titles = explode(",", $n['title']); ?>
<?php $smallimages = explode(",", $n['smallimage']); ?>
<?php $views= explode(",", $n['views']); ?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
<?php echo title[0] ?><br/>
<?php echo views[0] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
<?php echo title[1] ?><br/>
<?php echo views[1] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
<?php echo title[2] ?><br/>
<?php echo views[2] ?>
<?php endforeach ?>
<?php endif; ?>
I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:
$n['title'] = explode(',',$n['title'])
To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:
<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title'])
$n['views'] = explode(',',$n['views'])
$n['smallimage'] = explode(',',$n['smallimage'])
foreach ($n['title'] as $key=>$value) {
echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
echo $n['title'][$key] . "<br />";
echo $n['views'][$key]
}
?>
</div>
You would place this in the existing foreach-loop.
I am trying to use $description variable outside the loop. Help me do it please.
<?php
$sql_album = "SELECT * FROM albums";
$res_album = mysql_query($sql_album) or die(mysql_error());
$albums = array();
$description = "";
while ($row_album = mysql_fetch_assoc($res_album)) {
$description = $row_album['description'];
$albums[$row_album['title']] = array(
'images/albums/'.$row_album['title'].'/1.jpg',
'images/albums/'.$row_album['title'].'/2.jpg',
'images/albums/'.$row_album['title'].'/3.jpg',
'images/albums/'.$row_album['title'].'/4.jpg'
);
}
foreach ($albums as $name => $a) {
?>
<div id="album">
<a href="view_album.php?name=<?php echo $name; ?>" data-images="<?php echo implode('|', array_slice($a,1))?>" class="album">
<img src="<?php echo $a[0]?>" alt="<?php echo $name?>" />
<span class="preloader"></span>
</a>
<div class="album_info">
<h4><?php echo $name?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php
}
?>
Should I make an array, or define it first, i am totally confused, need help.
In your $albums array (in the while loop), store your images and description like this:
$albums[$row_album['title']] = array(
"description" => $row_album['description'],
"images" => array(
'images/albums/'.$row_album['title'].'/1.jpg',
'images/albums/'.$row_album['title'].'/2.jpg',
'images/albums/'.$row_album['title'].'/3.jpg',
'images/albums/'.$row_album['title'].'/4.jpg'
)
);
Then in your foreach loop, act like this:
<img src="<?php echo $a['images'][0]?>" alt="<?php echo $name?>" />
and
<p><?php echo $a['description']; ?></p>
Edit:
Don't forget to change this
array_slice($a,1)
to this:
array_slice($a['images'],1)
I would just combine the whole thing into a single loop; the below code is untested, but I hope you can follow it.
while ($row_album = mysql_fetch_assoc($res_album)) {
print_album($row_album);
}
function print_album(array $album)
{
$name = htmlspecialchars($album['title'], ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($album['description'], ENT_QUOTES, 'UTF-8');
$view_link = sprintf('view_album.php?%s', http_build_query([
'name' => $album['title'],
]);
$images = array_map(function($index) use ($album) {
return sprintf(
'images/albums/%s/%d.jpg',
urlencode($album['title']),
$index
);
}, range(1, 4));
$images_data = htmlspecialchars(join('|', array_slice($images, 1)), ENT_QUOTES, 'UTF-8');
?>
<div id="album">
<a href="<?php echo $view_link ?>" data-images="<?php echo $images_data; ?>" class="album">
<img src="<?php echo htmlspecialchars($images[0], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo $name; ?>" />
<span class="preloader"></span>
</a>
<div class="album_info">
<h4><?php echo $name; ?></h4>
<p><?php echo $description; ?></p>
</div>
</div>
<?php
}
I've added escaping with the use of urlencode(), htmlspecialchars() and http_build_query() to prevent a potential break in your HTML (or XSS is the worst case).
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 } ?>