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).
Related
I'm trying to pass variables in an include that appears on a page multiple times. While the following sample may technically work, I'm sure there is a much better way of doing this. Any help would be appreciated.
index.php
<?php
$user_avatar = "user-1-avatar.jpg";
$user_username = "username-1";
$user_name = "User 1";
include '/_user-card.php';
?>
<?php
$user_avatar = "user-2-avatar.jpg";
$user_username = "username-2";
$user_name = "User 2";
include '/_user-card.php';
?>
_user-card.php
<div class="user-card">
<img src="<?php echo $user_avatar; ?>" alt="">
<p class="user-card__username"><?php echo $user_username; ?></p>
<p class="user-card__name"><?php echo $user_name; ?></p>
</div>
You can create data that you can use over and over again by using the following function.
function get_user_card($params = array()){
$output = '';
extract($params);
if( isset($user_avatar) && isset($user_username) && isset($user_name) ) {
ob_start();
?>
<div class="user-card">
<img src="<?php echo $user_avatar; ?>" alt="">
<p class="user-card__username"><?php echo $user_username; ?></p>
<p class="user-card__name"><?php echo $user_name; ?></p>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
}
return $output;
}
$user_1 = get_user_card(
array(
"user_avatar" => "user-1-avatar.jpg",
"user_username" => "username-1",
"user_name" => "User 1"
)
);
echo $user_1;
Looking for some help with this. I've created an instagram gallery using the api. When I print_r the json output I get the full array with all 12 posts, but when I echo out the contents in the foreach loop I'm only getting the first post in the list displayed. Thanks for any help. hopping it's just something silly I have missed.
This is the code I have:
<?php
$access_token = "xxxxxxxxxxxxxxxx";
$photo_count = 12;
$json_link = "https://api.instagram.com/v1/users/self/media/recent/?";
$json_link .= "access_token={$access_token}&count={$photo_count}";
$json = file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
//print_r($json);
?>
<div id="instafeed">
<?php
foreach($obj['data'] as $post){
$pic_text = $post['caption']['text'];
$pic_link = $post['link'];
$pic_src = str_replace("http://", "https://", $post['images']['low_resolution']['url']);
$pic_time = date('d-m-Y H:i', $post['caption']['created_time']);
$pic_icon_src = $post['user']['profile_picture'];
$pic_handle = $post['user']['username'];
?>
<div class="social-block">
<a href="<?php echo $pic_link; ?>" target="_blank" title="<?php echo $pic_text; ?>">
<img src="<?php echo $pic_src; ?>" title="<?php echo $pic_text; ?>" width="100% " height="auto">
<div class="insta-acc">
<span class="insta-icon"><img src="<?php echo $pic_icon_src; ?>" title="Uk Pro Surf on Instagram"></span>
<div>
<span class="insta-handle">#<?php echo $pic_handle; ?></span>
</div>
</div>
<span class="insta-text"><p><?php echo $pic_text; ?></p></span>
</a>
</div>
<?php
}
?>
</div>
I love jQuery and dont really understand php.
I am making a slider with albums. So far so good except I am now stuck trying to add two arrays into one for each loop. At least I think that is the best solution.
In my code you can see I have achieved what I need by hard coding 6 images for the 6 custom fields in the custom post type (I am using wordpress). The problem is that if there are not six images then the slider show a blank image (as it exists but doesnt have a src). I tried removing the element with jquery but that was no good. Here is the code I have so far, perhaps there is something i am missing, I just cant seem to get the logic quite right.
<?php
$args = array(
'post_type' => 'albums_gallery',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$album_name = get_the_ID();
$image_1 = get_field('image_1');
$image_1_url = $image_1['url'];
$image_1_caption = get_field('image_1_caption');
$image_2 = get_field('image_2');
$image_2_url = $image_2['url'];
$image_2_caption = get_field('image_2_caption');
$image_3 = get_field('image_3');
$image_3_url = $image_3['url'];
$image_3_caption = get_field('image_3_caption');
$image_4 = get_field('image_4');
$image_4_url = $image_4['url'];
$image_4_caption = get_field('image_4_caption');
$image_5 = get_field('image_5');
$image_5_url = $image_5['url'];
$image_5_caption = get_field('image_5_caption');
$image_6 = get_field('image_6');
$image_6_url = $image_6['url'];
$image_6_caption = get_field('image_6_caption');
?>
<div class="album album_<?php echo $album_name ?>">
<div class="slider-wrapper theme-default">
<div class="slider" class="nivoSlider">
<!--<img src="<?php echo $image_1['url']; ?>" alt="<?php echo $image_1['alt']; ?>" title="<?php echo $image_1_caption; ?>" />
<img src="<?php echo $image_2['url']; ?>" alt="<?php echo $image_2['alt']; ?>" title="<?php echo $image_2_caption; ?>" />
<img src="<?php echo $image_3['url']; ?>" alt="<?php echo $image_3['alt']; ?>" title="<?php echo $image_3_caption; ?>" />
<img src="<?php echo $image_4['url']; ?>" alt="<?php echo $image_4['alt']; ?>" title="<?php echo $image_4_caption; ?>" />
<img src="<?php echo $image_5['url']; ?>" alt="<?php echo $image_5['alt']; ?>" title="<?php echo $image_5_caption; ?>" />
<img src="<?php echo $image_6['url']; ?>" alt="<?php echo $image_6['alt']; ?>" title="<?php echo $image_6_caption; ?>" />-->
<?php
$images = array("$image_1_url","$image_2_url","$image_3_url","$image_4_url", "$image_5_url", "$image_6_url");
foreach ($images as $image) {
if ($image != "") {
echo "<img src='";
echo $image;
echo "' ";
echo "title='caption'";
echo "/>";
}
};
?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
I need to add the image caption, and probably a link into the for each and if statements, little out of my depth being a designer.
Thanks for any help.
This is all you need
<div class = "slider-wrapper theme-default">
<div class = "slider" class = "nivoSlider">
<?php
$images = Array();
for($i = 1; $i <= 6; $i++) {
$image = get_field("image_{$i}");
if(!$image || !$image['url']) {
break;
}
$caption = get_field("image_{$i}_caption");
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $caption; ?>" />
<?php
}
?>
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>
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 } ?>