Reputation: 101
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 ?>
Upvotes: 0
Views: 240
Reputation: 753
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 "";
}
?>
Upvotes: 1