PHP IE9 XML Issue - php

I have a page that displays XML info in a table. The XML files are server side and I'm using PHP to get the file names and data using a drop down box. JSON is used to put the names in the dropdown and DOM to send the XML across. That works fine in all browsers.
There is an issue with my adding entries feature. When I add an entry in Chrome or Firefox it shows up the next time that table is selected. It doesn't work in IE9 though. The entries are added to the xml file but to view these changes in IE I have to open a new tab. Simply refreshing doesn't work. To redirect in this script I use the header function:
header('Location: ./client2.html');
Is there something that needs to be added here for IE or is there an issue somewhere else. I've added the php that gets the data when the file is chosen.
ini_set('display_errors',1);
error_reporting(E_ALL);
/* gets the selected file to use to return data */
$xml_filename = './XML/'.$_REQUEST['file'];
$xml = simplexml_load_file($xml_filename);
/* gets the root of the selected file */
$rootname = $xml->getName();
/* gets the children in that root */
$children = $xml->children();
$firstchild = $children[0];
// gets the table headings
$data = '{"headings":[';
foreach ($firstchild as $elem)
{
$data = $data.'"'.$elem->getName().'",';
}
// removes trailing ','
$data = substr_replace($data,"",-1);
$data = $data.'],';
// gets the cell values
$data = $data. '"vals":[';
foreach ($children as $child)
{
$data = $data.'[';
foreach ($child as $elem => $vals)
{
$data = $data.'"'.$vals.'",';
}
$data = substr_replace($data,"",-1);
$data = $data.'],';
}
// removes trailing ','
$data = substr_replace($data,"",-1);
$data = $data.']}';
/* sends created JSON string back to client */
echo $data;

If it's a caching problem, you could try adding a random string in the header() call, like this:
$random_str = sha1(uniqid(mt_rand(), true));
header('Location: ./client2.html?' . $random_str);
exit();

Turns out it was a caching issue. I had to add this: $.ajaxSetup({cache:false}) to the document.ready() section of the JavaScript. Nothing else seemed to work

Related

Get info form phpdom

I am trying to get information using curl.
Approximately I got all information, but I need to get information individually.
For example I am getting a td text using curl.
here is the td content
jsfiddle
I need to extract text "my info", myinfo href link and last page number.
How i can do this?
here is my code which i am using in curl
$nodes = $finder->evaluate('//td[contains(text(), "") and starts-with(#id, "td_threadtitle_") ]');
foreach ($nodes as $node)
{
$innerHTML = trim($tmp_dom->saveHTML());
$fh = fopen("test.html", 'w'); // we create the file, notice the 'w'. This is to be able to write to the file once.
//writing response in newly created file
fwrite($fh, $node->c14n()); // here we write the data to the file.
fclose($fh);
}
My info
(//td[starts-with(#id, "td_threadtitle_") ]//a[1]/text())[1]
It's href
(//td[starts-with(#id, "td_threadtitle_") ]//a)[1]/#href
Last page number
substring-after(//td[starts-with(#id, "td_threadtitle_") ]//a[. = "Last Page"]/#href, "page=")
I tried this and it's according to my requirement.
Please tell me is this ok or not?
$options = $node->getElementsByTagName('a');
$post_message_id=$node->getAttribute('id');
foreach($options as $option) {
$value = $option->getAttribute('id');
if($value!=""){
print_r( $option->getAttribute('href'));
echo "\n";
print_r( $option->textContent);
echo "\n";
print_r($options->item(($options->length)-1)->getAttribute('href'));
echo "\n";
}
}

Google Calendar layout

I have been working with this php code, which should modify Google Calendars layout. But when I put the code to page, it makes everything below it disappear. What's wrong with it?
<?php
$your_google_calendar=" PAGE ";
$url= parse_url($your_google_calendar);
$google_domain = $url['scheme'].'://'.$url['host'].dirname($url['path']).'/';
// Load and parse Google's raw calendar
$dom = new DOMDocument;
$dom->loadHTMLfile($your_google_calendar);
// Change Google's CSS file to use absolute URLs (assumes there's only one element)
$css = $dom->getElementByTagName('link')->item(0);
$css_href = $css->getAttributes('href');
$css->setAttributes('href', $google_domain . $css_href);
// Change Google's JS file to use absolute URLs
$scripts = $dom->getElementByTagName('script')->item(0);
foreach ($scripts as $script) {
$js_src = $script->getAttributes('src');
if ($js_src) { $script->setAttributes('src', $google_domain . $js_src); }
}
// Create a link to a new CSS file called custom_calendar.css
$element = $dom->createElement('link');
$element->setAttribute('type', 'text/css');
$element->setAttribute('rel', 'stylesheet');
$element->setAttribute('href', 'custom_calendar.css');
// Append this link at the end of the element
$head = $dom->getElementByTagName('head')->item(0);
$head->appendChild($element);
// Export the HTML
echo $dom->saveHTML();
?>
When I'm testing your code, I'm getting some errors because of wrong method call:
->getElementByTagName should be ->getElementsByTagName with s on Element
and
->setAttributes and ->getAttributes should be ->setAttribute and ->getAttribute without s at end.
I'm guessing that you don't have any error_reporting on, and because of that don't know anything went wrong?

change variable with GET method

I have a page test.php in which I have a list of names:
name1: 992345
name2: 332345
name3: 558645
name4: 434544
In another page test1.php?id=name2 and the result should be:
332345
I've tried this PHP code:
<?php
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("/test.php");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*#".$_GET["id"]."");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
I need to be able to change the name with GET PHP method in test1.pdp?id=name4
The result should be different now.
434544
is there another way, becose mine won't work?
Here is another way to do it.
<?php
libxml_use_internal_errors(true);
/* file function reads your text file into an array. */
$doc = file("test.php");
$id = $_GET["id"];
/* Show your array. You can remove this part after you
* are sure your text file is read correct.*/
echo "Seeking id: $id<br>";
echo "Elements:<pre>";
print_r($doc);
echo "</pre>";
/* this part is searching for the get variable. */
if (!is_null($doc)) {
foreach ($doc as $line) {
if(strpos($line,$id) !== false){
$search = $id.": ";
$replace = '';
echo str_replace($search, $replace, $line);
}
}
} else {
echo "No elements.";
}
?>
There is a completely different way to do this, using PHP combined with JavaScript (not sure if that's what you're after and if it can work with your app, but I'm going to write it). You can change your test.php to read the GET parameter (it can be POST as well, you'll see), and according to that, output only the desired value, probably from the associative array you have hard-coded in there. The JavaScript approach will be different and it would involve making a single AJAX call instead of DOM traversing using PHP.
So, in short: AJAX call to test.php, which then output the desired value based on the GET or POST parameter.
jQuery AJAX here; native JS tutorial here.
Just let me know if this won't work for your app, and I'll delete my answer.

How to dynamically update a sitemap with php?

I have a dynamic site where posts are constantly being generated i have it coded so each time a new post is added it appends the site map url entry to the bottom of the sitemap.xml file.
$lastID = $db->lastInsertId();
$file = 'sitemap.xml';
$current = file_get_contents($file);
$current .= "<url>
<loc>http://website.net/viewpost.php?ID=".$lastID."</loc>
<changefreq>monthly</changefreq>
</url>";
file_put_contents($file, $current);
this works but the closing tag in the xml file urlset needs to be at the end of the file. So when i append this data it goes after the urlset and even if i added this to the string there would be multiple closing tags for this. How do i update the file so it doesnt go after the closing tag.
Regenerating the entire site map every time would work but it seems like a lot of work as there are almost 100 pages as of right now and it will need to query multiple tables to get the data
This is a simple function for inserting an element before,after or inside another element.
<?php
public function myInsertNode($newNode, $refNode, $insertMode=null) {
if(!$insertMode || $insertMode == "inside") {
$refNode->appendChild($newNode);
} else if($insertMode == "before") {
$refNode->parentNode->insertBefore($newNode, $refNode);
} else if($insertMode == "after") {
if($refNode->nextSibling) {
$refNode->parentNode->insertBefore($newNode, $refNode->nextSibling);
} else {
$refNode->parentNode->appendChild($newNode);
}
}
}
?>

Using media field from Page in an extension

We all may know the following snippet that gets the first entry of the media field of a page to e.g. display a header image on a page.
10 = IMAGE
10.file {
import = uploads/media/
import.data = levelmedia: 0, slide
import.listNum = 0
}
I am creating an extension (plugin, no ItemuserFunc etc) which needs to work with this media. I don't want to process dozens of typoscript directives in php to create this images. How can I use the example above to process the media field through typoscript, using core functionality instead of reinventing the wheel?
So now I'm in my extension code, having an array of page records. How to get further? The code below doesn't provide the functionality I need because I am not working on the page level (like I would when using the TS from above in a TMENU).
$content .= $this->cObj->stdWrap($page['media'], $this->conf['media_stdWrap.']);
Having an array of page records ($pages) with the associative keys same as the field names in the database, mainly the media field, you would simply walk through the array and create the images using the Typoscript configuration.
Example 1 (a programmer sets the rendering)
foreach($pages as $value) {
$mediaField = t3lib_div::trimExplode(',', $value['media']);
if(!$mediaField[0]) continue;
$imageConf = array(
'file' => 'uploads/media/' . $mediaField[0],
);
$content .= $this->cObj->cObjGetSingle('IMAGE', $imageConf);
}
Example 2 (an editor sets the rendering)
foreach($pages as $value) {
$this->cObj->data = $value;
$content .= $this->cObj->stdWrap($value['media'], $this->conf['media_stdWrap.']);
}
It sets the page data so that they are available as field in Typoscript. The configuration would then look something like this:
{
media_stdWrap {
cObject = IMAGE
cObject {
file {
import = uploads/media/
import.field = media
import.listNum = 0
}
}
}
}
Example 3 (combination of the previous 2 examples)
foreach($pages as $value) {
$mediaField = t3lib_div::trimExplode(',', $value['media']);
$mediaOutput = '';
// Creating the output with a default rendering
if($mediaField[0]) {
$imageConf = array(
'file' => 'uploads/media/' . $mediaField[0],
);
$mediaOutput = $this->cObj->cObjGetSingle('IMAGE', $imageConf);
}
// Allowing custom Typoscript to completely modify the media part
if(array_key_exists('media_stdWrap.', $this->conf)) {
$this->cObj->data = $value;
$mediaOutput = $this->cObj->stdWrap($mediaOutput, $this->conf['media_stdWrap.']);
}
$content .= $mediaOutput;
}
NOTE: These examples are completely untested. I've came up with them out of my head.

Categories