How to insert foreach after echo? - php

this might be a stupid question so please accept my apologies.
I have written a code snippet for a wordpress website that lists restaurants. You enter the instagram id of the venues (if they have one) and it shows the latest instagram photos they've uploaded. If the instagram id value is left empty, i want it to echo nothing. I've written the code and it works fine for places with instagram id's, but as I'm unable the last piece of code (that starts with foreach) inside the echo section, it tries to show it even when the id is empty hence i get a "Invalid argument supplied for foreach() in" error. Can you help me out so that I can include that code inside the echo part, so that it won't call that code when id is empty? Thanks very much in advance.
<?php
if ( $instagramid ) {
echo "
<br><br>
<span style=\"float:left; -webkit-border-radius: 3px;
border-radius: 3px;
font-size: 14px;
float: left;
line-height: 32px;
margin-right: 5px;
margin-top:12px;
padding: 0px 3px 0px 5px;
\">
<a target=\"_blank\" href=\"http://instagram.com/".$username."\"><img width=\"80\" border=\"1px\" src=\"".$profilepic."\"></a></span>
<br>
<div style=\"margin-left: 50px;\">
<div style=\"position: relative;
float: left;
left: 0.00%;
width: 75.00%;
background-color: #f4f4f4\">
<span style=\"
font-weight: bold;
font-style: normal;
font-size: 12px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"><a style=\"color: #3f729b; font-weight: bold; \" target=\"_blank\" href=\"http://instagram.com/".$username."\">".$username."</a> # instagram</span>
</div>
<div style=\"position: relative;
float: left;
left: 0.00%;
width: 82.00%;\">
<div style=\"position: relative;
float: left;
left: 0.50%;
width: 33.00%;\">
<span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Photos<br>
<span style=\"
font-weight: bold; \"><span style=\"
letter-spacing: 0px;\">".$sayi."</span></span></span>
</div>
<div style=\"position: relative;
float: left;
left: 1.50%;
width: 33.00%;\">
<span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Followers<br>
<span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipci."</span></span></b></span>
</div>
<div style=\"position: relative;
float: right;
right: 0.50%;
width: 31.00%;\">
<span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Following<br><span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipediyor."</span></span></span>
</div>
</div>
</div>
<br><br>
";
}
else {
echo "";
}
?>
<?php foreach ($result->data as $fotos): ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<span style="font-size: 12px; float:left;
margin:5px 0 2px 10px;">
<a target="_blank" class="group" rel="group1" href="<?= $fotos->link ?>">
<img width="90" src="<?= $fotos->images->thumbnail->url ?>"></a>
<span style="font-weight:bold; float:left;"></span>
<span style="float:right;"><span style="color:red; font-weight: bolder;">❤ </span><?= $fotos->likes->count ?></span>
</span><?php endforeach //Biter ?>

Maybe like someone said you need to put your loop in if statment like this
<?php
if ( $instagramid ) {
// Your old code is here "your echo"
// but before end of if statment, and after echo"" put loop
foreach ($result->data as $fotos){ ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<span style="font-size: 12px; float:left; margin:5px 0 2px 10px;">
<a target="_blank" class="group" rel="group1" href="<?= $fotos->link ?>">
<img width="90" src="<?= $fotos->images->thumbnail->url ?>"></a>
<span style="font-weight:bold; float:left;"></span>
<span style="float:right;"><span style="color:red; font-weight:bolder;">❤ </span><?= $fotos->likes->count ?></span>
</span><?php } //Biter ?>
}
else {
echo "";
}
?>

Related

PHP email body background color not coloring whole mail

The background color only colors the first half of my email when I try to add background-color in my body.
$mail->Body =
"<body style='background-color: #fbedde;'>
<div style='width:800px; text-align: center; margin-left: auto; margin-right: auto;'>
<img src='cid:logo' width='40%' style='margin-left: auto; margin-right: auto; padding-top: 20px; padding-bottom: 20px;'>
<h1 style='color: #00a6a6; font-family: 'brandon_black'; letter-spacing: 2px; padding-bottom: 60px;'>COUPON CLAIMED</h1>
<div style='padding-top:30px; width: 400px;float:left;'>
<img src='cid:coupon' width='85%' style='padding-left: auto; padding-right: auto; border-radius: 2% !important; box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);'>
</div>
<div style='padding-top:15px; width: 400px; float:left; text-align: left; color: #00a6a6;'>
<h3 style='font-weight: bold;'>Claimed By:</h3>
<h2 style='font-family: 'brandon_black'; letter-spacing: 1px;'>$usr_fname $usr_lname</h2>
<h3 style='font-weight: bold;'>$usr_contact</h3>
<h3 style='font-weight: bold;'>$usr_member</h3><br>
<h3>Please proceed to<h3>
<a href='https://www.mywhitecard.ph/cms' style='color: #00a6a6; text-decoration: underline;'>https://www.mywhitecard.ph/cms</a>
<h3>for scheduling of member.</h3>
</div>
</div>
</body>";
I added the css directly into the tags because the css styles dont get recognized if taken from a different file since this is an html email.
I do not have any other background-color styles thats why I am questioning why the other half of my mail remains uncolored. Any help would be appreciated.

PHP code to call instagram api gives invalid argument

UPDATE: Ok, so apparently instagram changed its API that's why it's not working. I have no idea what to do now.
I have a php code that calls instagram api and it has been working perfectly so far. However, it stopped working today - it gives an invalid argument error as you can see below (on the left).
http://www.weheartalacati.com/alavya-hotel/
The error is: Warning: Invalid argument supplied for foreach() in /home/alacati/public_html/wp-content/themes/coastline/content.php on line 303
The code below is the related part of the code (around line 303 and before), do you know what could be the problem? Any help would be greatly appreciated. (the problematic line is the one that starts with " foreach ($result->data as $fotos)")
<article id="entry-<?php the_ID(); ?>" <?php post_class('entry'); ?>>
<div class="row">
<div class="col-md-4">
<div class="entry-info-wrap">
<?php
$instagramid = get_post_meta($post->ID, 'Instagram', true);
$foursquareid = get_post_meta($post->ID, 'Foursquare', true);
// Supply a user id and an access token
$userid = "xx";
$accessToken = "xx";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/$instagramid/media/recent/?access_token={$accessToken}&count=6");
$result = json_decode($result);
$sonuc = fetchData("https://api.instagram.com/v1/users/$instagramid/?access_token={$accessToken}");
$sonuc = json_decode($sonuc);
$username=$sonuc->data->username;
$profilepic=$sonuc->data->profile_picture;
$sayi=$sonuc->data->counts->media;
$takipci=$sonuc->data->counts->followed_by;
$takipediyor=$sonuc->data->counts->follows;
....
<?php
if ( $instagramid )
{
echo "<br><br>
<span style=\"float:left; -webkit-border-radius: 3px; border-radius: 3px; font-size: 14px; float: left; line-height: 32px; margin-right: 5px; margin-top:12px; padding: 0px 3px 0px 5px; \">
<a target=\"_blank\" href=\"http://instagram.com/".$username."\"><img width=\"80\" border=\"1px\" src=\"".$profilepic."\"></a></span>
<br>
<div style=\"margin-left: 50px;\">
<div style=\"position: relative; float: left; left: 0.00%; width: 75.00%; background-color: #f4f4f4\">
<span style=\"font-weight: bold;font-style: normal;font-size: 12px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">
<a style=\"color: #3f729b; font-weight: bold; \" target=\"_blank\" href=\"http://instagram.com/".$username."\">".$username."</a> # instagram</span>
</div>
<div style=\"position: relative; float: left; left: 0.00%; width: 82.00%;\">
<div style=\"position: relative; float: left; left: 0.50%; width: 33.00%;\">
<span style=\"font-weight: lighter; font-style: normal; font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Photos<br>
<span style=\"font-weight: bold; \"><span style=\"letter-spacing: 0px;\">".$sayi."</span></span></span>
</div>
<div style=\"position: relative;float: left;left: 1.50%;width: 33.00%;\">
<span style=\"font-weight: lighter;font-style: normal;font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Followers<br>
<span style=\"font-weight: bold;\"><span style=\"letter-spacing: 0px;\">".$takipci."</span></span></b></span>
</div>
<div style=\"position: relative; float: right; right: 0.50%; width: 31.00%;\">
<span style=\"font-weight: lighter; font-style: normal; font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Following<br>
<span style=\"font-weight: bold;\">
<span style=\"letter-spacing: 0px;\">".$takipediyor."</span></span></span>
</div>
</div>
</div>
<br><br>";
foreach ($result->data as $fotos)
{
$instagramlink = $fotos->link;
$instagramthumb = $fotos->images->thumbnail->url;
$instagramlikes = $fotos->likes->count;
echo "<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<span style=\"font-size: 12px; float:left; margin:5px 0 2px 10px;\">
<a target=\"_blank\" class=\"group\" rel=\"group1\" href=\"".$instagramlink."\">
<img width=\"90\" src=\"".$instagramthumb."\">
</a>
<span style=\"font-weight:bold; float:left;\"></span>
<span style=\"float:right;\"><span style=\"color:red; font-weight: bolder;\">❤ </span>".$instagramlikes."</span>
</span>";
}
} else { echo ""; }
?>
</div>
</div>
<div class="col-md-8">
<?php ci_post_thumbnail(); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php if ( is_single() ) {
comments_template();
} ?>
</div>
</div>
</article>

Align div's next to each other

As you might see, im having some problems aligning 4 divs next to each other:
For some reason i cant get them to be aligned.
Can anybody tell me whats wrong?
html:
<?php foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="frontPost">
<a href="<?php the_permalink();?>">
<div class="crop">
<img class="postImg" src="<?php the_field('frontimg')?>" alt="frontImg">
</div>
<p class="postTitle"> <?php the_title()?> </p>
<p class="postIntro"> <?php the_field('introduction') ?> </p>
</a>
</div>
<?php endforeach; ?>
css:
.crop {
width: 250px;
height: 150px;
overflow: hidden;
}
.frontPost{
width: 250px;
display: inline-block;
margin-left: 5px;
margin-top: 25px;
margin-right: 5px;
}
.bigTxt{
letter-spacing: 10px;
font-size: 16px;
}
.postTitle{
text-transform: uppercase;
letter-spacing: 1.5px;
font-weight: bold;
}
.postIntro{
text-align: left;
}
What about using vertical-align property. Use the code below for css
.crop {
width: 250px;
height: 150px;
overflow: hidden;
}
.frontPost{
vertical-align:top;
width: 250px;
display: inline-block;
margin-left: 5px;
margin-top: 25px;
margin-right: 5px;
}
.bigTxt{
letter-spacing: 10px;
font-size: 16px;
}
.postTitle{
text-transform: uppercase;
letter-spacing: 1.5px;
font-weight: bold;
}
.postIntro{
text-align: left;
}
Hope this helps you
Where your CSS code is showing inline-block add this rule to it.
vertical-align:top;
Output:
.frontPost{
width: 250px;
display: inline-block;
margin-left: 5px;
margin-top: 25px;
margin-right: 5px;
vertical-align:top;
}
This will then make all of the divs align at the top of the parent div.
A useful link to read through! - http://css-tricks.com/almanac/properties/v/vertical-align/

My comment are running out of comment box

In my web i allow user to post comment with there name and picture. I have notice one issue that when some one post bigger picture in comment using html code it run out of comment box. so all i want is thing should stay inside a comment box. not matter how big picture they post. i have posted all detail below kindly help thanks :|
My html
<div class="mycomment">
<div id="postpic">
<img id="profile_pic" width="50px" height="150px" src="image/user/1.gif" class="">
</div>
<div class="mycommentpost">
<a class="postername" href="profile.php?userID=1">Deepak Kumar: </a><br>
<img src="http://i00.i.aliimg.com/wsphoto/v0/789295022/-font-b-Easter-b-font-day-font-b-Bunny-b-font-ears.jpg" width="650" height="350" alt="">
</div>
</div>
My css:
.mycomment {
font-weight: normal;
color: #000000;
border: 2px solid #DEDEDE;
letter-spacing: 1pt;
font-family: arial, helvetica, sans-serif;
float: left;
width: 550px;
margin: 20px 0;
}
#postpic {
float: left;
width: 70px;
}
.mycommentpost {
font-weight: normal;
color: #000000;
letter-spacing: 1pt;
width: 480px;
float: right;
}
.postername {
color: #580000;
font-weight: bold;
font-size: 1em;
text-decoration: none;
}
Comment here is just a picture:
<img src="http://i00.i.aliimg.com/wsphoto/v0/789295022/-font-b-Easter-b-font-day-font-b-Bunny-b-font-ears.jpg" width="650" height="350" alt="">
Solved : By adding this ->
.mycommentpost > img{
max-height: XXXpx !important;
max-width: XXXpx !important;
}
Add to your CSS
.mycommentpost > img{
max-height: XXXpx !important;
max-width: XXXpx !important;
}
This way you will limit the max sizes of added images..
no matter what size of image they upload using this css set the image to default size in your mycommentpost div.
> .mycommentpost img{
> border: 1px solid #DEDEDE;
> padding: 10px;
> width: 130px; }
Try this
<div class="mycomment">
<div id="postpic">
<img id="profile_pic" width="50px" height="150px" src="image/user/1.gif" class="">
</div>
<div class="mycommentpost">
<a class="postername" href="profile.php?userID=1">Deepak Kumar: </a><br>
<img src="http://i00.i.aliimg.com/wsphoto/v0/789295022/-font-b-Easter-b-font-day-font-b-Bunny-b-font-ears.jpg" width="650" height="350" alt="">
</div>
.mycommentpost {
font-weight: normal;
color: #000000;
letter-spacing: 1pt;
width: 480px;
float: left;
}
.mycommentpost img{
width :450px;
}

Having a custom xml from youtube channel

I want to show say last 5 videos that I have uploaded in my youtube channel in a block in my site. I currently have this url that produces a xml file.
I dont know where should I go in youtube to customize this xml file lets say I only want it return only the last 5 videos.
Alos, it returns in entry tags some sort of html formatted stuff for the description I had written in youtube for the uploaded file. for example for one of my videos it shows like this:
<div style="color: #000000;font-family: Arial, Helvetica, sans-serif; font-size:12px; font-size: 12px; width: 555px;">
<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td width="140" valign="top" rowspan="2"><div style="border: 1px solid #999999; margin: 0px 10px 5px 0px;"><img alt="" src="http://i.ytimg.com/vi/Ihy785MpH7g/0.jpg"></div></td>
<td width="256" valign="top"><div style="font-size: 12px; font-weight: bold;"><a style="font-size: 15px; font-weight: bold; font-decoration: none;" href="http://www.youtube.com/watch?v=Ihy785MpH7g&feature=youtube_gdata">TinyOS Tutorial #3 - Quick overview and HelloWorld application!</a>
<br></div>
<div style="font-size: 12px; margin: 3px 0px;"><span>Topics:
-Quick overview of TinyOS
-Application Structure in TinyOS
-Simple HelloWorld application
-Extending HelloWorld applications with timers</span></div></td>
<td style="font-size: 11px; line-height: 1.4em; padding-left: 20px; padding-top: 1px;" width="146" valign="top"><div><span style="color: #666666; font-size: 11px;">From:</span>
ElectronicsPubVideos</div>
<div><span style="color: #666666; font-size: 11px;">Views:</span>
37</div>
<div style="white-space: nowrap;text-align: left"><img style="border: 0px none; margin: 0px; padding: 0px; vertical-align: middle; font-size: 11px;" align="top" alt="" src="http://gdata.youtube.com/static/images/icn_star_full_11x11.gif"> <img style="border: 0px none; margin: 0px; padding: 0px; vertical-align: middle; font-size: 11px;" align="top" alt="" src="http://gdata.youtube.com/static/images/icn_star_full_11x11.gif"> <img style="border: 0px none; margin: 0px; padding: 0px; vertical-align: middle; font-size: 11px;" align="top" alt="" src="http://gdata.youtube.com/static/images/icn_star_full_11x11.gif"> <img style="border: 0px none; margin: 0px; padding: 0px; vertical-align: middle; font-size: 11px;" align="top" alt="" src="http://gdata.youtube.com/static/images/icn_star_full_11x11.gif"> <img style="border: 0px none; margin: 0px; padding: 0px; vertical-align: middle; font-size: 11px;" align="top" alt="" src="http://gdata.youtube.com/static/images/icn_star_full_11x11.gif"></div>
<div style="font-size: 11px;">1
<span style="color: #666666; font-size: 11px;">ratings</span></div></td></tr>
<tr><td><span style="color: #666666; font-size: 11px;">Time:</span>
<span style="color: #000000; font-size: 11px; font-weight: bold;">42:52</span></td>
<td style="font-size: 11px; padding-left: 20px;"><span style="color: #666666; font-size: 11px;">More in</span>
Science & Technology</td></tr></tbody></table></div>
What I need is just few things: Title, link, screenshot of the video(That I have selected in youtube itself) and raw description text. So that I can put them nicely in my website with my own style.
This works for me
$string = file_get_contents('https://gdata.youtube.com/feeds/api/users/USER_NAME/uploads?v=2&alt=json&max-results=5&orderby=published');
if($result = json_decode($string, true)) {
foreach($result['feed']['entry'] as $entry) {
echo "Title: ".$entry['title']['$t']."<br />";
echo "Description: ".$entry['media$group']['media$description']['$t']."<br />";
echo "Published: ".$entry['published']['$t']."<br />";
echo "Link: ".$entry['link'][0]["href"]."<br />";
echo "Thumb: ".$entry['media$group']['media$thumbnail'][0]['url']."<br />";
echo "<hr />";
}
} else{
echo "<h1>Problem with service</h1><br />";
echo $output;
}
Hope that helps.

Categories