XML node content change - php

I am having a xml content like
<?xml version="1.0"?>
<content
ID="immunSect"/>
<table
border="1"
width="100%">
<thead>
<tr>
<th>Vaccine</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td><content
ID="immun2"/>Influenza virus vaccine</td>
<td>May 2012</td>
<td>Completed</td>
</tr>
<tr>
<td><content
ID="immun4"/>Tetanus and diphtheria toxoids</td>
<td>April 2012</td>
<td>Completed</td>
</tr>
</tbody>
</table>
</text>
My problem is I would like to change this node
<tbody>
<tr>
<td><content
ID="immun2"/>Influenza virus vaccine</td>
into
<tbody>
<tr>
<td><content
ID="immun2">Influenza virus vaccine</content</td>
Please help me how can I fetch that particular section and change the node structure from
<td><content id="xxx"/>test
into
<td><content id="xxx">test</content>

Basically when geting XML data, put loop until EOF in which get each line (#string1) and parse it, rewriting it to new variable (#string2), which you can save it to XML file/whatever you need with it. Start parsing #string1, moving everything to #string2, until you encounter and ignore it, instead put a flag to 1. Continue with copying, until you find then input into #string2 .
Of course it can be done in more elegant way, but I would need to know what are you using to get XML data.
Hope that helps.

Related

PHP simplehtmldom find text in blank table structure

I'm having difficulty finding the DYNAMIC-TEXT value in a sea of HTML tables.
I have tried $html->find("th[plaintext*=Type") and from here, I wanted to access the sibling, but return nothing. Here's the table structure
<table>
<tbody>
</tbody>
<colgroup>
<col width="25%">
<col>
</colgroup>
<tbody>
<tr class="odd">
<th colspan="2">Name</th>
</tr>
<tr class="even">
<th width="30%">Type</th>
<td>DYNAMIC-TEXT</td>
</tr>
</tbody>
</table>
I expect the output to be the text of DYNAMIC-TEXT but the action output is nothing
Thanks
In your code $html->find("th[plaintext*=Type") you want to use an attribute selector *= but there is no attribute plaintext.
But there is an attribute width with the value 30%. You might use a pattern ^[0-9]+%$ to check for 1+ digits followed by a percentage sign.
If you find a result, you could get the next_sibling and get the plaintext from it.
For example:
$html = str_get_html($str);
foreach ($html->find("th[width*=^[0-9]+%$]") as $value) {
echo $value->next_sibling()->plaintext;
}
Result:
DYNAMIC-TEXT

angular js ng-repeat issues inside html

I have like this now.
<tr ng-repeat="a in xx">
<td>Name</td>
<td>{{a.name}}</td>
</tr>
<tr ng-repeat ="b in kk">
<td>Id</td>
<td>{{b.ll}}</td>
</tr>
But i need the two loops inside one tr.Means, I need 4 td inside one tr.
How can I do it.
You would need to reply on the $index to reference the second array:
<tr ng-repeat="a in xx">
<td>Name</td>
<td>{{a.name}}</td>
<td>Id</td>
<td>{{kk[$index].ll}}</td>
</tr>

Split mysql retrieved data into headers and rows

I retrieve data from DB and I need to insert all of them in a table.
I have some constant headers and some that depends on the query results.
Below you can see a table; how i want it to look like:
I did prepare the HTML structure:
<table class="table-fill">
<thead>
<tr>
<th>ID</th>
<th>CODE</th>
<th colspan=2>TREND A</th>
<th colspan=2>TREND B</th>
<th colspan=2>TREND C</th>
</tr>
<tr>
<th></th>
<th></th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td>1</td>
<td>A</td>
<td>10</td>
<tD>150</td>
<td>5</td>
<tD>200</td>
<td>8</td>
<tD>120</td>
</tr>
</tbody>
No problem so far. The hard part is to use actual data from DB and insert them to the table.
Headers "trend a", "trend b" etc are not constants. The below tables will be displayed on different countries. Some countries have specific trend names. Let's say England has "Trend A", "Trend B" and "Trend D". Italy has only "Trend A" etc.
Is it possible to write php code and in some way auto generate the table headers based on the query results?
And then split the results by trend so that correct values will go to correct trend?
I know how to do a simple table like:
$sql_r = "SELECT o.id, o.code, tl.name, ts.items, ts.revenue
FROM overview o, trend_stats ts, trend_lang tl
WHERE o.id_shop = '1'
AND o.id = ts.id
AND ts.id_trend = tl.id_trend";
$data_to_output = Db::getInstance()->ExecuteS($sql_r);
foreach ($data_to_output as $data) {
echo '
<tr>
<td>'.$data['id'].'</td>
<td>'.$data['code'].'</td>
<td>'.$data['trend'].'</td>
<td>'.$data['items'].'</td>
<td>'.$data['revenue'].'</td>
</tr>
';
}
But I'm not sure how to split and structure them the way I want them to be. If there's any tutorial or anything online related to what I'm asking I'm happy to check it out. Thank you
I don't think you can do that other than rewrite your headers. The fact that you want to retrieve your informations from your database tells that you'll have to work with any kind of loop. So you first have to display your headers and then display your informations. The only way is to store somewhere in your loop the value that will change your headers when it changes, and add another line of header when the value actually changes. You can do that with just a simple if condition.

How to extract only the second cell of the second column of a html table using PHP

I'm trying to extract only the second cell of the second column of a html table using php.
This is an example of the table:
<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
<tr>
<td>Name</td>
<td>Marcos</td>
</tr>
<tr>
<td>Address</td>
<td>1234 west 34 st</td>
</tr>
<tr>
<td>Phone</td>
<td>2013336666</td>
</tr>
<tr>
<td>fax</td>
<td>201456789</td>
</tr>
I just want to pull the cell with the address.
First, I would recommend you use the PHP DOMDocument class which is much more full-featured and well-supported. I also use DOMXPath for easy traversal.
$dom = DOMDocument::loadHTML($your_html_string);
$dom_xpath = new DOMXPath($dom);
$value_you_want = $dom_xpath->evaluate('string(/table/tr[2]/td[2])');
Well, since you didn't state how you plan to do this, I'll just throw something out there. Try the PHP DOM found here:
http://us2.php.net/manual/en/book.dom.php
It's a great OOP approach to reading and manipulating an XML or HTML DOM in PHP.

xPath, getting tabular data

I have a HTML thus like:
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<tr>
<td>Joe Bloggs</td>
<td>joe#bloggs.com</td>
<td>40</td>
</tr>
<tr>
<td>John Doe</td>
<td>john#doe.com</td>
<td>40</td>
</tr>
</table>
Is there a way using xPath to get the first 2 columns, i.e the Name and Email fields?
I can get the table data using $data = $xpath->query( '//table'); just unsure how to get only the first 2 columns.
Many thanks
Get the first two td's:
//table/tr/td[position() <= 2]

Categories