I am trying to get two foreach loop by explode with two delimiters <> and "\n" but getting error. Warning: Invalid argument supplied for foreach()
Here is my code
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
?>
The value entering into textarea something like this Title here<>this is the first scooter ever made.
Title here 2<>another line for specification In fact I would like to make it like <title 1> here detail text
Thanks a lot
actually you should do something like this
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($descs as $desc){
$title = explode('<>', $desc);
echo '<dt>' . $title[0] . '</dt>';
for($i=1; $i<=count($title); $i++){
echo '<dd>' . $title[$i] . '</dd>';
}
}
echo '</dl>';
?>
The $descs variable isn't an array because the first foreach loop sets $descs.
See this line :
foreach($titles as $title => $descs){
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
$descs = explode("\n", $descs);
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
Related
i have array
$id = $atts['home_blog_id'];
$slug = str_replace( array('post:','post'), array('',''), $id);
if echo $id have array: post:du-an and post:tin-tuc
if echo $slug have array: du-an and tin-tuc
how to foreach $slug to list item as
<li>du-an</li>
<li>tin-tuc</li>
Thanks.
Explode the items to an array the either foreach as you say or use implode.
$atts['home_blog_id'] ="post:du-an,post:tin-tuc";
$id = $atts['home_blog_id'];
$slug = str_replace( array('post:','post'), array('',''), $id);
echo "<li>" . implode("</li><li>", explode(",", $slug)) . "</li>";
//<li>du-an</li><li>tin-tuc</li>
https://3v4l.org/4j5OK
As Nigel requsted, a foreach version.
foreach(explode(",", $slug) as $val){
echo "<li>" . $val . "</li>";
}
I have an array in the following format.
$data = array(
1=>array('img'=>'1.png','title'=>'title1','desc'=>'desc1'),
2=>array('img'=>'2.png','title'=>'title2','desc'=>'desc2'),
1=>array('img'=>'3.png','title'=>'title3','desc'=>'desc3'),
);
Here is the final output I need,
<img src="1.png">
<h1>title1</h1>
<p>desc1</p>
<img src="2.png">
<h1>title2</h1>
<p>desc2</p>
.........
How can i create it? Thanks for the help.
Use a foreach loop, like so:
foreach( $data as $item) {
echo '<img src="' . $item['img'] . '">';
echo '<h1>' . $item['title'] . '</h1>';
echo '<p>' . $item['desc'] . '</p>';
echo "\n";
}
Simply use your array like
foreach($data as $item){
echo $item['title'];
.....
}
This will give you the logic. Now you can apply the img and tags properly
<?php
foreach($data as $item) {
?>
<img src="<?=$item['img']?>">
<h1><?=$item['title']?></h1>
<p><?=$item['desc']?></p>
<br />
<?php } ?>
Another option here.
here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)
I have the following snippet of code:
function getFeed($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'>" . $entry->title . "</a></li>";
echo "<li>$entry->content</li>";
echo "</ul>";
}
It works EXCEPT the $entry->content
That part doesn't register. In the actual feed the tag is listed as <content:encoded> but I can't get it to feed. Any suggestions?
The Tag name here is "encoded".
Try this:
$url = 'put_your_feed_URL';
$rss = new DOMDocument();
$rss->load($url);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'content' => $node->getElementsByTagName('encoded')->item(0)->nodeValue
);
array_push($feed, $item);
}
In <content:encoded>, content is the namespace and encoded is the tag name.
You have to use SimpleXMLElement::children. See the output of
var_dump($entry->children("content", true));
I'll suggest you the following code:
function getFeed($feed_url) {
$feeds = file_get_contents($feed_url);
$feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
$feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
$rss = simplexml_load_string($feeds);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
echo "<li>$entry->contentEncoded</li>";
echo "</ul>";
}
Hope this works for you.
.... PHP example
<?php
// --------------------------------------------------------------------
$feed_url = 'http://www.tagesschau.de/xml/rss2';
$xml_data = simplexml_load_file($feed_url);
// --------------------------------------------------------------------
$i=0;
foreach($xml_data->channel->item as $ritem) {
// --------------------------------------
$e_title = (string)$ritem->title;
$e_link = (string)$ritem->link;
$e_pubDate = (string)$ritem->pubDate;
$e_description = (string)$ritem->description;
$e_guid = (string)$ritem->guid;
$e_content = $ritem->children("content", true);
$e_encoded = (string)$e_content->encoded;
$n = ($i+1);
// --------------------------------------
print '<p> ---------- '. $n .' ---------- </p>'."\n";
print "\n";
print '<div class="entry" style="margin:0 auto; padding:4px; text-align:left;">'."\n";
print '<p> Title: '. $e_title .'</p>'."\n";
print '<p> Link: '. $e_link .'</p>'."\n";
print '<p> Date: '. $e_pubDate .'</p>'."\n";
print '<p> Desc: '. $e_description .'</p>'."\n";
print '<p> Guid: '. $e_guid .'</p>'."\n";
print '<p> Content: </p>'."\n";
print '<p style="background:#DEDEDE">'. $e_encoded .'</p>'."\n";
print '</div>'."\n";
// --------------------------------------
print '<br />'."\n";
print '<br />'."\n";
$i++;
}
// --------------------------------------------------------------------
?>
if you want to see the content HTML Source Code in your Browser, use eg:
print '<pre style="background:#DEDEDE">'. htmlentities($e_encoded) .'</pre>'."\n";
:=)
The working answer for this is just:
$e_content = $entry->children("content", true);
$e_encoded = (string)$e_content->encoded;
Using SimpleXmlElement or loading via simplexml_load_file you could access it via $entry->children("http://purl.org/rss/1.0/modules/content/")->encoded, just remember to cast it to a string:
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
echo "<li>" . (string)$entry->children("http://purl.org/rss/1.0/modules/content/")->encoded . "</li>";
}
I have the following output (via link) which displays the var_dump of some XML im generating:
http://bit.ly/aoA3qY
At the very bottom of the page you will see some output, generated by this code:
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
}
echo $title;
echo $title2;
For some reason $title2 only outputs once, where there are multiple entries?
Im using $xml = simplexml_load_string($data); to create the xml.
You re-assign a value to $title and $tile2 in each iteration of the foreach loop. After the loop is finished only the last assigned value is accessible.
Possible alternatives:
// print/use the values within the loop-body
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
echo $title, ' ', $title2, "\n";
}
// append the values in each iteration to a string
$title = $title2 = '';
foreach ($xml->feed as $entry) {
$title .= $entry->title . ' ';
$title2 .= $entry->entry->title . ' ';
}
echo $title, ' ', $title2, "\n";
// append the values in each iteration to an array
$title = $title2 = array();
foreach ($xml->feed as $entry) {
$title[] = $entry->title;
$title2[] = $entry->entry->title;
}
var_dump($title, $title2);