我创建了一个PHP脚本,它将从Flickr帐户中获取图像,然后以专辑形式显示它们。一切都很好,除了每张专辑之前发生的这个讨厌的小0。这是一个示例HTML文档:
<html>
<head>
<script type="text/javascript" src="chrome-extension://bfbmjmiodbnnpllbbbfblcplfjjepjdn/js/injected.js"></script>
<style type="text/css"></style>
</head>
<body>0
<div class="album" id="72157643471292484">
<img class="album-thumnail" src="https://farm4.staticflickr.com/3702/13647757594_46fb10ca56_m.jpg">
<div class="album-information">
<div class="album-header">
<p class="album-title">Set 1</p><span class="album-contents">3</span>
</div>
<p class="album-description"></p>
</div>
</div>0
<div class="album" id="72157643469056814">
<img class="album-thumnail" src="https://farm4.staticflickr.com/3739/13647432073_9c11d27b7c_m.jpg">
<div class="album-information">
<div class="album-header">
<p class="album-title">Test Gallery 1</p><span class="album-contents">9</span>
</div>
<p class="album-description">Bing Backgrounds</p>
</div>
</div>
</body>
</html>
在上面的代码中,在标签关闭之后,一旦关闭,就会有第二个0.它们位于专辑的开始标记之前,我不知道为什么。我不认为我已经提到过如何调用它,但它是通过jQuery的AJAX调用的。
这是PHP
<?php
$api = '';
$userid = '';
$apiurl = 'https://api.flickr.com/services/rest/?';
$responseformat = 'php_serial';
//Initiate the PHP script
echo createHTML();
function createHTML(){
return '<div class="gallery">'.createCollectionHTML().'</div>';
}
function createCollectionHTML(){
$html_resp = '';
$collections = getCollections();
foreach($collections['collections']['collection'] as $c){
$html_resp = $html_resp.'
<div class="collection">
<div class="coll-header">
<div class="text-header"><span>'.$c['title'].'</span></div>
</div>
<div class="coll-albums">'.getAlbumHTML($c).'</div></div>';
}
return $html_resp;
}
function getAlbumHTML($coll_data){
$album_html = "";
$album_num = 0;
foreach($coll_data['set']as $s){
$album_num++;
$album_html_lower = '<div id="'.$s['id'].'"><img class="album-thumnail" src="'.getSetThumbnailURL($s['id']).'"/><div class="album-information"><div class="album-header"><p class="album-title">'.$s['title'].'</p><span class="album-contents">'.getSetContentsNum($s['id']).'</span></div><p class="album-description">'.$s['description'].'</p></div></div>';
if($album_num == 1){
$album_html = $album_html + '<div class="album-row">'.$album_html_lower;
}else if($album_num == 2){
$album_html = $album_html + $album_html_lower;
}else if($album_nnum == 3){
$album_html = $album_html + $album_html_lower.'</div>';
$album_num = 0;
}
}
return $album_html;
}
function getSetContentsNum($id){
$set_data = getSetInformation($id);
return $set_data['photoset']['count_photos'];
}
function getRequest($arguments){
global $apiurl;
//Enocdes the arguments for JSON request
$encoded_params = array();
foreach ($arguments as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
//Join the url via arguments and $apiurl variable
$url = $apiurl.implode('&', $encoded_params);
//Request the file, unserialize the JSON response
$rsp = file_get_contents($url);
$rsp_obj = unserialize($rsp);
//Checks if response was okay, else, returns failure message
if($rsp_obj['stat']== 'ok'){
return $rsp_obj;
}else{
echo 'requestfail|message:'.$rsp_obj['message'];
return;
}
}
function getCollections(){
global $api;
global $userid;
global $responseformat;
$args = array(
'method' => 'flickr.collections.getTree',
'api_key' => $api,
'user_id' => $userid,
'format' => $responseformat
);
return getRequest($args);
}
function getSetInformation($id){
global $api;
global $userid;
global $responseformat;
$args = array(
'method' => 'flickr.photosets.getInfo',
'photoset_id' => $id,
'api_key' => $api,
'format' => $responseformat
);
return getRequest($args);
}
function getSetPhotos($id){
global $api;
global $userid;
global $responseformat;
$args = array(
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => $id,
'extras' => 'url_s',
'media' => 'photos',
'api_key' => $api,
'format' => $responseformat
);
return getRequest($args);
}
function getSetThumbnail($id){
$set_pics = getSetPHotos($id);
return $set_pics['photoset']['photo'][0];
}
function getSetThumbnailURL($id){
$thumbnail_info = getSetThumbnail($id);
return $thumbnail_info['url_s'];
}
?>
我认为值得注意的是,如果我放置一个&#34; 1&#34;在$ album_html变量中或在$ album_html_lower的开始标记之前放置1,在任何一种情况下都将0替换为1,但是,我无法完全删除0.所以我该如何删除它?
答案 0 :(得分:2)
您使用加号连接字符串,而不是使用点:
// Wrong
$album_html = $album_html + $album_html_lower;
// Right
$album_html = $album_html . $album_html_lower;
加号仅用于算术运算,这使得PHP希望将变量转换为整数。不以整数开头的字符串将强制转换为0,从而生成您提供的HTML。用点替换加号,问题就解决了。
您也可以用速记符号替换这些语句:
$album_html .= $album_html_lower;
.=
运算符将右手值连接到左手值的末尾。
在旁注中,getAlbumHTML()
中的第三个if语句包含&#39; n&#39;变量名$album_nnum
中的内容太多。