I get some links from feed with this code on simplepie :
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links = $item->get_permalink();
echo $links;
endforeach; endif;
thats result me:
http://link1....
http://link2....
http://link3....
i want to put each link in separate variable like :
$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';
thanks , mori
$i = 0;
foreach($feed->get_items(0,3) as $item)) {
${'link' . ++$i} = $item->get_permalink();
}
Maybe what you want is array variable?
try this:
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links[] = $item->get_permalink();
endforeach; endif;
if ($check) :
$i=0;
foreach ($feed->get_items(0,3) as $item):
$links.$i = $item->get_permalink();
echo $links.$i;
$i++;
endforeach; endif;
I think it may works...
Related
I am just starting to learn PHP and to compound the issue I am trying to modify existing code to match what I am attempting to do. I have multiple variables I am trying to define all while partially using a counter. My problem is appending the count to my individually defined variables. Also, I'm sure there is a cleaner way to do this (like a loop or nested array), I just can't see how.
<?php
$pageId0 = opt('first_tab_page');
$post0 = get_post($pageId0);
$content0 = apply_filters('the_content', $post0->post_content);
$icon0 = wp_get_attachment_image_src( get_post_thumbnail_id( $post0->ID ), 'full' );
$pageId1 = opt('second_tab_page');
$post1 = get_post($pageId1);
$content1 = apply_filters('the_content', $post1->post_content);
$icon1 = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'full' );
$pageId2 = opt('third_tab_page');
$post2 = get_post($pageId2);
$content2 = apply_filters('the_content', $post2->post_content);
$icon2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'full' );
?>
<div>
<?php echo $icon0[0]; ?>
</div>
<div>
<?php echo $icon1[0]; ?>
</div>
<div>
<?php echo $icon2[0]; ?>
</div>
<?php
$tab_position = opt('tab_position');
if ($tab_position == '' || count($tab_position) != 3) {
$tab_position = array(0, 1, 2);
}
for($i=0; $i < count($tab_position); $i++)
{
$selected = '';
if (opt('default_selected_tab') == $tab_position[$i]){
$selected = 'class="selected"';
}
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $content//should be 0 to start; ?>
</a>
<?php } ?>
Here is a short example on using arrays. comment in code
// Storing tab names to an array, just add more tabs if required
// and details for all those will be loaded in the following arrays
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
//declare arrays to store (not required, but better practice)
$pageids = array();
$posts = array();
$content = array();
$icons = array();
//Iterate through the tabs array
foreach ($tabs as $tab){
//Store page id and post in a variable, we require it
$pageid = opt($tab);
$post = get_post($pageid);
// Store pageid in pageids. note the [] this means it will store pageid at next index
//also store other items
$pageids[] = $pageid;
$posts[] = $post;
$contents[] = apply_filters('the_content', $post->post_content);
$icons[] = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
}
now $icons[0] will contain first icon $icons[1] the second one and so on. Same thing applies to other variables.
Note: this code is just typed in here and not checked. Please fix if there is any syntax errors. Also note this is one way and there are more ways.
But I would suggest keeping data of each page together.
Edit: Here is how you can keep things together (only relevant parts shown)
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
$pages = array();
foreach ($tabs as $tab){
$pageid = opt($tab);
$post = get_post($pageid);
$content = apply_filters('the_content', $post->post_content);
$icon = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
//Store everything to $pages array
$pages[] = array('pageid' => $pageid, 'post' => $post, 'content' => $content, 'icon', $icon);
}
foreach ($pages as $page){
?>
<div>
<?php echo $page['icon'][0]; ?>
</div>
<?php } ?>
foreach ($pages as $page){
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $page['content']; //echo the content of page ?>
</a>
<?php } ?>
Instead of foreach to display you can use index also like
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[0]['content']; //echo the content of the first page ?>
</a>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[1]['content']; //echo the content of the second page ?>
</a>
I guess there is no other way then (nested) arrays. Also I'm thinking that arrays are a acceptable solution.
foreach ($tabs as $tab) {
$tab['content'] = 'somecontent';
$tab['counter'] = 'counter';
}
or
foreach ($tabs as $tab) {
$tab = [
"content" => "content",
"counter" => "counter"];
}
I'm not sure what you are trying to do as your question is not very clear to me. But i think you are trying to echo $content in your last line of code. If yes, just put your all your variables $content1, $content2 and $content3 in an array like below:
$contents = array($content1, $content2, $content3);
Then use foreach loop and traverse this array like this.
$count = 0;
foreach($contents as $content) {
echo $content.$count;
$count++;
}
Try and let me know if it works for you.
I'm trying to get data from an XML file using simple_xml , so far I can get all the data except the images . How can I call a single image name ?
<?php
$ur="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($ur);
foreach ($xml->property as $property):
var_dump($property->images->image);
echo 'images->image">'; // this is not displaying
endforeach;?>
My code output as the image below . How can i display image number 1
public 1 => string 'http://media2.jupix.co.uk/v3/clients/657/properties/1356/IMG_1356_9_large.jpg' (length=77)
I think SimpleXMLElement::xpath can do what you are looking for:
You can give this a try:
<?php
$ur="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($ur);
$image = $xml->xpath('//property/images/image[#modified="2014-07-23 14:22:05"]')[1]->__toString();
var_dump($image);
Or you can loop through all the images and check for the name the you are looking for:
$images = $xml->xpath('//property/images/image');
foreach ($images as $image) {
$url = $image->__toString();
if (false !== strpos($url, "_9_large.jpg")) {
var_dump($url);
}
}
If you want to get the second image of each /images section, then you could do it like this:
$images = $xml->xpath('//property/images');
foreach ($images as $image) {
if (isset($image->children()[1])) {
var_dump($image->children()[1]->__toString());
}
}
Thanks Guy I found solution to my problem .
Looking at the back at the question it seems I did not put it in a proper way
All i wanted is to display images within that section. Xpath was not necessary But i have learned from it . Here is my solution if you can improve it you are much welcome.
$url ="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($url);
foreach ($xml->property as $property):
?>
<li>
<h3> <?php echo $property->addressStreet;?> </h3>
<?php
$imgCount = count($property->images->image);
for ($i=0; $i < $imgCount; $i++) { ?>
<img src="<?php echo $property->images->image[$i];?>">
<?php } ?>
<p><?php echo limit_text($property->fullDescription,30);?></p>
<h4>£ <?php echo $property->price;?> </h4>
</li>
<?php endforeach; ?>
I tried to change the positions of the loadObjectList(). but it is not working. can anyone help me to solve this problem?
This is the code i used.
$catID = 8;
//echo $catID;
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
$db = JFactory::getDBO();
$db->setQuery("SELECT title,alias FROM #__content WHERE catid = ".$catID);
$articles = $db->loadObjectList(); ?>
<div>
<?php foreach($articles as $article){
$title = $article->title;
if($title == $page_title){?>
<h4><?php echo $article->title; ?></h4>
<?php
}else{ ?>
<h4><?php echo $article->title; ?></h4>
<?php }
}
?>
</div>
what i want to do is, i want to display the page title as the first element of the list. Thats means if condition's item should display first.
can anyone helpe me?
A quick dry solution is to add 2 foreach loops and first display the value if condition match and after the rest values.
<?php
foreach($articles as $article){
$title = $article->title;
if($title == $page_title){
echo '<h4>'. $article->title .'</h4>';
}
}
foreach($articles as $rest_articles){
$title = $rest_articles->title;
if($title != $page_title){
echo '<h4>'. $rest_articles->title .'</h4>';
}
}
?>
A better solution is to store values in a new array and display them afterwards.
Hope this helps
Just had a double take here. Multiple foreach loops can be used, however are not required. Try the following:
<?php
foreach($articles as $article) {
$title = $article->title;
$alias = $article->alias;
if($title == $page_title) {
echo '<h4>'. $title .'</h4>';
}
else {
echo '<h4>'. $title .'</h4>';
}
}
?>
Model
function Show_all_products()
{
return $this->db->get('printer')->result();
}
View
The table contents is being echoed in a loop
$i = -1;
echo '<ul class="products">'; foreach($products as $product) :
if($i % 11 == 10) echo '</ul><ul class="products">';
?>
<li><?php echo $product->name; ?> </li>
<?php
$i += 1;
endforeach;
echo "</ul>";
The product names are saved as Something_Something_Blah I cannot modify the product names as they are configured to show clean URL's and Breadcrumbs.
The issue is that the links in this view show as Something_Something_Blah
I tried to do a str_replace as $product = str_replace('_',' ', $product); However this isn't working.
How do i strip the '_' and insert \s ?
I see you have <?php echo $product->name; ?> which would mean $product is an object.
So you should call str_replace('_', ' ', $product->name);
<?php
$i = -1;
echo '<ul class="products">'; foreach($products as $product) :
$product->name = str_replace('_',' ', $product->name);
if($i % 11 == 10) echo '</ul><ul class="products">';
?>
<li><?php echo $product->name; ?> </li>
<?php
$i += 1;
endforeach;
echo "</ul>";
?>
str_replace must be called from within the foreach or an error will be returned. That was my mistake.
As per PHP documentation, str_replace() accepts a string or array as it's third argument. You are passing it an object. You should pass $product->name to it.
Furthermore, please strive to embed PHP in your HTML and not HTML in your PHP. You're going to find yourself echoing everything. You don't need to manually create the $i iterator either, because the foreach loop will create one for you, provided you use the foreach($x as $key => $val) syntax. You can lose the <?php echo $var;?> for <?= $var;?> too, AND concatenate with the . symbol:
<ul class="products">
<? foreach($products as $pK => $pV):?>
<? if($pK % 11 == 10):?></ul><ul class="products"><? endif;?>
<li>
<?= $pV->name;?>
</li>
<? endforeach;?>
</ul>
I have looked and looked and tried to find an answer for what I've been looking for, but I have yet to see an answer for this:
I am trying to generate a Wordpress loop that takes all the posts from a single category and displays them three at a time inside <li></li> tags.
Output should look like this:
<li>My post title | Another Title | Third title</li>
<li>The next post title | A different post | Post #6</li>
<li>And so on | And so forth</li>
I need this to loop through all the entries in the category until finished, and then exit the loop.
My code is completely non-working at this point, but I've provided what I'm working with below. If anyone has any solution to this, I'd love to give mad props to you, as this has hounded me for three days without any solution so far.
<?php // Loop through posts three at a time
$recoffsetinit = '0';
$recoffset = '3';
query_posts('cat=1&showposts=0');
$post = get_posts('category=1&numberposts=3&offset='.$recoffsetinit.');
while (have_posts()) : the_post();
?>
<li>
<?php
$postslist = get_posts('cat=1&order=ASC&orderby=title');
foreach ($postslist as $post) : setup_postdata($post);
static $count = 0; if ($count == "3") { break; } else { ?>
<?php $count++; } ?>
<?php endforeach; ?>
<?php $recoffsetinit = $recoffset + $recoffsetinit; ?>
</li>
<?php endwhile; ?>
I hacked up your solution to make it work. It took a little doing, since my code-fu is not what you'd call "good." Here's the solution:
<ul>
<?php
query_posts('category=1&showposts=0');
$posts = get_posts('category_name=my_cat&order=ASC&orderby=title&numberposts=0');
$postsPerLine = 3;
$currentPostNumber = 0;
foreach ($posts as $post) :
if ($currentPostNumber == 0) {
echo '<li>';
}
?>
<?php
$currentPostNumber++;
if ($currentPostNumber >= $postsPerLine) {
$currentPostNumber = 0;
echo '</li>';
}
endforeach;
?>
</ul>
Thanks for the input!
No wordpress to test with, and no time, but something like this might be a better way of going about it?
<?php
$postList = get_posts('cat=1&order=ASC&orderby=title');
$postsPerLine = 3;
echo "<ul>";
echo buildPosts($postList, $postsPerLine);
echo "</ul>";
function buildPosts($list, $perLine) {
$out = '';
$currentPostNumber = 0;
foreach ($list as $post) {
if ($currentPostNumber == 0) {
$out .= '<li>';
}
$out .= "<a href='" . the_permalink() . "'></a> ";
$currentPostNumber++;
if ($currentPostNumber <= $perLine) {
$currentPostNumber = 0;
$out .= '</li>';
}
}
return $out;
}
?>
Just snag all the posts for a category, at once, then iterate over it. Create a link to every post, toss in the separator, and on every third post start a new <li>
<ul>
<?php
global $post;
$postsPerLine = 3;
$counter = 0;
$myposts = get_posts('category=1&orderby=title&order=ASC');
foreach($myposts as $post) :
echo (++$counter % postsPerLine) ? : '<li>';
?>
<?php the_title(); ?></li>
<?php
echo ($counter % postsPerLine) ? ' | ' : '</li>';
endforeach;
?>
</ul>