simple html dom on non valid markup - php

I'm using Simple HTML Dom and it's working very well on most of my Data. However, on is being a pain as the markup isn't valid. Is there any other way to do this in PHP.
I've got this result from a page I'm trying to extract the price from:
<taconite><replacecontent select="#basketcontents"><![CDATA[
<table id="sellingb" cellpadding="10px" cellspacing="15" width="600" border="0">
<thead>
<tr class="title">
<th width="47" scope="col" align="center">Book</th>
<th width="213" scope="col" align="left">Title</th>
<th width="139" scope="col" align="left">ISBN/Barcode</th>
<th width="63" scope="col" align="left">Value</th>
<th width="29" scope="col"> </th>
</tr>
</thead>
<tbody>
<tr class="trrow">
<td class="tdbook" align="center" valign="middle" ><img src="http://ecx.images-amazon.com/images/I/61JEp-wF3zL._SL75_.jpg" /><input name="offers_row_img[0]" type="hidden" value="http://ecx.images-amazon.com/images/I/61JEp-wF3zL._SL75_.jpg" /></td>
<td class="tdtitle">The Last Of Us (PS3) [Video Games]<input name="offers_row_title[0]" type="hidden" value="The Last Of Us (PS3) [Video Games]" /></td>
<td class="tdisbn">0711719274551<input name="offers_row_isbn[0]" type="hidden" value="0711719274551" /></td>
<td class="tdval">£15.00<input name="offers_row_price[0]" type="hidden" value="15.00" /></td>
<td class="tdremove"><input type="button" onclick="removeitem(0);" value="Reject Offer" /></td>
</tr>
</tbody>
</table>]]></replacecontent><eval><![CDATA[jQuery('#isbn').val('');]]></eval><replacecontent select="#price"><![CDATA[£15.00<br /><input type="button" class="bask-sb" id="acceptoffer" onclick="confirm('By clicking OK you are accepting the offer of £15.00 for your 1 item(s).'); acceptoffer();"/>]]></replacecontent></taconite>
However, there seems to be a problem. Simple HTML Dom only works on valid markup and this isn't valid. What is dthe best way I can extract the £15.00 from this result.
Thanks. It is much appreciated.

Only use valid markup, unless the markup is use by another resource.
One alternative could be to use strpos()/substr() for example:
$price = substr($input,strpos($h, "$")); // or euro symbol whatever you need
$price = substr($x, 0, strpos($x, "<"));
I assume your input is set to the variable $input.
This will only work well if you can be fairly sure that the next character after the price will be a < and that there will only be one price instance. If there will be more than one price instances you'll have to adjust it to get the right one(s).

Related

WordPress page content randomly inserting inside of shortcode

I am writing a plugin where you put in a shortcode in a page and it displays a table of data.
However, the page content prior to the shortcode is being inserted randomly into the middle of the table. When I refresh the page the content typed above the shortcode randomly moves within the table produced by the shortcode.
Content under the shortcode does not appear within the shortcode return.
Does anyone have any idea why this might be happening. This is super strange.
------------------ wordpress page edit ---------------
Here is some content.
Here is another paragraph.
[view_contributions]
End of page content.
------------------end of wordpress page edit ----------------------------
It then produces
------ displays --------
[Shortcode data table with "Here is some content. Here is another paragraph." randomly inserted into a cell somewhere. Then more data table]
End of page content.
-------- end of display -----
This is so odd. It's as if shortcode is rendering first but then WordPress injects the page content into whatever the shortcode is rendering. Any ideas what could cause this?
EDIT: Added entirety of code in case something really strange is happening...
function soco_view_contributions_shortcode() {
$view_contributions = Soco_Contributions::soco_display_contributions();
return $view_contributions;
}
add_shortcode( 'view_contributions', 'soco_view_contributions_shortcode');
public function soco_display_contributions() {
$contribution_results = Soco_Contributions::soco_get_contributions_view();
ob_start;
?>
<div name="div-output-container">
<form name="frm-search-contributions">
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Minimum</th>
<th scope="col">Maximum</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<tr>
<td><input type="date" name="start-date"></td>
<td><input name="end-date" type="date" ></td>
<td><input type="number" name="low-number"></td>
<td><input type="number" name="high-number"></td>
<td><text name="txt-auto-name"> </textarea></td>
<td><select> </select></td>
</tr>
</tbody>
</table>
<input type="submit">
<input type="reset">
</form>
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Cycle</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<?php foreach ($contribution_results as $cr) { ?>
<tr>
<td><?php echo $cr->contribution_date ?></td>
<td><?php echo $cr->amount ?></td>
<td><?php echo $cr->cycle_amount ?></td>
<td><?php echo $cr->last_name.', '.$cr->first_name ?></td>
<td> </td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
</div>
<?php
$contribution_output = ob_get_clean();
return $contribution_output;
}
It seems that you are attempting to return an output buffer as string. When ob_start() is called, all output is suppressed until ob_end(), ob_end_flush() or ob_end_clean() is called so logically, you can't return it. Simply call them within the function itself and return the contents of the output buffer (ob_get_contents()) as a string:
add_shortcode('view_contributions','soco_view_contributions_shortcode');
function soco_view_contributions_shortcode( ) {
ob_start();
?>
<h1>Shortcode Output</h1>
<p><?php echo "Some other output" ?></p>
<?
return ob_end_clean();
}
Why PHP doesn't throw a fatal error when attempting to return an entire buffer from a function surprises me.
The problem turned out to be mixing ECHO and RETURN in the output buffer. To fix this I turned the whole thing into a concatenated string to output instead.
The echos that are in the foreach loop I think were messing up the output buffer. So I removed ob_start all together and will just output a final HTML string.
Not a great solution but at least it is working now and not producing random results. If anyone has suggestions or examples on how to mix ob_start with php logic inside of it that would be great. It seems weird to me that ob_start() has issues with this.
$contribution_output = '<div name="div-output-container">
<form name="frm-search-contributions">
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Minimum</th>
<th scope="col">Maximum</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<tr>
<td><input type="date" name="start-date"></td>
<td><input name="end-date" type="date" ></td>
<td><input type="number" name="min-amount"></td>
<td><input type="number" name="max-amount"></td>
<td>
<input type="text" name="donor_name">
<input type="hidden" name="hdn-donor-id" value="">
</td>
<td><select> </select></td>
</tr>
</tbody>
</table>
<input type="submit">
<input type="reset">
</form>
<div name="contribution-results-container" >
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Cycle</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>';
foreach ($contribution_results as $cr) {
$contribution_output .= '
<tr>
<td>'.$cr->contribution_date.'</td>
<td>'.$cr->amount.'</td>
<td>'.$cr->cycle_amount.'</td>
<td>'.$cr->last_name.', '.$cr->first_name.'</td>
<td> </td>
</tr>';
}
$contribution_output .= '</tbody>
</table>
<button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
</div>
</div>';

Why datagrid_data1.json data can not appear

In my project, i use easyui-datagrid.
Here is my html code:
<tr>
<td style="width:10%" colspan="2">attachment</td>
<td style="width:90%" colspan="18">
<table class="easyui-datagrid" data-options="url:'datagrid_data1.json',method:'get'" title="" style="width:100%">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true"></th>
<th data-options="field:'name',align:'center'">filename</th>
</tr>
</thead>
</table>
</td>
</tr>
The code of datagrid_data1.json:
{"total":2,"rows":[
{"name":"1.jpg"},
{"name":"123.jpg"}
]}
But unfortunately, I works fail. Two names of 1.jpg and 123.jpg can not appear. Who can help me?

I want a drop down option will be selected in class=item_circle and save it in mysql

I want a drop down option will be selected in class=item_circle and save it in
MySQL. Is there any answer? I'm getting single data in MySQL
database but not the selected one:
<table class="table table-bordered" id="crud_table">
<tr>
<th width="10%">CIRCLE</th>
<th width="10%">Item1</th>
<th width="10%">Item1</th>
<th width="10%">Item1</th>
<th width="10%">Item1</th>
<th width="2%"></th>
</tr>
<tr>
<td contenteditable="false" class="item_circle">Bus Stand</td>
<td contenteditable="true" name="amt" class="item_1"></td>
<td contenteditable="true" name="amt" class="item_2"></td>
<td contenteditable="true" name="amt" class="item_3"></td>
<td contenteditable="true" name="amt" class="item_4"></td>
<td></td>
</tr>
</table>
I don't know if that's what you've asked for (because sorry but your question is a little bit cryptic), but you can just send the data you want to write into the database by either an AJAX request, or, in the case of a drop-down menu as you said, by assigning a value-attribute to each dropdown entry and then sending that via a form to the backend script.

Creating input field from html table that has PHP value

I have a page full of small html tables, each representing data from a database table. This is a one page print out report, so there are quite a few fields across these html tables, but only about 4 or 5 of them may need to be changed.
When the user chooses a record and the report is displayed, all the tables look great but I'm trying to find a way to edit and save the 4 or 5 that might need editing. I know I can create a form with inputs and turn certain table rows into inputs and then with a submit button I can attach a SQL statement to update those fields based on a primary key in the DB table.
How can I actually get these table rows to be editable inputs within a form? I've wrapped the whole page in a form and I even tried by wrapping just one table in a form as a test but I think the problem is my syntax for the table row.
Here is what I tried so far:
<form>
<table style="width:100%; border:none;
border-collapse:collapse;">
<tr style="border:none;">
<th style="border:none; text-align: left;" >Meter Test</th>
<th style="border:none;">Test 1</th>
<th style="border:none;">Test 2</th>
<th style="border:none;">Test 3</th>
<th style="border:none;">Test 4</th>
<th style="border:none;">Test 5</th>
<th style="border:none;">Test 6</th>
<th style="border:none;">Test 7</th>
<th style="border:none;">Test 8</th>
</tr>
<tr style="border: none;" >
<td style="border:none; text-align: left;">Test Rate GPM: </td>
<td><? echo $row['test1TestRateGPM'];?> </td>
<td><? echo $row['test2TestRateGPM'];?> </td>
<td><? echo $row['test3TestRateGPM'];?> </td>
<td><? echo $row['test4TestRateGPM'];?> </td>
<td><? echo $row['test5TestRateGPM'];?> </td>
<td><? echo $row['test6TestRateGPM'];?> </td>
<td><input type="text" value = "<?php $row['test7TestRateGPM'];?>">
<td><input type="text" value = "<?php $row['test8TestRateGPM'];?>"> </td>
</tr>
</table>
</form>
You can see i have the normal html table syntax but for the field I'm testing, I tried it by creating a line of php and doing it that way, but still no form on the web page.
I think you just need a simple input text field.
Do it like this:
<td><input type="text" value = "<?php echo $row['test1TestRateGPM'];?>"></td>
And make multiple of these.

How to create an html link to another page from "<?=$objResult["id"];?>"

I'm working on a php/mysql application and need to make the output of one column, one row an html link to another html page. Have not been able to find any relavant information. The "" needs to be the link. Thanks for any help.
<table id="display" style="width:800px;">
<tr>
<th width="40">ID</th>
<th width="70">Last</th>
<th width="70">First</th>
<th width="10">Middle</th>
<th width="70">Birth</th>
<th width="70">Death</th>
<th width="170">Notes</th>
<th width="100">Cemetery</th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td style='text-align:center;'><?=$objResult["id"];?></td>
<td style='text-align:center;'><?=$objResult["last"];?></td>
<td style='text-align:center;'><?=$objResult["first"];?></td>
<td style='text-align:center;'><?=$objResult["middle"];?></td>
<td style='text-align:center;'><?=$objResult["birth"];?></td>
<td style='text-align:center;'><?=$objResult["death"];?></td>
<td><?=$objResult["notes"];?></td>
<td style='text-align:center;'><?=$objResult["cemetery"];?></td>
</tr>
<?
}
?>
</table>

Categories