-3

Hi guys im trying to format an html output based on this picasa script using foreach, this way:

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

The output is:

<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> 

But i need this:

<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>   

The idea is bring all the album in my picasa account with their pictures inside, example:

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

album 2 has:
foto4.jpg

So on ... That's it i hope someone could help me and understand better my really bad english :)

FULL SOURCE:

<?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;
?>
fecapeluda
  • 133
  • 1
  • 6
  • 20
  • if you don't know hwo to add the part you're looking at, you didn't write the code there. You have made no effort on your own. – deltree Mar 02 '12 at 17:00
  • Are there any opening span tags anywhere? – Mark Baker Mar 02 '12 at 17:02
  • @deltree with all respect do you even read my question? I left a link to the owner. Besides im asking help because i dont have idea how to do it i already try a lot, but no results thats why im here. – fecapeluda Mar 02 '12 at 17:08
  • 1
    Hey how can we check whether it is first album second or so on. Is there any parameter for that. I can't see that in yours. – Sabari Mar 02 '12 at 17:14
  • please explain how you know that foto3 is in album2? in fact, in your demonstration, you say thgat foto3 is in 2 albums and foto4 is in no albums – deltree Mar 02 '12 at 18:01
  • @deltree sorry i fix the problem in the example. But i guess that the thing how to split pictures and put it in the right album. If this cant be done please tell me :) – fecapeluda Mar 02 '12 at 18:25
  • @fecapeluda if you show us the entire $albums object, and how it's created, we'll be able to use the property that defines which album each $photo is in to finish this script – deltree Mar 02 '12 at 18:26
  • @deltree FULL source code added, i hope it could help a lot!. – fecapeluda Mar 02 '12 at 18:46

2 Answers2

0

If you wish to split the output into 'albums' you need to know how to split the array of photos based on which album they should belong to. How are you populating $photo? Is each 'album' going to have only 3 photos in?

Shaun Bohannon
  • 491
  • 1
  • 4
  • 11
  • The idea is bring all content inside the albums of my picasa account, example: album 1 has: foto1.jpg foto2.jpg foto3.jpg album 2 has: foto3.jpg ETC. The max photos by album are 5. – fecapeluda Mar 02 '12 at 17:33
  • I think what you need to do is print_r($albums) and understand that structure better and then maybe have two loops. One over $albums that will give you each $album, and then one over $album to give you each $photo. – Shaun Bohannon Mar 02 '12 at 21:15
0

here, your homework done for you

<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>  

note the other answer as well. splitting out which "foto" goes into which album requires more information, this is just an example with html markup so you can understand how the php is fitting in the mix

deltree
  • 3,756
  • 1
  • 30
  • 51