使用Picasa PHP FOREACH的Html / Markup格式

时间:2012-03-02 16:57:17

标签: php picasa

大家好我试图使用foreach格式化基于this picasa script的html输出,这样:

<? foreach($albums as $photo) {?>  
<span><img src="<? echo $photo[1]; ?>" border=0></a><p><?=$photo[0]; ?></p></span> 
    <?  } ?>

输出结果为:

<span><img src="foto1.jpg" border=0></a><p>This is pict 1 Album 1</p></span>
<span><img src="foto2.jpg" border=0></a><p>This is pict 2 Album 1</p></span>
<span><img src="foto3.jpg" border=0></a><p>This is pict 3 Album 1</p></span>
<span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span> 

但我需要这个:

<div>
<h1>Album 1</h1>
<span><img src="foto1.jpg" border=0></a><p>This is pict 1 Album 1</p></span>
    <span><img src="foto2.jpg" border=0></a><p>This is pict 2 Album 1</p></span>
    <span><img src="foto3.jpg" border=0></a><p>This is pict 3 Album 1</p></span>
</div>
<div>
<h1>Album 2</h1>
    <span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span> 
 </div>   

这个想法是将我的picasa帐户中的所有相册带到里面,例如:

album 1 has:
foto1.jpg
foto2.jpg
foto3.jpg

album 2 has:
foto4.jpg

所以...这就是我希望有人可以帮助我,更好地理解我真正糟糕的英语:)

完整来源:

<?php

$userid = "cramosb"; // Your Google user name

$target = "PicasaBox.php/?album="; //URL to pass the name of the album to for the links
$imgmax = "512"; 
/*------------------------------------------------------------------------------
| USER CONFIGURATION END
------------------------------------------------------------------------------*/

// *** Only modify past this point if you know what you're doing ***

$insideentry = false;
$tag = "";
$title = "";
$url = "";

// function to parse the start of an XML element
function startElement($parser, $name, $attrs) {
    global $insideentry, $tag, $title, $url;
    if ($insideentry) {
        $tag = $name;

        if ($name == "MEDIA:CONTENT"){
            $url = $attrs["URL"];
        }
    } elseif ($name == "ENTRY") {
        $insideentry = true;
    }
}

// function to parse the end of an XML element
function endElement($parser, $name) {
    global $insideentry, $tag, $title, $url, $albums;
    if ($name == "ENTRY") {
        $albums[] = array($title, $url);
        //echo $title . ' ' . $url;
        $title = "";
        $url = "";
        $insideentry = false;
    }
}

// function to parse the contents of an XML element
function characterData($parser, $data) {
    global $insideentry, $tag, $title, $url;
    if ($insideentry) {
        if ($tag == "TITLE") {
            $title .= $data;
        }
    }
}

// Lets get started... 

// Create an XML parser, using the functions above
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// The URL of the album feed I CHANGE THIS: $feed = "http://picasaweb.google.com/data/feed/api/user/" . $userid . "?kind=album"; TO:
$feed = "http://picasaweb.google.com/data/feed/api/user/" . $userid . "?kind=photo";

// Open the feed
$fp = fopen($feed,"r")
    or die("Error reading RSS data.");

// Parse the feed
while ($data = fread($fp, 4096))
    xml_parse($xml_parser, $data, feof($fp))
        or die(sprintf("XML error: %s at line %d", 
            xml_error_string(xml_get_error_code($xml_parser)), 
            xml_get_current_line_number($xml_parser)));
// Close the feed
fclose($fp);
xml_parser_free($xml_parser); 


foreach($albums as $album)
{
    $htmlout .= '<span><a href="'. $target . $album[0] . '"><img src="' . $album[1] . '" border=0></a><p>' . $album[0] . '</p></span>';
}
print $htmlout;
exit;
?>

2 个答案:

答案 0 :(得分:0)

如果您希望将输出拆分为“相册”,您需要知道如何根据照片属于哪个相册来分割照片数组。你是如何填充$照片的?每张“专辑”中只有3张照片吗?

答案 1 :(得分:0)

在这里,你的功课已经完成了

<div>
<h1>Album 1</h1>
<? foreach($albums as $photo) {?>  
<span><img src="<? echo $photo[1]; ?>" border=0></a><p><?=$photo[0]; ?></p></span> 
<?  } exit;?>
</div>
<div>
<h1>Album 2</h1>
    <span><img src="foto4.jpg" border=0></a><p>This is pict 4 Album 2</p></span> 
</div>  

另请注意其他答案。拆分哪个“foto”进入哪个专辑需要更多信息,这只是一个html标记的例子,所以你可以理解php如何适合混合