calling a variable in php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have written two seperate scripts in php. one of them is parsing xml data and the other one is date sorting.
xml parsing
<?php
$date = date ("d.m.Y");
$url = "http://www.cbar.az/currencies/" .$date. ".xml";
$doc = new DOMDocument();
$doc->load($url);
$valyutalar = $doc->getElementsByTagName( "Valute" );
foreach( $valyutalar as $valyuta )
{
$nominal = $valyuta->getElementsByTagName( "Nominal" );
$nom = $nominal->item(0)->nodeValue;
$code = trim($valyuta->getAttribute('Code'));
$value = $valyuta->getElementsByTagName( "Value" );
$val = $value->item(0)->nodeValue;
echo "<b>".$code." - " .$val. "AZN\n</b><br>-----------------------<br>";
}
?>
date
<?php
date_default_timezone_set('UTC');
$beginning = "25.11.1993";
$ending = date("d.m.Y");
$date = $beginning;
while(strtotime($date) <= strtotime($ending))
{
/* echo "$date<br />"; */
$date = date ("d.m.Y", strtotime("+1 day", strtotime($date)));
}
?>
i want to join thetse two, for parsing the data from 1993 to now. i`m aware of that i have to put the parser in a while loop but have no any idea exactly where to put. can anyone give an archimedean point just to start? -))

If $date is iterating +1 day in your while loop correctly, then:
while(strtotime($date) <= strtotime($ending))
{
/* echo "$date<br />"; */
$date = date ("d.m.Y", strtotime("+1 day", strtotime($date)));
$url = "http://www.cbar.az/currencies/" .$date. ".xml";
$doc = new DOMDocument();
$doc->load($url);
$valyutalar = $doc->getElementsByTagName( "Valute" );
foreach( $valyutalar as $valyuta )
{
$nominal = $valyuta->getElementsByTagName( "Nominal" );
$nom = $nominal->item(0)->nodeValue;
$code = trim($valyuta->getAttribute('Code'));
$value = $valyuta->getElementsByTagName( "Value" );
$val = $value->item(0)->nodeValue;
echo "<b>".$code." - " .$val. "AZN\n</b><br>-----------------------<br>";
}
}$u
?>
so if $date is incrementing, each time the while loop is triggered, it will load your $url with the next iteration of $date

Related

compare date only part from the string

I have two strings (read from an XML file), say '2014-10-17T11:17:44' and '2014-10-17T16:19:15'. I want to compare them using only the date part (without time). I already tried ...script below
if(file_exists($xmlFullFilename)) {
if($entryTimeNode->format("Y-m-d") = $entryTimeNode->format("Y-m-d")){
( appends to the exisiting file ....)
$xmlDoc = new DomDocument();
$tmp = split(" ", $entryTime);
$dateString = $tmp[0] . "T" . $tmp[1];
$entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
}
else {
//create a new xml file .
}}
XML FILE
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="2" Date="2014-10-24" Time="12:05:39" FileName="2014-10-24_Brook_Retail_Park.xml">
<Incident>
<EntryTime>2014-10-17T11:17:44</EntryTime>
</Incident>
<Incident>
<EntryTime>2014-10-17T16:19:15</EntryTime>
</Incident></Incidents>
My soluction is
$date = "2014-10-17T11:17:44";
$date2 = "2014-10-17T16:19:15";
$date = new DateTime($date);
$date2 = new DateTime($date2);
$diff = $date->diff($date2);
if((int)$diff->format('%y%m%d')==0)
{
// Dates are equal
}

How do I parse a remote web page and extract strings from a list of <span> fields [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have got a problem with printing the list of strings from the arrays. I'm using the $test variable to print the list of strings. When I'm trying to print the strings from the arrays, I will get the short strings with one letter from each array which I don't want that. I want to print the full strings from each array using with $count to count the values.
Here is the input:
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSLLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
When I try this code:
<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = true;
function getState($string)
{
$ex = explode(" ",$string);
return $ex[1];
}
$baseUrl = file_get_contents('http://www.myownsite.com/get-listing.php'
);
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
#$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
for ($i = 1; $i < 70; $i++)
{
$time_arr[] = $xpath->query("*/span[#id='time".$i."']");
$programme_arr[] = $xpath->query("*/span[#id='title".$i."']");
}
$programme_title = array();
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title[] = $programme1;
}
foreach($time_arr as $time)
{
//$test = implode(' ', $programme_title);
//$tester[] = $test;
//echo $tester;
$test = implode(' ', $programme_title);
echo $test[$count];
}
}
?>
Do you know how I can print the full strings from each array without printing one letter
when using with $test variable?
Edit: Here is the output:
Sister Act Sister Act 2: Back in the Habit Mamma Mia!Forrest Gump(D,L,V,S)
The Blind SideJoel OsteenJoyce Meyer: Enjoying Everyday LifeShaun T's
Focus T25Summer Sexy With T25!Total Gym for $14.95Dr. Ordon's Secret!
Sleep Better!Steam And Spray The Dirt Away ... SHARK Style!Shaun T's Focus T25
Airbrushed BeautyJoseph PrinceLife Today With James Robison - Mark Driscoll 1
Joyce Meyer: Enjoying Everyday LifeShaun T's Focus T25That '70s Show - I Love Cake
That '70s Show - Sleepover That '70s Show - Eric Gets Suspended That '70s Show -
Red's BirthdayStill Standing - Still Thankful700 Club InteractiveThe 700 ClubGil
more Girls - Haunted Leg8 Simple Rules -
and so on...
Edit: Here is the update code:
$programme_title = array();
foreach($programme_arr as $programme)
{
$programme1 = $programme->item(0)->nodeValue;
$programme_title[] = $programme1;
}
foreach($time_arr as $time)
{
echo $programme_title[$count];
//$test = implode(' ', $programme_title);
//echo $test[$count];
}
You are calling
$time_arr[] = $xpath->query("*/span[#id='time".$i."']");
before it is even defined. You need to define $xpath as parser. Something like this
#$domdoc->loadHTML($baseUrl);
$xpath = DOMXPath( $domdoc );
then you will be able to request data from the document.
Now here is what might give you the result as expected. I did use the provided URL
<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = true;
function getState($string)
{
$ex = explode(" ",$string);
return $ex[1];
}
// read the remote file
$baseUrl = file_get_contents('http://some-server.com/get-listing.php?channels=ABC%20FAMILY&id=101');
// create the parser
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
#$domdoc->loadHTML($baseUrl);
$xpath = new DOMXpath($domdoc);
//$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;
for ($i = 1; $i < 5; $i++){
$time_arr[] = $xpath->query("*/span[#id='time".$i."']");
$programme_arr[] = $xpath->query('//span[#id="title'.$i.'"]');
}
$programme_title = array();
foreach($programme_arr as $programme) {
$programme1 = $programme->item(0)->nodeValue;
$programme_title[] = $programme1;
}
//
echo "<pre>";
print_r( $programme_title );
echo "</pre>";
$count = 0;
foreach($time_arr as $time){
//$test = implode(' ', $programme_title);
//$tester[] = $test;
//echo $tester;
echo $time->item(0)->nodeValue." ";
echo $programme_title[$count++]."<br>";
}
?>
it returns
Array
(
[0] => Sister Act 2: Back in the Habit
[1] => Mamma Mia!
[2] => Forrest Gump(D,L,V,S)
[3] => The Blind Side
)
1:30 PM Sister Act 2: Back in the Habit
3:30 PM Mamma Mia!
6:00 PM Forrest Gump(D,L,V,S)
9:00 PM The Blind Side

Changing XML source on click using PHP

I have a simple newsfeed which is pulling from an xml file which works great. The news feed is broken down into separate XML files for each year. I want the page to show only one year at a time with links to other years above. Ideally when a new year is clicked I would like the content below to update without going to a new page.
I am trying to avoid repeating the PHP code which is parsing the XML for each year and instead would like to simply update the $dom_object->load source when the user clicks a different year.
I am a PHP newbie so some help would be appreciated!
<?php
$dom_object = new DOMDocument();
$dom_object->load("http://EXAMPLE.com/XML-Feed-10100524539?year=2013");
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
}
?>
You would need to use ajax by using $.get or $.post so you could do something like:
$('#yourElement').click(function(){
var path = $('#path'); // for example
$.post("your/path/getXML.php", { path: path },
function (dat) {
console.log(dat);
}
);
});
Where getXML would be a file similar to this one:
$dom_object = new DOMDocument();
$dom_object->load($_POST["path"]);
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";

Group 'Today's Events' under single Date Header?

I am trying to parse a Google Calendar to use on our TV's to display 'Today's Events'.
While I am most of the way there thanks to the help of a friend, I wanted to see if somebody could help me the rest of the way.
The code below generates the calendar with all the information, but for EVERY entry it shows the date. Since they are all the same day, this is kind of frustrating and confusing when looking at it. I am nowhere near a programmer, but I can make sense of some things.
How would I group all Todays events under a single date heading?
Thanks in advance.
<?php
$confirmed = 'http://schemas.google.com/g/2005#event.confirmed';
$three_months_in_seconds = 60 * 60 * 24 * 28 * 3;
$three_months_ago = date("Y-m-d\Th:i:s", time() - $three_months_in_seconds);
$three_months_from_today = date("Y-m-d\Th:i:s", time() + $three_months_in_seconds);
$params = "?orderby=starttime&start-min=" . $three_months_ago . "&start-max=" . $three_months_from_today;
//$params = "?orderby=starttime&start-min=2012-12-01T05:48:47&start-max=2013-05-07T05:48:47&sortorder=a&singleevents=true&futureevents=true";
$params = "?orderby=starttime&sortorder=a&singleevents=true&futureevents=true";
$feed = "https://www.google.com/calendar/feeds/REDACTED%40gmail.com/private-REDACTED/full".$params;
$doc = new DOMDocument();
if (!$doc->load( $feed )) echo 'failed to load';
$entries = $doc->getElementsByTagName( "entry" );
foreach ( $entries as $entry ) {
$status = $entry->getElementsByTagName( "eventStatus" );
$eventStatus = $status->item(0)->getAttributeNode("value")->value;
if ($eventStatus == $confirmed) {
$titles = $entry->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
$times = $entry->getElementsByTagName( "when" );
$startTime = $times->item(0)->getAttributeNode("startTime")->value;
$when = date( "D M j, Y", strtotime( $startTime ) );
$time = date("g:i A",strtotime($startTime));
$places = $entry->getElementsByTagName( "where" );
$where = $places->item(0)->getAttributeNode("valueString")->value;
print "<div class='row when'>$when</div>";
echo "<div class='row event'><span class='time'>$time</span><span class='title'>$title</span><span class='where'>$where</span></div>";
// print $where . "\n";
print "\n";
}
}
?>
Have an answer:
just change this:
print "<div class='row when'>$when</div>";
to this:
if ($old_when!=$when) print "<div class='row when'>$when</div>"; $old_when=$when;
and add
$old_when = null;
before the foreach

return rss node based on date

I am trying to return twitter titles based on today's date only. I have made the following code below, but it returns every title no matter if its today's date or not.
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=google');
$xml = simplexml_import_dom($dom);
$twitter = $xml->xpath("//item");
foreach ($twitter as $item) {
$timezone = new DateTimeZone('America/Los_Angeles');
$date = new DateTime($item->pubdate);
$date->setTimeZone($timezone);
$twitter_date = $date->format("F j Y");
$todays_date = date("F j Y");
if ($twitter_date == $todays_date) {
foreach ($twitter as $item) {
$text = $item->title;
echo $text.'<br />';
}
}
}
You are looping again through EVERY $twitter inside the if statement. Try removing the foreach tag inside and just using the current $item:
if ($twitter_date == $todays_date) {
$text = $item->title;
echo $text.'<br />';
}

Categories