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><img src="' . $album[1] . '" border=0><p>' . $album[0] . '</p></span>';
}
print $htmlout;
exit;
?>
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?
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
Related
I've been trying to figure this out for like 3 hours and am not getting anywhere. I'm trying to set the img src in the avatar div to the Roblox user's avatar URL with this endpoint https://thumbnails.roblox.com/v1/users/avatar-bust?userIds=3102777127&size=150x150&format=Png&isCircular=true but it requires usernames and my data is usernames so we have to use this endpoint to get IDs https://api.roblox.com/users/get-by-username?username=VOICECHAT22983. I have been trying a bunch of different stuff and nothing is working and just getting a bunch of errors.
This is how the data looks:
brenda12322323
eunaodoaisnudhnac
Lovesroblox1334
Cho5963
Sinmin191
vikaa_qwixxk
yourfav_stepsister
SnipesG0D
This is the PHP Code
<?php
$lines = file('../txt/names.txt');
foreach ($lines as $line) {
echo '<div class="avatar">
<img src="'.$avatar.'" alt="">
<h1>' . $line . '</h1>
Profile
</div>';
}
?>
I'm trying to set the $id variable as the user's ID and the avatar variable as the image URL
You need to get all the IDs first from the get-by-username endpoint, pass those into the avatars endpoint, then put the data back together:
<?php
/*
Question Author: cdnAshton
Question Answerer: Jacob Mulquin
Question: PHP Set Roblox Avatar to Image
URL: https://stackoverflow.com/questions/74977675/php-set-roblox-avatar-to-image
Tags: php, roblox
*/
function get_id_by_username($name)
{
$url = 'https://api.roblox.com/users/get-by-username?username=' . $name;
echo 'getting ' . $url . PHP_EOL;
return json_decode(file_get_contents($url), true);
}
function get_avatar_urls($ids)
{
$ids_param = implode(',', $ids);
$url = 'https://thumbnails.roblox.com/v1/users/avatar-bust?userIds='.$ids_param.'&size=150x150&format=Png&isCircular=true';
return json_decode(file_get_contents($url), true);
}
if (!file_exists('users.json')) {
$names = file('names.txt');
$users = [];
foreach ($names as $name) {
$name = trim($name);
$id = get_id_by_username($name);
$users[$id['Id']] = $id;
sleep(15); // Needed otherwise you will hit API rate-limits
}
file_put_contents('users.json', json_encode($ids, JSON_PRETTY_PRINT));
} else {
$users = json_decode(file_get_contents('users.json'), true);
$ids = [];
foreach ($users as $user) {
$ids[] = $user['Id'];
}
$avatars = get_avatar_urls($ids);
foreach ($avatars['data'] as $avatar) {
$users[$avatar['targetId']]['AvatarUri'] = $avatar['imageUrl'];
}
file_put_contents('users.json', json_encode($users, JSON_PRETTY_PRINT));
}
The script is executed twice, the first time it will read the names from names.txt, and get the IDS, saving into a users.json file. Then when you execute it again, it will get the IDs from the JSON file, then call the avatars and then merge it back together. The result is a JSON file like so:
{
"3528175625": {
"Id": 3528175625,
"Username": "brenda12322323",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/3c195f46b44d37aa250c0d7c6ae41ded\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
},
"4028593775": {
"Id": 4028593775,
"Username": "eunaodoaisnudhnac",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/0fc96af99739e29c780af459db6c2bd8\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
}
}
Then to output this information on a webpage:
$users = json_decode(file_get_contents('users.json'), true);
foreach ($users as $user) {
echo '<div class="avatar">
<img src="'.$user['AvatarUri'].'" alt="">
<h1>' . $user['Username'] . '</h1>
Profile
</div>';
}
Gives you something like this:
I want to print the file size of the songs using the filesize() function in PHP. This is my code:
<?php
class parent_class{
function file_size($filename){
$size=filesize($filename);
if( $size>1023 && $size<1048575){
$size = $size/1024;
$size = " (". $size." kb)";
}
else if(file_exists($filename) && $size>1048575){
$size = $size/1048576;
$size = " (". round($size, 2) ." mb)";
}
else{
$size = " (". $size." b)";
}
return $size;
}
function displaysong(){
$songs = glob('songs/*.mp3');
foreach ($songs as $song){ ?>
<li class="mp3item">
<a href="<?php echo $song; ?>"> <?php echo basename($song); echo $this->file_size($song);
?></a>
</li>
<?php }
}
function display_playlists(){
$playlists = glob('songs/*.txt');
foreach ($playlists as $file){ ?>
<li class="playlistitem">
<?php echo basename($file) ; echo $this->file_size($file);?>
</li>
<?php }
}
function display_indivsong($file){ ?>
<li class="mp3item">
<?php $path = "songs/$file";?>
<?php echo $file; echo $this->file_size($path);?>
</li>
<?php }
} ?>
<body>
<div id="header">
<h1>190M Music Playlist Viewer</h1>
<h2>Search Through Your Playlists and Music</h2>
</div>
<div id="listarea">
<?php $song = NULL?>
<form method="$_REQUEST" >
<ul id="musiclist" name="playlist">
<?php
$song = new parent_class();
if(!empty($_REQUEST["playlist"])){
$playlist = $_REQUEST["playlist"];
$path = pathinfo($playlist, PATHINFO_EXTENSION);
}
if(empty($playlist)){
$song->displaysong();
$song->display_playlists();
}
else if(!empty($playlist) && $path == "txt"){
$list = $playlist;
$content = file("songs/$list");
foreach($content as $indiv_song){
$song->display_indivsong($indiv_song);
}
}
?>
Everything works alright when I display the songs and their sizes. However, when display_indivsong() is called, filesize() throws an error. Note, inside display_indivsong(), the file is used again and it throws an error. But for the first time it is used in the same file, the sizes of the files are returned with no problems.
In display_indivsong() you are appending "songs/" to the filename before calling $this->file_size($path). Instead of passing $path, you should instead pass the original $file:
function display_indivsong($file){ ?>
<li class="mp3item">
<?php $path = "songs/$file";?>
<?php echo $file; echo $this->file_size($file);?>
</li>
<?php
}
This is because the argument passed to $file already contains the path.
Also, you're reading filenames from a textfile and then looping through those filenames:
$content = file("songs/$list");
foreach($content as $indiv_song){
$song->display_indivsong($indiv_song);
}
This will cause problems if you don't trim the trailing EOL. You should change that line to:
$song->display_indivsong(trim($indiv_song));
I am collecting username, title, video count and comment count from html source. Consider this link
Here is my code to get it:
function getParameter($url)
{
$html = file_get_html($url);
if($html)
{
$containers1 = $html->find('div.v div.v-link v-meta-entry');
foreach($containers1 as $container)
{
$plays = $container->find('v-num'); // get nos of time video played
$item = new stdClass();
foreach($plays as $play)
{
$nos = $play->plaintext;
}
//echo $address;
}
$containers2 = $html->find('div.v div.v-link v-meta va a'); //get user name
foreach($containers2 as $username)
{
$user = $username->plaintext;
}
$containers3 = $html->find('div.v div.v-link a'); //get video title
foreach($containers3 as $title)
{
$title = $title->plaintext;
}
$commentcontainers = $html->find('div.ico-statcomment span'); //get nos of comments
foreach($commentcontainer as $cont)
{
$comments = $cont->plaintext;
}
return $data;
}
}
But it gives this error:
Warning: Invalid argument supplied for foreach() in /var/www/html/scrap/yelp/yoku.php on line 41
any hints for this?
Here is the source code snippet:
<div class="v" >
<div class="v-thumb">
<img src="http://r1.ykimg.com/0515000052B7B3D46714C0318106EA36" alt="和小姚晨约会激动抽筋" />
<div class="v-thumb-tagrb"><span class="v-time">08:56</span></div>
</div>
<div class="v-link">
</div>
<div class="v-meta va">
<div class="v-meta-neck">
<a class="v-useravatar" href="http://i.youku.com/u/UMTQxNDg3NzA4" target="_blank"><img title="嘻哈四重奏" src="http://g3.ykimg.com/0130391F455211D40E180C021BBB97D37C4057-26F4-2F53-A8CD-4A4139415701"></a>
<span class="v-status"> </span>
<a class="v-username" href="http://i.youku.com/u/UMTQxNDg3NzA4" target="_blank">嘻哈四重奏</a>
</div>
<div class="v-meta-title">和小姚晨约会激动抽筋</div>
<div class="v-meta-entry">
<i class="ico-statplay" title="播放"></i><span class="v-num">588万</span> <i title="评论" class="ico-statcomment"></i><span class="v-num">1,290</span> </div>
<div class="v-meta-overlay"></div>
</div>
</div>
Check selectors, documentation here simplehtmldom. For example, I did some changes in selectors.
function getParameter($url)
{
$html = file_get_html($url);
if($html)
{
//we iterate all 'div.v' and select data from every 'div.v' separately
$containersDiv = $html->find('div.v');
foreach($containersDiv as $div)
{
$containers1 = $div->find('div[class=v-meta va] div.v-meta-entry');
foreach($containers1 as $container)
{
$plays = $container->find('.v-num'); // get nos of time video played
$item = new stdClass();
foreach($plays as $play)
{
$nos = $play->plaintext;
}
//echo $address;
}
$containers2 = $div->find('div[class=v-meta va] a'); //get user name
foreach($containers2 as $username)
{
$user = $username->plaintext;
}
$containers3 = $div->find('div.v-link a'); //get video title
foreach($containers3 as $title)
{
$title = $title->plaintext;
}
$commentcontainers = $div->find('div[class=v-meta va] div.v-meta-entry span'); //get nos of comments changed
foreach($commentcontainer as $cont)
{
$comments = $cont->plaintext;
}
}
return $data;
}
}
I wanna get the title and link from this html page :
<div class="gs_r">
<h3 class="gs_rt">
<span class="gs_ctc">[BOOK]</span>
titleA
</h3>
<div class="gs_ggs gs_fl">
<a href="http://exampleA.pdf" onmousedown="return scife_clk(this.href,'gga','gga','1')">
How can I get them?
here's the code :
<?php
include 'simple_html_dom.php';
$url = 'http://example.com';
$html = file_get_html($url);
//get the first link
foreach($html->find('span[class=gs_ctc]')as $Link){
echo $link;
}
foreach($html->find('div[class=gs_ggs gs_fl]')as $docLink){
echo $docLink;
}
?>
For the first link, it's a sibling of the <span>. Try this:
//get the first link
foreach($html->find('span[class=gs_ctc]') as $link){
$link = $link->next_sibling();
echo $link->plaintext;
echo $link->href;
}
As for the 2nd link, it's a child of the <div>:
foreach($html->find('div[class=gs_ggs gs_fl]') as $docLink){
$link = $docLink->first_child();
echo $link->href;
}
EDIT: The 2nd link is grouped with the first, so you can try this:
foreach($html->find('span[class=gs_ctc]') as $link){
foreach($link->parent()->parent()->find('div[class=gs_ggs gs_fl]') as $docLink){
$link1 = $link->next_sibling();
$link2 = $docLink->first_child();
if(preg_match('/\.pdf$/i', $link2->href) === 1){
echo $link1->plaintext;
echo $link1->href;
echo $link2->href;
}
}
}
I have a problem that concerns blog posts and displaying the tag words from another table.
I seem to be able to pull the info out of the tables fine, however when I try to display the posts and the tags, I get one tag per post. In other words if I have 7 tags for a post, I get 7 iteration's of that post each with one tag instead of 1 post with 7 tags.
My Controller ( do have a question about the $this->db->get(posts, tags) is that correct
$this->db->order_by('posts.id', 'DESC');
$where = "publish";
$this->db->where('status', $where);
$this->db->join('tags', 'tags.post_id = posts.id');
$this->db->limit('7');
$query = $this->db->get('posts', 'tags');
if($query->result())
$data = array();
{
$data['blog'] = $query->result();
}
$data['title'] = 'LemonRose';
$data['content'] = 'home/home_content';
$this->load->view('template1', $data);
The view.
$limit = 5; // how many posts should we show in full?
$i = 1; // count
foreach ($blog as $row):
$permalink = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
$url = CompressURL ("$permalink");
$description = $row->title . $row->post;
$twittermsg = substr($description, 0, 110);
$twittermsg .= "...more " . $url;
if ($i < $limit) // we are under our limit
{ ?>
<div class="titlebox">
<div class="title"><? echo ucwords($row->title); ?></div>
<span><? echo $row->date, nbs(10), $row->author; ?></span>
</div>
<div class="clear"></div>
<? $str = str_word_count($row->post, 0);
if ($str >= 500) {
$row->post = html_entity_decode($row->post);
$row->post = $this->typography->auto_typography($row->post); // display?>
<div class="split"> <? echo $row->post = word_limiter($row->post, 480); ?>
<div class="tags"><? echo $row->tag; ?></div>*** These 3 lines seem to be where I am confused and getting the wrong display
<p><h3>More <?php echo anchor("main/blog_view/$row->id", ucwords($row->title)); ?> </h3></p>
<p>Trackback URL: <? echo base_url() . "trackbacks/track/$row->id"; ?></p>
<!-- tweet me -->
<?echo anchor("http://twitter.com/home?status=$twittermsg", 'Tweet'); ?>
This is my first attempt with join and I have very little experience getting the display with implode, if that is the right way to go.
Thank you in advance.
Try
<div class="tags"><? echo implode(', ', $row->tag); ?></div>
and remove the 2 rows before this one.