Jump to content

Retrieving single xml node value in PHP GCal Script


m1kcal

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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:Untitled.png

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?

Link to comment
Share on other sites

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>";
Link to comment
Share on other sites

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 by m1kcal
Link to comment
Share on other sites

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 by m1kcal
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.