I cant get this to work, is it possible? I need to display an banner ad after every 25 wallpapers...
<?php
$xml=simplexml_load_file("wallpaper.xml") or die("Error: Cannot create object");
foreach (array_chunk($xml->children, 25, true) as $array) {
foreach ($array as $wall)
{
echo "<a href='" . $wall->url . "' target='_blank'><img src='"$wall->thumbnail . "' alt='Wallpaper' /></a> \n";
}
echo '<div id="banner">Test</div>';}
?>
When I write things like this I use the $i = 0 thing. Then I use the $i = count++ after each foreach. Then I do the if $i = 25 { echo 'img banner code' }.
This above (unfinished) code counts the number of foreach iterations, and when it has reached 25 it echoes your banner images.
Is the code you want? If so I might finish it for you.
Related
In my for loop for ($i = 1; $i <= 3; $i++) i have have html code that will be echod 3 times. The html also using PHP objects ($help).
$help has 3 things, $help->url_1, $help->text_1, $help->icon_1. But through the loop, I want it so that when for example $i is at 2, i want to use $help->url_2, etc. How can i sort of increment the variable name in the echo string of in the loop?
<?php
class Help
{
public $url;
public $text;
public $icon;
}
$help1 = new Help();
$help1->url = 'your url';
$help1->text = 'your text';
$help1->icon = 'url to your icon';
$helps[] = $help1;
//Repeat for other helps (help2, help3, etc.)
?>
Once you have an array of objects, you only need to loop into it using a foreach loop :
foreach ($helps as $help) {
echo "<h3>" . $help->url . "</h3>";
echo "<p>" . $help->text . "</p>";
echo "<img src='" . $help->icon . "' alt='your alt'/>";
}
I have a case where I am using the code below to populate a template of sorts. Within the templates' javascript I would individually check the data attribute field I setup, essentially causing me to have multiple JS files instead of one that is shared. I then thought I could use a generic name field too, but prepend a number through the loop.
For example, with the line of code below where the `name="testField". I want to see if there is a way that I can add a number, but auto increment it through the loop with php.
Is this possible?
echo '<div class="markerItem" name="testField' . $number . '" "data-marker="' . $marker_data . '">';
PHP Code
if ($marker_stmt = $con->prepare($sql_marker)) {
$marker_stmt->execute();
$marker_rows = $marker_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="projMarker">';
foreach ($marker_rows as $marker_row) {
$marker_solution = $marker_row['solution'];
$maker_item = $marker_row['subSolution'];
$marker_data = $marker_row['subSolution'];
echo '<div class="markerItem" data-marker="' . $marker_data . '">';
echo $marker_item;
echo '</div>';
}
}
echo '</div>';
if ($marker_stmt = $con->prepare($sql_marker)) {
$marker_stmt->execute();
$marker_rows = $marker_stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<div id="projMarker">';
foreach ($marker_rows as $key=>$marker_row) {
$marker_solution = $marker_row['solution'];
$maker_item = $marker_row['subSolution'];
$marker_data = $marker_row['subSolution'];
echo '<div class="markerItem" name="testField_'.$key.'" data-marker="' . $marker_data . '">';
echo $marker_item;
echo '</div>';
}
}
echo '</div>';
Using this, $key is assigned from the array index which will be a number starting from 0 and ending at count($marker_rows)-1.
Suprisingly, I couldn't find an appropriate duplicate for this.
You can easily increment or decrement variables in PHP.
$number = 0;
foreach ($marker_rows as $marker_row) {
...
$number++; // $number will now be $number+1
}
You can use $number++ directly in your attribute (if concatenating) as this will return the current $number then increment the value.
I am getting trends using the Twitter API.
My current code displays all the trends for a given location identified by WOEID, 2295424 for example. How do I need to change it to display only the top five trends?
<?php
$jsonop = $connection->get("trends/place", array('id' => '2295424'));
//var_dump($statuses);
foreach ($jsonop as $trend) {
echo "As of {$trend->created_at} in ";
foreach($trend->locations as $area)
echo "{$area->name}";
echo " the trends are:<br />";
echo "<ul>";
foreach($trend->trends as $tag)
echo "<li>{$tag->name}</li>";
echo "</ul>";
}
?>
This isn't really specific to Twitter. All you really need to know for this is how to break out of a PHP loop after X iterations. There are various ways to do that. A simple way is by keeping track of a counter and using a break statement to exit the loop when it reaches the desired value.
<?php
$jsonop = $connection->get("trends/place", array('id' => '2295424'));
//var_dump($statuses);
foreach ($jsonop as $trend) {
echo "As of {$trend->created_at} in ";
foreach($trend->locations as $area) {
echo "{$area->name}";
echo " the trends are:<br />";
echo "<ul>";
$counter = 0;
foreach($trend->trends as $tag) {
$counter++;
echo "<li>{$tag->name}</li>";
if ($counter == 5) break;
}
echo "</ul>";
}
}
?>
I'm using the following PHP code to get the comments for a specific video:
<?php
$vid = "G0k3kHtyoqc";
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;
$entry = simplexml_load_file($feedURL);
$gd = $entry->children('http://schemas.google.com/g/2005');
if($gd->comments->feedLink){
$attrs = $gd->comments->feedLink->attributes();
$commentsURL = $attrs['href'];
$commentsCount = $attrs['countHint'];
}
if($commentsURL && $commentsCount > 0){
$commentsFeed = simplexml_load_file($commentsURL);
echo "<ol>";
foreach($commentsFeed->entry as $comment){
echo "<li>";
echo "<a target='_blank' href='http://www.youtube.com/user/" . $comment->author->name . "'>";
echo $comment->author->name;
echo "</a>";
echo " - " . $comment->content;
echo "</li>";
}
echo "</ol>";
}
?>
The problem with the code above is that it only gets the most recent 24 comments. I need a way to paginate through all comments.
Your help is much appreciated.
Thanks
Use the "start-index" param. Begins on 1, and depending on the comments count, add [comments-count] to start-index param.
For example:
First page of comments, getting 25 comments per page, use max-results=25 and start-index=1
Second page of comments, getting 25 comments per page, use max-results=25 and start-index=26
And so on =)
Regards!
What i want to do is read rssfeed, so I already did it, but I display as foreach loop, so how can I only display 5 records ? now I get more than 10 records, but I only need top 5 records, Isn't anyway php, javascript or jquery make it only show 5 records?
here is my code to read the rss file:
function getrssFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href = '$entry->link' title='$entry->title'><h3>" . $entry->title . "</h3></a>" . $entry->pubDate . "<br /><br />" . strip_tags($entry->description) . "</li>";
}
echo "</ul>"; }
getrssFeed("http://thestar.com.my.feedsportal.com/c/33048/f/534555/index.rss");
thank you
the easiest way would be to stop your loop after 5 iterations:
$i = 0;
foreach($x->channel->item as $entry) {
// do something
$i++;
if($i==5){
break;
}
}
another (more beautiful) way would be to use a for-loop instead of foreach:
for($i=0; $i<=min(5, count($x->channel->item)); $i++) {
$entry = $x->channel->item[$i];
// do something
}
EDIT :
thanks to Juhana, i changed the code to take that into account.
Try putting a counter in your foreach loop with an if statement to check when the counter is over 5. If the counter is under 5 -> display RSS post, counter++. Else -> Exit loop.
Why not use a simple for loop instead?
for($i = 0, $i <= 5, $i++) {
echo "<li><a href = '$x->channel->item[$i]->link' title='$x->channel->item[$i]->title'><h3>" . $x->channel->item[$i]->title . " </h3></a>" . $x->channel->item[$i]->pubDate . "<br /><br />" . strip_tags$x->channel->item[$i]->description) . "</li>";
}
You can use jQuery feed Plugin.
$('div.feed').Feed({
count:5;
});
<div class="feed" link="http://thestar.com.my.feedsportal.com/c/33048/f/534555/index.rss" ></div>
Feeds would be loaded to container feed.