Liberty53k Posted February 21, 2013 Share Posted February 21, 2013 I'm working through some code to display a tumblr blog on a website. Each of Tumblr's post types are easy enough to get to... <ul id="reblog"> <? foreach ($xml->posts->post as $post) { ?> <li> <? // REGULAR POSTS if ($post['type'] == "regular" ){ echo "This is a regular post"; } // PHOTO POSTS if ($post['type'] == "photo" ){ echo "This is a photo post"; } // LINK POSTS if ($post['type'] == "link" ){ echo "This is a link post"; } // AUDIO POSTS if ($post['type'] == "audio" ){ echo "This is an audio post"; } ?> </li> <? } ?> </ul> What I'm trying to do is to check and see if certain tags are used on a post, and the based on that I'll be formatting the entry a little different. I can list all of the tags, but I'm stumped on how to check the existence of a certain tag in $post->{'tag'} $xml = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt'); <ul id="reblog"> <? foreach ($xml->posts->post as $post) { ?> <li> <? // REGULAR POSTS if ($post['type'] == "regular" ){ echo "type of post: regular<br>"; // THIS DOESN'T WORK -- if ($post->{'tag'} == "infographic") { echo "INFOGRAPHIC"; } else { echo "non-INFOGRAPHIC"; } } ?> </li> <? } ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/ Share on other sites More sharing options...
us2rn4m2 Posted February 21, 2013 Share Posted February 21, 2013 <?php foreach($xml->posts->post as $post) { foreach($post->tag as $tag) { if ($tag == "infographic") { echo "INFOGRAPHIC<br />" ; } else { echo "non-INFOGRAPHIC<br />"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1414028 Share on other sites More sharing options...
Liberty53k Posted February 22, 2013 Author Share Posted February 22, 2013 Thanks for the response… Not exactly what I'm trying to get at... Rather than printing all of the tags as being "infographic" or "non-infographic" -- what I'm trying to do is simply test to see if a certain tag is present --- and do something based on that or NOT if the infographic tag exists for this post.. {do this } else {do this} Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1414075 Share on other sites More sharing options...
us2rn4m2 Posted February 22, 2013 Share Posted February 22, 2013 Your questions associate to your code are not very clear... So I show you the method that I use to check the presence of "certain tag". // check the presence of 'post' if(property_exists($xml, 'posts')) { echo 'post'; } else { // ... } // check the presence of 'regular-title' if(property_exists($xml->posts->post[0], 'regular-title')) { echo 'regular-title'; } else { // ... } // check the presence of 'tag' foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { echo 'tag'; } else { // ... } } Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1414272 Share on other sites More sharing options...
Liberty53k Posted February 25, 2013 Author Share Posted February 25, 2013 (edited) I'm apologize for the lack of clarity. I'm not sure what would make it more understandable. // check the presence of 'tag' foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { echo 'tag'; } else { // ... } } The effect of that code is that it's echoing the word "tag" for every occurrence of a tag on each post OR doing something else if it doesn't exist. What I'm trying to do is more narrow: Each blog post has a set of tags-- (ie "infographic", "news", "real estate", "buying a house", "foreclosure" ...etc) I want to be able to check for the the existence of a particular tag... (ie "infographic") and then if that particular tag exists.. do something. Edited February 25, 2013 by Liberty53k Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1414841 Share on other sites More sharing options...
us2rn4m2 Posted February 28, 2013 Share Posted February 28, 2013 foreach($feed->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { // print_r($post->tag); echo '<b>YES</b>'; } else { echo 'no'; } $i++; } } else { // ... } } Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415656 Share on other sites More sharing options...
us2rn4m2 Posted March 1, 2013 Share Posted March 1, 2013 (edited) "" Edited March 1, 2013 by us2rn4m2 Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415861 Share on other sites More sharing options...
us2rn4m2 Posted March 1, 2013 Share Posted March 1, 2013 (edited) // ! Different output with PHP 5.3 and 5.4 // PHP 5.4 if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') { echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate [1] => vets [2] => Veterans Affairs [3] => VA [4] => home loans [5] => mortgage info [6] => video [7] => Frank Garay [8] => Brian Stevens [9] => buying a house ) SimpleXMLElement Object ( [0] => real estate [1] => dream home [2] => buying a hosue [3] => neighbours [4] => compatibility [5] => lifestyle [6] => Frank Garay [7] => Brian Stevens ) SimpleXMLElement Object ( [0] => real estate [1] => news [2] => hoopla [3] => mortgage rates [4] => video [5] => Frank Garay [6] => Brian Stevens [7] => hype ) SimpleXMLElement Object ( [0] => green [1] => lifestyle [2] => energy conservation [3] => NEST [4] => personal finance [5] => real estate [6] => home systems [7] => Energy Star ) SimpleXMLElement Object ( [0] => real estate [1] => buying a house [2] => internet [3] => www [4] => infographic [5] => home search ) */ // PHP 5.3 echo '<pre>'; print_r($post->tag); /* SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => real estate ) SimpleXMLElement Object ( [0] => green ) SimpleXMLElement Object ( [0] => real estate ) */ Edited March 1, 2013 by us2rn4m2 Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415862 Share on other sites More sharing options...
Liberty53k Posted March 1, 2013 Author Share Posted March 1, 2013 (edited) thank you!!!... I've been able to adapt this. Edited March 1, 2013 by Liberty53k Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415868 Share on other sites More sharing options...
Liberty53k Posted March 1, 2013 Author Share Posted March 1, 2013 thank you!!!... I've been able to adapt this. I spoke too soon.... Here's what I have based off of your post... <? // if the post is tagged "infographic" if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') { echo 'infographic'; } else { echo 'non-infographic'; } $i++; } } ?> What I'm getting is something like this (each bullet represents a single post ): non-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographic non-infographicnon-infographicnon-infographicnon-infographicinfographicnon-infographicnon-infographic non-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographic What I'm trying to get is output that looks like this... non-infographic infographic non-infgraphic I do not NEED to print something for every single tag -- I only need to check the whole bunch of tags to see if something is indeed an "infographic" Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415915 Share on other sites More sharing options...
salathe Posted March 1, 2013 Share Posted March 1, 2013 You could take a slightly different approach, like getting the list of tags and looking to see if any have an "infographic" tag. foreach ($xml->posts->post as $post) { $tags = array_map('strval', $post->xpath('tag')); if (in_array('infographic', $tags)) { // yay, has an infographic tag } else { // aww, not infographic } } Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415920 Share on other sites More sharing options...
us2rn4m2 Posted March 2, 2013 Share Posted March 2, 2013 $xml = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt'); $tag = null; $bunch = array(); echo '<ul>'; foreach($xml->posts->post as $post) { if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') { $bunch[$i] = '<li><b>infographic</b></li>'; $tag = true; } else { $bunch[$i] = '<li>non-infographic</li>'; } $i++; } // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; } } } /** Result: non-infographic non-infographic non-infographic non-infographic infographic non-infographic non-infographic non-infographic non-infographic non-infographic non-infographic YES, has an infographic tag */ Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415944 Share on other sites More sharing options...
Solution us2rn4m2 Posted March 2, 2013 Solution Share Posted March 2, 2013 update // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } $bunch = array(); // add this line // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; } Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1415947 Share on other sites More sharing options...
Liberty53k Posted March 2, 2013 Author Share Posted March 2, 2013 You could take a slightly different approach, like getting the list of tags and looking to see if any have an "infographic" tag. foreach ($xml->posts->post as $post) { $tags = array_map('strval', $post->xpath('tag')); if (in_array('infographic', $tags)) { // yay, has an infographic tag } else { // aww, not infographic } } In my own research, I had come across the in_array function and couldn't figure out to use it since the info was an object??? I tried putting your example in, and couldn't get it working... This seems like the right path though. I've now got something working, & I intend on tinkering some more with this idea, and see if I can work through it. I appreciate your input!! update // check the whole bunch of tags and print "a list" if something is indeed an "infographic" if(in_array('<li><b>infographic</b></li>', $bunch)) { echo implode('', $bunch) . '</ul>'; } $bunch = array(); // add this line // check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic" if($tag) { echo 'YES, has an infographic tag'; $tag = null; } So now using this (and a slight modification) I've finally gotten something working with the desired output. Here's what I've got, in case anyone else want to see it... <ul id="reblog"> <? foreach ($xml->posts->post as $post) { ?> <li> <? // REGULAR POSTS if ($post['type'] == "regular" ){?> <? // if this is a status post with no title... if ($post->{'regular-title'} == "") {?> Regular Post: This is a status update <? ;} ?> <? // regular post with check for the "infographic" tag if(property_exists($post, 'tag')) { $count = count($post->tag); $i = 0; while($i <= $count) { if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') { $bunch[$i] = 'infographic'; $tag = true; } else { $bunch[$i] = 'non-infographic'; } $i++; } if($tag) {?> Regular Post: This is an infographic <? $tag = null;} else {?> Regular Post <? ;} } ?> <? } // PHOTO POSTS if ($post['type'] == "photo" ){ ?> Photo Post <? } // LINK POSTS if ($post['type'] == "link" ){ ?> Link Post <? } // Quote Posts if ($post['type'] == "quote" ){ ?> Quote POST <? } // CHAT POSTS if ($post['type'] == "conversation" ){ ?> Chat Post <? } // AUDIO POSTS if ($post['type'] == "audio" ){ ?> Audio Post <? } // VIDEO POSTS if ($post['type'] == "video" ){ ?> Video Post <? } ?> </li> <? } ?> </ul> /** <ul> <li>Video Post</li> <li>Video Post</li> <li>Photo Post <li>Regular Post</li> <li>Video Post</li> <li>Link Post</li> <li>Regular Post: This is an infographic</li> <li>Video Post</li> <li>Video Post</li> <li>Regular Post</li> <ul> */ Quote Link to comment https://forums.phpfreaks.com/topic/274798-simplexml_load_file-and-tumblr-api/#findComment-1416050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.