m1kcal Posted August 23, 2014 Share Posted August 23, 2014 Hey guys. I just recently started learning php and I like to learn hands on. So, my first project involves the Google Calendar. I found a script on github that is basically a Google calendar feed and I've been manipulating it to meet my personal needs and expectations, but the only thing I am struggling with is the xml part. All I want to do in this case is get the value from ###TITLE### (after it is sorted by newest date which is done automatically in the script) and store it in a new variable but no matter what I try or do and after reading a bunch of material on xml and php, I still cannot get it to work. If anyone knows how to do this and provide an explanation so I can actually learn I would really really appreciate it. Here is the github script https://github.com/media-uk/GCalPHP/blob/master/gcalphp.php Thank you and I am looking forward to any suggestions or anything. Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/ Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 All I want to do in this case is get the value from ###TITLE### ###TITLE### is replaced by the value of $entry->title on line 144. $entry->title returns the title for the current entry in the google calendar. Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488676 Share on other sites More sharing options...
m1kcal Posted August 23, 2014 Author Share Posted August 23, 2014 Yes, that was the second thing I tried Ch0cu3r, but, when I echo it, it returns all titles, not just the one Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488683 Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 Sounds like you are concatenating $entry->title to a variable. This variable on its own will only return the title for the current event being listed. Can you tell us what you trying to do with the title. Also are you wanting to get the title for all the events or just a specific event? Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488686 Share on other sites More sharing options...
m1kcal Posted August 23, 2014 Author Share Posted August 23, 2014 What I am trying to accomplish is, for example, if the title returns as Sunderland - Manchester United, I want to check if the first word is Sunderland, and if it is, to echo a html img code, something like: if (0 === strpos($entry->title, 'Sunderland')) { echo "<div id='left'><img src='sun.png' /></div>";} Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488691 Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 That should work. It should output an image if $entry->title starts with the word Sundaland. If it didnt where did you place it? Anywhere between lines 122 and 169 should be ok. However do note that how the event is displayed depends on the $event_display variable. This variable contains a string which contains placeholders which will be replaced by their associated values returned from google calander. For example ###DATE### will be replaced by the date for the event. It probably would be a good idea to add a placeholder in that string for where you want the image to appear when the event is outputted. Eg, add a ###TEAM_IMAGE### placeholder $event_display="<P><B>###TITLE###</b> - from ###FROM### ###DATESTART### until ###UNTIL### ###DATEEND### (<a href='###LINK###'>add this</a>)<BR>###WHERE### (<a href='###MAPLINK###'>map</a>)<br>###TEAM_IMAGE### ###DESCRIPTION###</p>"; Now any where between lines 142 and 169 add the following // default replacement ###TEAM_IMAGE### placeholder valuem an empty string $team_image = ''; // if the entry title contains the word Sundardland at the start of the string if (0 === strpos($entry->title, 'Sunderland')) { // replace the ###TEAM_IMAGE### placeholder with a html image $team_image = "<div id='left'><img src='sun.png' /></div>"; } // replace the ###TEAM_IMAGE### placeholder $temp_event=str_replace("###TEAM_IMAGE###", $team_image,$temp_event); Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488698 Share on other sites More sharing options...
m1kcal Posted August 23, 2014 Author Share Posted August 23, 2014 First I would like to thank you for helping me out. I just tried your suggestion/code , and it worked flawlessly. Second, although it works, I don't think I am fully understanding the entire code or process. I understand that the team_image variable is first set to an empty string. Then, in the if statement it is being set to the img tag if it matches the criteria, but then the last part I don't quite get; it's confusing me. In the last line of code, what is the point of using ###_### ? Why can't we just reference it by $team_image for example? Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488717 Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 The purpose of this line is to replace the placeholder with the value of $team_image $temp_event=str_replace("###TEAM_IMAGE###", $team_image,$temp_event); have a read up on str_replace to see how it works. Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488718 Share on other sites More sharing options...
m1kcal Posted August 23, 2014 Author Share Posted August 23, 2014 That is interesting. What if I wanted to use a function instead of a single variable like in the case of $team_image given that the function would have couple echo spitting out , for example function TeamImgs() { ............. ..... echo ("<div id='left'><img src='sun.png' /></div>"); echo ("<div id='right'><img src='sun2.png' /></div>"); } And then this would change to $temp_event=str_replace("###TeamImgs###", $team_image,$temp_event);And also this one to, $event_display="<P><B>###TITLE###</b> - from ###FROM### ###DATESTART### until ###UNTIL### ###DATEEND### (<a href='###LINK###'>add this</a>)<BR>###WHERE### (<a href='###MAPLINK###'>map</a>)<br>###TeamImgs### ###DESCRIPTION###</p>";Would that work, or am I completely wrong here Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488721 Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2014 Share Posted August 23, 2014 Your are completely wrong. What is the purpose of the teamImages function? Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488724 Share on other sites More sharing options...
m1kcal Posted August 25, 2014 Author Share Posted August 25, 2014 Let me show you what I did and the result; the code you gave me few posts ago, I added to it, // default replacement ###TEAM_IMAGE### placeholder valuem an empty string $team_image = ''; // if the entry title contains the team name at the start of the string if (0 === strpos($entry->title, 'Arsenal')) { $team_image = "<div id='left'><img src='logos/sunderland.png' /></div>"; } elseif (0 === strpos($entry->title, 'Aston Villa')) { $team_image = "div id='left'><img src='logos/astonvilla.png' /></div>"; } elseif (0 === strpos($entry->title, 'Burnley')) { $team_image = "div id='left'><img src='logos/burnley.png' /></div>"; } elseif (0 === strpos($entry->title, 'Chelsea')) { $team_image = "div id='left'><img src='logos/chelsea.png' /></div>"; } elseif (0 === strpos($entry->title, 'Crystal Palace')) { $team_image = "div id='left'><img src='logos/crystalpalace.png' /></div>"; } elseif (0 === strpos($entry->title, 'Everton')) { $team_image = "div id='left'><img src='logos/everton.png' /></div>"; } elseif (0 === strpos($entry->title, 'Hull City')) { $team_image = "div id='left'><img src='logos/hull.png' /></div>"; } elseif (0 === strpos($entry->title, 'Leicester City')) { $team_image = "div id='left'><img src='logos/leicester.png' /></div>"; } elseif (0 === strpos($entry->title, 'Liverpool')) { $team_image = "div id='left'><img src='logos/liverpool.png' /></div>"; } elseif (0 === strpos($entry->title, 'Manchester City')) { $team_image = "div id='left'><img src='logos/manchestercity.png' /></div>"; } elseif (0 === strpos($entry->title, 'Manchester United')) { $team_image = "div id='left'><img src='logos/manchesterunited.png' /></div>"; } elseif (0 === strpos($entry->title, 'Newcastle United')) { $team_image = "div id='left'><img src='logos/newcastle.png' /></div>"; } elseif (0 === strpos($entry->title, 'Queens Park Rangers')) { $team_image = "div id='left'><img src='logos/qpr.png' /></div>"; } elseif (0 === strpos($entry->title, 'Southampton')) { $team_image = "div id='left'><img src='logos/southampton.png' /></div>"; } elseif (0 === strpos($entry->title, 'Stoke City')) { $team_image = "div id='left'><img src='logos/stoke.png' /></div>"; } elseif (0 === strpos($entry->title, 'Sunderland')) { $team_image = "div id='left'><img src='logos/sunderland.png' /></div>"; } elseif (0 === strpos($entry->title, 'Swansea')) { $team_image = "div id='left'><img src='logos/swansea.png' /></div>"; } elseif (0 === strpos($entry->title, 'Tottenham Hotspur')) { $team_image = "div id='left'><img src='logos/tottenham.png' /></div>"; } elseif (0 === strpos($entry->title, 'West Bromwich Albion')) { $team_image = "div id='left'><img src='logos/wba.png' /></div>"; } elseif (0 === strpos($entry->title, 'West Ham United')) { $team_image = "div id='left'><img src='logos/westham.png' /></div>"; } elseif (0 === strpos($entry->title, 'MK Dons')) { $team_image = "div id='left'><img src='logos/mkdons.png' /></div>"; } else { $team_image = "div id='left'><img src='logos/unavailable.png' /></div>"; } // replace the ###TEAM_IMAGE### placeholder $temp_event=str_replace("###TEAM_IMAGE###", $team_image,$temp_event); And the result of the modification is this picture: I can't figure out why it is echoing div id='left"> next to the picture. Also, yes, I know there are 3 far more efficient ways I can think off but I went for the simplest one for now. Any suggestion what it could be? Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488822 Share on other sites More sharing options...
CroNiX Posted August 25, 2014 Share Posted August 25, 2014 Probably because you left off the < in <div...in more than one place, too. Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488825 Share on other sites More sharing options...
m1kcal Posted August 25, 2014 Author Share Posted August 25, 2014 Probably because you left off the < in <div...in more than one place, too. Oh wow, I feel embarrassed right now Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488826 Share on other sites More sharing options...
Ch0cu3r Posted August 25, 2014 Share Posted August 25, 2014 Does the title for each event in the calendar resemble the the two football teams playing each other eg the title is like Manchester United v Arsenal If that is the case you could do away with your if/elseif statement completely as long as the filename for the football team logos are named the same as the football team name, are in lower-case and without spaces in the filename. You could simpify your code to // get the team name before the versus symbol list($teamNameLeft, ) = explode(' v ', $entry->title); // generate the file path to the team logo. // - converts the team name to lowercase // - removes spaces in the team name $team_image_path = 'logos/' . str_replace(' ', '', strtolower($teamNameLeft)) . '.png'; // if the logo image does NOT exist, use the unavailable logo if(!file_exists($team_image)) $team_image_path = 'logos/unavailable.png'; // set the HTML for the team logo $team_image = "<div id='left'><img src='$team_image_path' /></div>"; Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488839 Share on other sites More sharing options...
m1kcal Posted August 25, 2014 Author Share Posted August 25, 2014 I tried that, but it is always showing the unavailable.png logo; the name of all pictures are same as the team names in lower case with no spaces. Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488896 Share on other sites More sharing options...
m1kcal Posted August 25, 2014 Author Share Posted August 25, 2014 (edited) I actually got it to work, but, my last obstacle is echoing the other html elements. This is the entire page I need to be echoed out <html> <head> <title>Devils' match</title> <link rel='stylesheet' type='text/css' href='css/main.css' /> </head> </body> <div class='container'> <div class='container-inner'> <div class='datetime'>###FROM###</div> <div class='main'> <div class='left'>###TEAM_IMAGE###</div> <div class='mid'>###TITLE###</div> <div class='right'>###TEAM_IMAGE2###</div> </div> <div class='stadium'>###WHERE###</div> </div> </div> </body> </html> So what I tried is putting the entire code above between the following double quotes: $event_display=" ";The result is this: http://i62.tinypic.com/sg33if.png ; this is what it should be: http://i61.tinypic.com/9kyqsj.png I triple checked all the paths for the css and images everything is fine. Edited August 25, 2014 by m1kcal Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488929 Share on other sites More sharing options...
m1kcal Posted August 26, 2014 Author Share Posted August 26, 2014 (edited) Update from above: $event_display="<html> <head> <title>Devils' match</title> <link rel='stylesheet' type='text/css' href='main.css' /> </head> </body> <div class='container'> <div class='container-inner'> <div class='datetime'>###FROM###</div> <div class='main'> ###TEAM_IMAGE### <div class='mid'>###TITLE###</div> ###TEAM_IMAGE2### </div> <div class='stadium'>###WHERE###</div> </div> </div> </body> </html>"; The only thing that is messed up is the ###FROM### which is in <div class='datetime'> . For some reason is all out of position unlike the rest, http://i58.tinypic.com/28gz7lc.png Edited August 26, 2014 by m1kcal Quote Link to comment https://forums.phpfreaks.com/topic/290605-retrieving-single-xml-node-value-in-php-gcal-script/#findComment-1488940 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.