[英]Foreach loop doesn't work
can anyone please please tell me what is wrong with this code? 谁能告诉我这段代码有什么问题吗? The loop at the end of the first if statement only returns one result instead of all of them. 第一个if语句末尾的循环仅返回一个结果,而不是所有结果。 I've attached the output as well. 我也附加了输出。 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>
"
;
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 "";
}
?>
Sorry for the answers. 对不起,答案。 Here is the result of the var_dump. 这是var_dump的结果。
array(6) {
[0]=>
object(stdClass)#450 (15) {
["attribution"]=>
NULL
["tags"]=>
array(29) {
[0]=>
string(11) "healthyfood"
[1]=>
string(10) "gastronomi"
[2]=>
string(8) "goodmood"
[3]=>
string(6) "aegean"
[4]=>
string(12) "egemutfağı"
[5]=>
string(13) "istanbulnight"
[6]=>
string(8) "goodfood"
[7]=>
string(12) "istanbullife"
[8]=>
string(9) "gastroart"
[9]=>
string(5) "vegan"
[10]=>
string(5) "gurme"
[11]=>
string(18) "rokaperalezzetleri"
[12]=>
string(9) "delicious"
[13]=>
string(8) "rokapera"
[14]=>
string(19) "sağlıklıyemekler"
[15]=>
string(3) "eat"
[16]=>
string(7) "cuisine"
[17]=>
string(13) "rokaperabahce"
[18]=>
string(8) "istanbul"
[19]=>
string(15) "istanbuldayasam"
[20]=>
string(8) "slowfood"
[21]=>
string(8) "beyoğlu"
[22]=>
string(9) "instafood"
[23]=>
string(4) "food"
[24]=>
string(6) "sunday"
[25]=>
string(4) "meze"
[26]=>
string(18) "istanbulrestaurant"
[27]=>
string(5) "pazar"
[28]=>
string(7) "fitfood"
}
["type"]=>
string(5) "image"
["location"]=>
NULL
["comments"]=>
object(stdClass)#451 (2) {
["count"]=>
int(1)
["data"]=>
array(1) {
[0]=>
object(stdClass)#452 (4) {
["created_time"]=>
string(10) "1451222250"
["text"]=>
string(37) "Bizim saksilarda da cok güzel oluyor"
["from"]=>
object(stdClass)#453 (4) {
["username"]=>
string(7) "rrahiya"
["profile_picture"]=>
string(107) "https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xpt1/t51.2885-19/11296890_1089406831069780_918421126_a.jpg"
["id"]=>
string(10) "1752184409"
["full_name"]=>
string(32) "Liberté, egalité, sensualité."
}
["id"]=>
string(19) "1149257992582175831"
}
}
}
["filter"]=>
string(6) "Normal"
["created_time"]=>
string(10) "1451222003"
["link"]=>
string(39) "https://www.instagram.com/p/_y-isiI5-I/"
["likes"]=>
object(stdClass)#454 (2) {
["count"]=>
int(147)
["data"]=>
array(4) {
[0]=>
object(stdClass)#455 (4) {
["username"]=>
string(8) "cbnsibel"
["profile_picture"]=>
string(108) "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11379317_1589253931356213_2037606977_a.jpg"
["id"]=>
string(10) "1986547402"
["full_name"]=>
string(5) "sibel"
}
[1]=>
object(stdClass)#456 (4) {
["username"]=>
string(18) "artisanotelalacati"
["profile_picture"]=>
string(99) "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11193131_105208333143926_294676378_a.jpg"
["id"]=>
string(10) "1992036455"
["full_name"]=>
string(22) "Artisan Otel Alaçatı"
}
[2]=>
object(stdClass)#457 (4) {
["username"]=>
string(10) "inci.yaras"
["profile_picture"]=>
string(108) "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11191206_1844971219060510_1434357002_a.jpg"
["id"]=>
string(10) "2108058746"
["full_name"]=>
string(13) "İNCİ YARAŞ"
}
[3]=>
object(stdClass)#458 (4) {
["username"]=>
string(17) "zerrinellialtoglu"
["profile_picture"]=>
string(115) "https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/s150x150/12277636_518301508335162_120675683_a.jpg"
["id"]=>
string(10) "2090528591"
["full_name"]=>
string(18) "Zerrinellialtioglu"
}
}
}
["images"]=>
object(stdClass)#459 (3) {
["low_resolution"]=>
object(stdClass)#460 (3) {
["url"]=>
string(111) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s320x320/e35/12394199_518383638336848_87665054_n.jpg"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
object(stdClass)#461 (3) {
["url"]=>
string(128) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s150x150/e35/c0.120.960.960/11261430_893913227383126_1405649502_n.jpg"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
object(stdClass)#462 (3) {
["url"]=>
string(118) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s640x640/sh0.08/e35/12394199_518383638336848_87665054_n.jpg"
["width"]=>
int(640)
["height"]=>
int(640)
}
}
["users_in_photo"]=>
array(0) {
}
["caption"]=>
object(stdClass)#463 (4) {
["created_time"]=>
string(10) "1451222003"
["text"]=>
string(355) "Keyifli Pazarlar
#rokapera #egemutfağı #sağlıklıyemekler #vegan #aegean #cuisine #fitfood #meze #istanbul
#rokaperabahce #rokaperalezzetleri #istanbul #istanbullife #istanbulrestaurant #istanbulnight #istanbuldayasam #beyoğlu #eat #delicious #gurme #gastronomi #gastroart #goodfood #goodmood #food #healthyfood #instafood #slowfood #sunday #pazar"
["from"]=>
object(stdClass)#464 (4) {
["username"]=>
string(13) "rokaperabahce"
["profile_picture"]=>
string(108) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11909310_888061464574620_968185939_a.jpg"
["id"]=>
string(10) "1550397278"
["full_name"]=>
string(9) "Roka Pera"
}
["id"]=>
string(19) "1149255927407550385"
}
["user_has_liked"]=>
bool(false)
["id"]=>
string(30) "1149255918096195464_1550397278"
["user"]=>
object(stdClass)#465 (4) {
["username"]=>
string(13) "rokaperabahce"
["profile_picture"]=>
string(108) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11909310_888061464574620_968185939_a.jpg"
["id"]=>
string(10) "1550397278"
["full_name"]=>
string(9) "Roka Pera"
}
}
[1]=>
object(stdClass)#466 (15) {
["attribution"]=>
NULL
["tags"]=>
array(29) {
[0]=>
string(11) "healthyfood"
[1]=>
string(10) "gastronomi"
[2]=>
string(8) "goodmood"
[3]=>
string(6) "aegean"
[4]=>
string(12) "egemutfağı"
[5]=>
string(13) "istanbulnight"
[6]=>
string(8) "goodfood"
[7]=>
string(12) "istanbullife"
[8]=>
string(9) "gastroart"
[9]=>
string(5) "vegan"
[10]=>
string(5) "gurme"
[11]=>
string(18) "rokaperalezzetleri"
[12]=>
string(9) "delicious"
[13]=>
string(8) "rokapera"
[14]=>
string(19) "sağlıklıyemekler"
[15]=>
string(8) "saturday"
[16]=>
string(7) "cuisine"
[17]=>
string(13) "rokaperabahce"
[18]=>
string(6) "flower"
[19]=>
string(8) "istanbul"
[20]=>
string(15) "istanbuldayasam"
[21]=>
string(8) "slowfood"
[22]=>
string(8) "beyoğlu"
[23]=>
string(3) "eat"
[24]=>
string(9) "instafood"
[25]=>
string(4) "food"
[26]=>
string(4) "meze"
[27]=>
string(18) "istanbulrestaurant"
[28]=>
string(7) "fitfood"
}
["type"]=>
string(5) "image"
["location"]=>
object(stdClass)#467 (4) {
["latitude"]=>
float(41.029172249)
["name"]=>
string(18) "Roka Pera Istanbul"
["longitude"]=>
float(28.97304208)
["id"]=>
int(226441757)
}
["comments"]=>
object(stdClass)#468 (2) {
["count"]=>
int(0)
["data"]=>
array(0) {
}
}
["filter"]=>
string(6) "Normal"
["created_time"]=>
string(10) "1451143170"
["link"]=>
string(39) "https://www.instagram.com/p/_woLm7o571/"
["likes"]=>
object(stdClass)#469 (2) {
["count"]=>
int(124)
["data"]=>
array(4) {
[0]=>
object(stdClass)#470 (4) {
["username"]=>
string(10) "inci.yaras"
["profile_picture"]=>
string(108) "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11191206_1844971219060510_1434357002_a.jpg"
["id"]=>
string(10) "2108058746"
["full_name"]=>
string(13) "İNCİ YARAŞ"
}
[1]=>
object(stdClass)#471 (4) {
["username"]=>
string(8) "nerminhm"
["profile_picture"]=>
string(99) "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11375775_368087600058975_581007122_a.jpg"
["id"]=>
string(10) "2125331353"
["full_name"]=>
string(22) "Nermin Hocaoğlu Mutlu"
}
[2]=>
object(stdClass)#472 (4) {
["username"]=>
string(12) "betulozkayaa"
["profile_picture"]=>
string(110) "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-19/s150x150/12353384_1688926777992013_1604858269_a.jpg"
["id"]=>
string(10) "2188974742"
["full_name"]=>
string(14) "Betül Özkaya"
}
[3]=>
object(stdClass)#473 (4) {
["username"]=>
string(10) "comoburger"
["profile_picture"]=>
string(100) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/11378524_1409822219347310_349229698_a.jpg"
["id"]=>
string(10) "2130593263"
["full_name"]=>
string(17) "Hamburger Gourmet"
}
}
}
["images"]=>
object(stdClass)#474 (3) {
["low_resolution"]=>
object(stdClass)#498 (3) {
["url"]=>
string(113) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s320x320/e35/12362432_600511983432932_1619827376_n.jpg"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
object(stdClass)#497 (3) {
["url"]=>
string(131) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s150x150/e35/c0.135.1080.1080/12345975_1506337882995996_2045435867_n.jpg"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
object(stdClass)#496 (3) {
["url"]=>
string(120) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s640x640/sh0.08/e35/12362432_600511983432932_1619827376_n.jpg"
["width"]=>
int(640)
["height"]=>
int(640)
}
}
["users_in_photo"]=>
array(0) {
}
["caption"]=>
object(stdClass)#495 (4) {
["created_time"]=>
string(10) "1451143170"
["text"]=>
string(375) "Cumartesi Çiçekleri
While we spotted that you didn't have an opening {
to your foreach loop, you also didn't have an ending }
for your if statement. 虽然我们发现您的if语句没有开头{
,但for语句也没有结尾}
。 Try changing that code to: 尝试将代码更改为:
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 "";
}
These types of mistakes are very easy to spot if you use clean and consistent formatting throughout your code! 如果您在代码中使用简洁一致的格式,则很容易发现这些类型的错误! Let me know if that fixes it. 让我知道是否可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.