Replace child in XML - php

<?php
$files = glob('users/*.xml');
foreach($files as $file) {
$xml = new SimpleXMLElement($file, 0, true);
echo '
<tr>
<td><input type="radio" name="browser" onclick="check(this.value)" value="'. basename($file) .'"></td>
<td class="alternate-row1">'. basename($file, '.xml') .'</td>
<td><span id="itm1" onclick="exchange(this.id)">'. $xml->name .'</span><input id="itm1b" class="replace" type="text" name="newname"></td>
<td class="alternate-row1">'. $xml->lastname .'</td>
<td>'. $xml->email .'</td>
<td class="alternate-row1">'. $xml->level .'</td>
<td>'. $xml->birthday .'</td>
<td class="alternate-row1">'. $xml->gender .'</td>
<td>'. $xml->age .'</td>
<td class="alternate-row1">'. $xml->country .'</td>
</tr>';
}
session_start();
if (isset($_POST['save'])){
$member = $_POST['newname'];
$dom = new DOMDocument();
$dom->load('users/' . basename($file) . '.xml');
$editname = $dom->getElementsByTagName('name');
$newname = $dom->createTextNode($member);
foreach ($editname as $edit) {
$edit->parentNode->replaceChild($newname, $edit);
}
$dom->save('users/' . basename($file) . '.xml');
}
?>
So I've made this code, as you can see it got DOM and Simple XML element.
I trying to replace child using $_POST throw input command, the input box is in the echo part above and I using js replacement code, it replace text to input text by onclick command (not really important for my question, it just for explaining).
Anyway I got little stuck with the code since it not working, I got set the $_POST, I try use some guides I found in Stack Overflow but it didn't work since it not matches my code.
Do you have any answer to me?
Thank you for the assistance, Y. D.

Example using simplexml ---> see live demo: http://codepad.viper-7.com/2oR5Wk
$xmlstr = <<<XML
<root>
<head>
<name id="1">test</name>
<name id="2">file</name>
<name id="3">name</name>
</head>
</root>
XML;
$xml = simplexml_load_string($xmlstr);
echo "<pre>";
var_dump($xml);
echo "</pre>";
$x = count($xml->head->name)-1;
for ($x; $x>=0; $x--) {
$xml->head->name[$x] = "newname";
}
echo "<pre>";
var_dump($xml);
echo "</pre>";

Related

While m exporting data to excel grid lines are not coming

$html='<table style="border: 0.1pt solid #ccc">
<tr><td>क्षेत्र </td>
<td>विभाग</td>
<td>नाम </td>
<td>पद </td>
<td>मोबाइल नम्बर </td>
<td>कोई अन्य नम्बर</td>
<td>ई-मेल </td>
<td>पता</td>
</tr>' ;
while( $row = mysqli_fetch_array($result) ) {
$html.='<tr>
<td>'.$row['jila'].' '.$row['vibhag'].' '.$row['name'].'</td>
<td>'.$row['post'].'</td>
<td>'.$row['mobile1'].'</td>
<td>'.$row['mobile2'].'</td>
<td>'.$row['email'].'</td>
<td>'.$row['address'].'</td>
</tr>' ;
}
$html.= '</table>';
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename=report.xls');
print chr(255) . chr(254) . mb_convert_encoding($html, 'UTF-16LE', 'UTF-8');
Adding table border was mentioned somewhere in a similar post so tried that but still the problem persist. Is there any problem with code due to which this is happening.

Getting href attribute

I have this php codes:
$main_url = "http://www.sports-reference.com/olympics/countries/DEN/summer/1896/";
$main_html=file_get_html($main_url);
$link = $main_html->getElementById('div_sports');
foreach ($link->find('td') as $element){
foreach($element->find('href') as $node){
echo $node->item(0)->nodeValue . "\n";
//$link_clean = $node->getAttribute('href');
echo $link_clean . "\n";
}
}
If I print out $element, I get this output:
<td align="left" >Athletics</td>
<td align="left" >Fencing</td>
<td align="left" >Gymnastics</td>
<td align="left" >Shooting</td>
<td align="left" >Weightlifting</td>
I need to extract this info:
/olympics/countries/DEN/summer/1896/ATH/
/olympics/countries/DEN/summer/1896/FEN/
..........
and so on. the code above is not working. CAn you helpme?
href is not a tag, it is a tag attribute.
So, you have to search for <a>:
foreach( $element->find('a') as $a)
{
echo $a->href . "\n";
(...)
}

How to number items of foreach loop in php? [duplicate]

This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 7 years ago.
I am using this to create a mp3 list.
$mp3 = glob($directory . '*.mp3');
//print each file name
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one"><!--number goes here --></td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>
';
}
I want to add number with every item of the list like, first one: 1, second one:2 etc. how can I do it?
$listOfMp3 = glob($directory . '*.mp3');
foreach($listOfMp3 as $i => $mp3)
{
echo $i;
}
This is not the best solution, but you can use a counter. Here is an example:
$mp3 = glob($directory . '*.mp3');
$c = 0; //Set this value to 1 if you don't want to start with 0
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one">'.$c.'</td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>';
$c++;
}
So now $c will display the number of the file every time. Anyway, you can get the foreach key to do exactly the same.

PHP DOM Parser Get Specific text by Class While Looping

I am working on a PHP Simple DOM Parser and i want a simple solution for my question
<tr>
<td class="one">1</td>
<td class="two">2</td>
<td class="three">3</td>
</tr>
<tr>
<td class="one">10</td>
<td class="two">20</td>
<td class="three">30</td>
</tr>...
the html of mine is will look similar to the above
and i am looping over through td something like this
foreach ($sample->find("td") as $ele)
{
if($ele->class == "one")
echo "ONE = ".$ele->plaintext;
if($ele->class == "two")
echo "TWO= ".$ele->plaintext;
}
But is there any simple solution that without if condition getting the plaintext of particular class i dont want shorthand if also
I am expecting something like this below
$ele->class->one
take a look at it:
<?php
$html = "
<table>
<tr>
<td class='one'>1</td>
<td class='two'>2</td>
<td class='three'>3</td>
</tr>
<tr>
<td class='one'>10</td>
<td class='two'>20</td>
<td class='three'>30</td>
</tr>
</table>
";
// Your class name
$classeName = 'one';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// Get the results
$results = $xpath->query("//*[#class='" . $classeName . "']");
for($i=0; $i < $results->length; $i++) {
echo $review = $results->item($i)->nodeValue . "<br>";
}
?>

Changing column in HTML table to hyperlink

for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...
I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.
I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)

Categories