Fetching the particular values from a table onclick of checkbox in php - php

My table has the following structure
When i click the particular checkbox.Its should be able to fetch the value of all the contents in the selected checkbox ..It can be n number of products the user selects.How to achieve this .... Thanks for ur time
Source code
<?$this->load->view('admin/header');?>
<script type="text/javascript">
function go() {
var frm = document.frm;
var status = inwardProductList['status'];
var statusList = [];
for ( i = 0; i < status.length; i++ ) {
if ( status[i].checked ) {
statusList.push('status=' + status[i].value);
}
}
document.getElementById('test').innerHTML = 'ajaxUrl?' + statusList.join('&');
}
</script>
<script src="<?=base_url();?>js/calendar.js" type="text/javascript"></script>
<form name="inwardProductList" action="" method="post" >
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" class="formtable">
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Cart Display</b></td>
</tr>
<tr>
<td height="66" align="left" valign="top"><table width="99%" id="suppliedtable" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="4%" height="43" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Sl.no</strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Product Name</strong></strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Barcode</strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Selection</strong></strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Quantity</strong></strong></td>
</tr>
<?
$i=0;
if($productName->num_rows() >0){
foreach($productName->result() as $row ){
$i++;
?>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$i;?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->product_name?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->barcode?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form>
<input type="checkbox" name="status" value="yes" /> Yes<br /></form></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form>Quantity: <input type="text" name="Qunatity" /></form></td>
</tr>
<? }}else{?>
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Selected product has not been processed yet</b></td>
</tr>
<?}?>
</table></td>
</tr>
</table>
<form><tr><td align="center" > <button onclick="go()">go</button></td> </tr>
<div id="test"></div>
</form></form>
</form>

This could be achieved using jquery, something like this, though its off the top of my head so there may be errors.
Jquery stuff
$('tr.tableRow > input[type=checkbox]').click(function() {
var productName = $(this).find('td.productName').text();
var productBarcode = $(this).find('td.productBarcode').text();
var productQuantity = $(this).find('input[name=productQuantity]').val();
});
HTML stuff
<tr class="table-row">
<td class="productName">My Product</td>
<td class="productBarcode">0389463844</td>
<td class="productSelected"><input type="checkbox" value="yes"></input></td>
<td class="productQuantity"><input type="text" name="productQuantity"></td>
</tr>

Related

Simple Dom Parser or CURL TABLE PARSING

I need help to get the data from a table. It's an internet usage table and the html code is down below :
<table width="572" border="0" align="center" cellspacing="0">
<tbody><tr valign="top">
<td width="1" class="bgsidelines"></td>
<td width="*" class="bgbottom">
<table summary="" width="100%" border="0" cellpadding="0">
<tbody><tr>
<td width="10" rowspan="2" bgcolor="#CCCCCC"></td>
<td width="443">
<table width="443" height="10" border="0" align="center" cellpadding="8">
<tbody>
<tr>
<td width="100%" class="path"><b>Internet usage</b></td>
</tr>
<tr>
<td class="reg"><!-- Begin yours codes -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<table cellpadding="5" cellspacing="1" border="0">
<tbody>
<tr>
<td width="43" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="reg" nowrap="nowrap">2017-06-01 to<br>2017-
06-18</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">54815.06</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">53.53</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">52114.59</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">50.89</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">106929.65</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">104.42</td>
</tr>
</tbody></table></td></tr>
</tbody></table>
<!-- End yours codes -->
</tr>
</tbody></table></td></tr>
</tbody></table></td></tr>
</tbody></table>
I've done it in a way that works but only works sometimes, this must be due to the user agent. and it fetches the entire table while I would like each separated values for the internet usage, the ones in the td class="reg" (54815.06, 53.53..) It's hard because there is a table in table.. Also it's
My PHP :
require_once 'advanced_html_dom.php';
$numvl = $_POST['numvl'];
$url =
'https://extranet.videotron.com/services/secur/extranet/tpia/Usage.do?
compteInternet='.$numvl;
$html = new AdvancedHtmlDom();
$html->load_file($url);
$element = $html->find("tr");
echo $element[1]->innertext;
no need for some external lib (advanced_html_dom.php? never heard of), just use PHP's DOMDocument and DOMXPath.
example:
<?php
declare(strict_types=1);
$domd=#DOMDocument::loadHTML(getHTML());
$xpath=new DOMXPath($domd);
foreach($xpath->query("//td[#valign='top' and #class='reg']") as $ele){
var_dump($ele->textContent);
}
function getHTML():string{
$html=<<<'HTML'
<table width="572" border="0" align="center" cellspacing="0">
<tbody><tr valign="top">
<td width="1" class="bgsidelines"></td>
<td width="*" class="bgbottom">
<table summary="" width="100%" border="0" cellpadding="0">
<tbody><tr>
<td width="10" rowspan="2" bgcolor="#CCCCCC"></td>
<td width="443">
<table width="443" height="10" border="0" align="center" cellpadding="8">
<tbody>
<tr>
<td width="100%" class="path"><b>Internet usage</b></td>
</tr>
<tr>
<td class="reg"><!-- Begin yours codes -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<table cellpadding="5" cellspacing="1" border="0">
<tbody>
<tr>
<td width="43" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="44" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>MB</center></b>
</td>
<td width="60" bgcolor="#EEEEEE" class="grey"><b><center>GB</center></b>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" class="reg" nowrap="nowrap">2017-06-01 to<br>2017-
06-18</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">54815.06</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">53.53</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">52114.59</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">50.89</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">106929.65</td>
<td bgcolor="#FFFFFF" align="right" valign="top" class="reg">104.42</td>
</tr>
</tbody></table></td></tr>
</tbody></table>
<!-- End yours codes -->
</tr>
</tbody></table></td></tr>
</tbody></table></td></tr>
</tbody></table>
HTML;
return $html;
}
output:
string(8) "54815.06"
string(5) "53.53"
string(8) "52114.59"
string(5) "50.89"
string(9) "106929.65"
string(6) "104.42"

Duplicated Results on SQL Query

PHP newbie here! I ve been struggling with this for a few days now and i have decided i cant figure this out on my own.
Basically i have 2 database tables "projects_2016" and "attachment".
I want to show the data of "projects_2016" to show in the top table and then check for a matching id number (and if it exsits) in "attachment" it will list all the results under the "project_2016 data".
At the moment it works great but it duplicates the "projects_2016" data for every "attachment" entry.
Here is my code, any input is appreciated!
PS not too concereded about Sql injections. Still learning that!
<?php include '../../../connection_config.php';
$sql = "SELECT DISTINCT * FROM attachment JOIN projects_2016 ON attachment.attachment_ABE_project_number = projects_2016.id ORDER BY `attachment_ABE_project_number` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<table width="20" border="1" cellspacing="0" cellpadding="2">
<tr>
<th height="0" scope="col"><table width="990" border="0" align= "center" cellpadding="3" cellspacing="0">
<tr class="text_report">
<td width="107" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>PNo</strong></td>
<td width="871" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>Project Name</strong></td>
</tr>
<tr>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["ID"]. "<br>";?></strong></td>
<td height="20" align="left" valign="middle" bgcolor= "#FFFFFF" class="text_report"><strong><?php echo "<br>". $row["project_name"]. "<br>";?></strong></td>
</tr>
</table>
<?php
$photo_id = $row["ID"];
$contacts = "SELECT DISTINCT * FROM attachment WHERE attachment_ABE_project_number = '$photo_id'" ;
$result_contacts = $conn->query($contacts);
if ($result_contacts->num_rows > 0) {
// output data of each row
while($row_contacts = $result_contacts->fetch_assoc()) {
?>
<table width="990" border="0" align="center" cellpadding= "3" cellspacing="0" class="text_report">
<tr>
<td height="0" colspan="4" align="left" valign="middle" nowrap="nowrap" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td width="319" height="30" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Name</strong></td>
<td width="279" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Type</strong></td>
<td width="315" align="left" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>File Size (KB)</strong></td>
<td width="53" align="right" valign="middle" nowrap="nowrap" bgcolor="#F5F5F5"><strong>View File</strong></td>
</tr>
<tr>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['file'] ?></td>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['type'] ?></td>
<td height="20" align="left" valign="middle" bgcolor="#FFFFFF"><?php echo $row_contacts ['size'] ?></td>
<td align="right" valign="middle" bgcolor="#FFFFFF">view file</td>
</tr>
<tr>
<td height="0" colspan="4" align="left" valign="middle" bgcolor="#FFFFFF"> </td>
</tr>
<?php
}
?>
</table>
<?php
}
?></th>
</tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
<tr>
<th height="26"> </th>
</tr>
</table>
<p>
<?php
}
}
?>
</p>
</table>
<?php $conn->close();
?>
$sql = "SELECT * FROM projects_2016
WHERE EXISTS (SELECT * FROM attachment WHERE projects_2016.id = attachment_ABE_project_number) ORDER BY id DESC ";

extract table data from htm

I need to extract data from a html table and save it in a csv file. Is there any easy way to get all the information inside the attributes of the table in bash, or php?
This is the code
<html><head> <link rel=STYLESHEET href="/XPIcons/style.css" type="text/css">
<title>Control 20 November 2014</title>
</head>
<body
>
<table width="100%" cellapdding="0" cellspacing="0">
<tr><td WIDTH="100%" class="username">xxxx<br><font color=#A4A6A0>IDIOMABASE</font> </td>
<td> </td><td bgcolor=#FFFFFF rowspan="3" align="right"><img src="/XPIcons/logo.jpg"></td></tr>
<tr><td width="100%" align="top" class="guio"><img src="/XPIcons/guion_verde.jpg"></td></tr>
<tr><td width="100%" align="top" class="title">CONTROL 20 NOVEMBER 2014
<br><font color=#B7D30C size="1px"><SCRIPT LANGUAGE="JavaScript" SRC="/XPIcons /calendar.js"></SCRIPT></font>
</td></tr>
</table>
<P>
<script language="JavaScript">
function doNothing(){
}
function ShowData(){
var obj = "QUALCTRL.ShowDataTD?p_date="+p_date.value;
location.href=obj;
}
</script>
<script language="JavaScript">
function test(formu)
{
error=formu.p_date.value==""?"ErrorDate\n":"";
if (error != "")
alert(error);
else
formu.submit();
}
</script>
<table>
<td>Fecha</td><td>
<input type="text" id="p_date" name="p_date" value="20/11/2014" onblur="Compruebap_fecha(formu.p_date);">
<A HREF="javascript:doNothing()" onClick="var obj=document.getElementById('p_date'); setDateField(obj);top.newWin=window.open('/XPIcons/calendar.html', 'cal', 'dependent=yes, resizable=yes, width=210, height=230, screenX=200, screenY=300, titlebar=no')">
<IMG SRC="/XPIcons/calendar.gif" BORDER=0></A><font size=1>Ver calendario</font>
(dd/mm/YYYY)
</td></table>
<input type="button" class="btn" onClick="javascript:RecarregaPlana();" value="Cambia de Dia >>">
<P>
<table border="1%">
<tr><td class="fila_blanca">Población</td>
<td class="fila_mesgris">MAX</td>
<td class="fila_mesgris">MIN</td>
<td class="fila_menysgris">MASS MAX</td>
<td class="fila_menysgris">MASS MIN</td>
<td class="fila_mesgris">MERGE MAX</td>
<td class="fila_mesgris">MERGE MIN</td>
<td class="fila_menysgris">MOS MAX</td>
<td class="fila_menysgris">MOS MIN</td>
<td class="fila_blanca">DIF MAX</td>
<td class="fila_blanca">DIF MIN</td>
<td class="fila_blanca">DIF MAX MERGE</td>
<td class="fila_blanca">DIF MIN MERGE</td>
<td class="fila_blanca">DIF MAX MOS</td>
<td class="fila_blanca">DIF MIN MOS</td>
</tr>
<tr>
<td class="fila_blanca">Palermo</td>
<td class="fila_mesgris">20</td>
<td class="fila_mesgris">11</td>
<td class="fila_menysgris">21</td>
<td class="fila_menysgris">10</td>
<td class="fila_mesgris">20</td>
<td class="fila_mesgris">17</td>
<td class="fila_menysgris">20</td>
<td class="fila_menysgris">9</td>
<td class="fila_blanca">-1</td>
<td class="fila_blanca">1</td>
<td class="fila_mesgris">0</td>
<td class="fila_mesgris">-6</td>
<td class="fila_menysgris">0</td>
<td class="fila_menysgris">2</td>
</tr>
<tr>
<td class="fila_blanca">Bergamo</td>
<td class="fila_mesgris"></td>
<td class="fila_mesgris"></td>
<td class="fila_menysgris">16</td>
<td class="fila_menysgris">7</td>
<td class="fila_mesgris">17</td>
<td class="fila_mesgris">7</td>
<td class="fila_menysgris">17</td>
<td class="fila_menysgris">7</td>
<td class="fila_blanca"></td>
<td class="fila_blanca"></td>
<td class="fila_mesgris"></td>
<td class="fila_mesgris"></td>
<td class="fila_menysgris"></td>
<td class="fila_menysgris"></td>
</tr>
<tr>
<td class="fila_blanca">Rome</td>
<td class="fila_mesgris"></td>
<td class="fila_mesgris"></td>
<td class="fila_menysgris">19</td>
<td class="fila_menysgris">16</td>
<td class="fila_mesgris">19</td>
<td class="fila_mesgris">14</td>
<td class="fila_menysgris">19</td>
<td class="fila_menysgris">14</td>
<td class="fila_blanca"></td>
<td class="fila_blanca"></td>
<td class="fila_mesgris"></td>
<td class="fila_mesgris"></td>
<td class="fila_menysgris"></td>
<td class="fila_menysgris"></td>
</tr>
</table>
<SCRIPT>
function openSearch() {
window.open('XPSearch.Search', 'XPSearch', 'scrollbars=yes,resizable=yes,toolbar=yes,location=yes,status=yes,width=550,height=500,screenX=550,screenY=500'); }
function doNothing() {
}
</SCRIPT>
<P>
<table width="100%" cellspacing="0">
<tr><td class="pie" width="100%"><b>Menú principal</b> </td><td bgcolor=#FCFCFA><img border="0" src="/XPIcons/search.jpg" ></td> <td><marquee hspace=147></marquee></td></table>
</body></html>
And I would like to get a csv like this:
Población,MAX,MIN,MASS MAX,MASS MIN,MERGE MAX,MERGE MIN,MOS MAX,MOS MIN,DIF MAX,DIF MIN ,DIF MAX MERGE,DIF MIN MERGE,DIF MAX MOS,DIF MIN MOS
Palermo,20,11,21,10,20,17,20,9,-1,1,0,-6,0,2
Bergamo,,,16,7,17,7,17,7,,,,
Rome,,,19,16,19,14,19,14,,,,
This can be a way:
awk -F'">|<' -v OFS=","
'NF>3{if (r) {r=r OFS $3} else r=$3}
/tr/ {print r; r=""}' file
For your sample input:
$ awk -F'">|<' -v OFS="," 'NF>3{if (r) {r=r OFS $3} else r=$3} /tr/ {print r; r=""}' a
td class="fila_blanca
MAX,MIN,MASS MAX,MASS MIN,MERGE MAX,MERGE MIN,MOS MAX,MOS MIN,DIF MAX,DIF MIN,DIF MAX MERGE,DIF MIN MERGE,DIF MAX MOS,DIF MIN MOS
Palermo,20,11,21,10,20,17,20,9,-1,1,0,-6,0,2
Bergamo,,,16,7,17,7,17,7,,,,,,
Rome,,,19,16,19,14,19,14,,,,,,
Explanation
-F'">|<' set the input field separator to either "> or <. This way, we can catch the values within the tags easily, without further processing.
-v OFS="," set the output field separator to a comma.
NF>3{if (r) {r=r OFS $3} else r=$3} if the record contains more than 3 fields, store the 3rd in the variable r. This will keep adding content until <tr is found...
/tr/ {print r; r=""} and that's when we print the content and empty the variable to start processing the next block.

jquery and php to grab the contents from a selected row When onclick of a selected checbox

Attached the source code of PHP and jquery , gives undefined as alert ...later displays all the contents in the table... i need to just display the one which the user clicks on checkbox..... what mistake is in my code.
--------------------php---------------------------------------------------------
</script>
<script src="<?=base_url();?>js/calendar.js" type="text/javascript"></script>
<form name="inwardProductList" action="" method="post" >
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" class="formtable">
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Cart Display</b></td>
</tr>
<tr>
<td height="66" align="left" valign="top"><table width="99%" id="suppliedtable" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="4%" height="43" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Sl.no</strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Product Name</strong></strong></td>
<td width="20%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Barcode</strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Quantity</strong></strong></td>
<td width="8%" align="center" valign="middle" bgcolor="#e7e6e6" class="rows"><strong>Select</strong></strong></td>
</tr>
<?
$i=0;
if($productName->num_rows() >0){
foreach($productName->result() as $row ){
$i++;
?>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$i;?></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->product_name?></td>
<input type="hidden" name="product_name<?=$i?>" id="product_name<?=$i?>" class="button" value="<?=$row ->product_name;?>"/>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><?=$row->barcode?></td>
<input type="hidden" name="barcode<?=$i?>" id="barcode<?=$i?>" class="button" value="<?=$row ->barcode;?>"/>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form><input type="text" name="Quantity<?=$i;?>" id="Quantity<?=$i;?>" /></form></td>
<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows"><form>
<input type="checkbox" name="status<?=$i;?>" id="status<?=$i;?>" value="yes" /> <br /></form></td>
</tr>
<? }}else{?>
<tr>
<td height="23" colspan="8" align="center" valign="middle" bgcolor="#FFFFFF" class="rows"><b>Selected product has not been processed yet</b></td>
</tr>
<?}?>
</table></td>
</tr>
<input type="hidden" name="numOflimit" id="numOflimit" class="button" value="<?=$i?>"/>
<tr><td><input type="hidden" name="cart1" id="cart1"></td></tr>
</table>
<form><tr><td align="center" > <button onclick="go()">Submit</button></td> </tr>
<tr> <td id="cart"> </td> </tr>
<div id="test"></div>
</form></form>
</form>
----------------------jquery code----------------------------------------------
for(k=0;k<=9000;k++)
{ //each change
$("#status"+k).change(function () {
var numOflimit = encodeURIComponent($('#numOflimit').val());
//alert(numOflimit);
for(j=0;j<=numOflimit;j++)
{
var product_name = encodeURIComponent($('#product_name'+j).val());
//alert(product_name);
var barcode = encodeURIComponent($('#barcode'+j).val());
var Quantity = encodeURIComponent($('#Quantity'+j).val());
//var unitBag = encodeURIComponent($('#unitBag'+k).val());
//var postData = $("form").serialize();
// alert(postData);
var cart=product_name + barcode + Quantity;
alert(cart);
$('#cart1').val(cart);
}
});
}
Have you got a full working example up somewhere, so we can actually try it? Debugging an undefined error in JavaScript just by looking at a code snippet is not the easiest thing in the world.
You might also want to open the JavaScript error console in Firefox and check the errors tab straight after the alert appears, as that might give you a useful pointer as to where the error is (e.g. you've forgotten to define a variable somewhere). The Firebug plugin may also help: http://getfirebug.com/
Edit: Just noticed that this seems to be a duplicate of another question:
jquery to fetch the contents from a selected row in php

PHP Trying to get string from a table cell with regular expressions

I have the following site and I want with regular expressions to get the text between the following tags
<td colspan="2" align="left" valign="top" bgcolor="#FBFAF4"> ..... </td>
I am trying with the following however it returns an empty array of $matches.
preg_match_all("/<td(.*) bgcolor=\"#FBFAF4\"\>(.*)\<\/td>/",$old_filecontents,$matches);
Which is the correct pattern for this?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Exotiq - Ðñïúüíôá</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7"> <link href="Styles.css" rel="stylesheet" type="text/css"> <link href="stylesheets/Styles.css" rel="stylesheet" type="text/css"> <script src="scripts/PopBox.js" type="text/javascript"></script> <script type="text/javascript"> popBoxWaitImage.src = "images/spinner40.gif"; popBoxRevertImage = "images/magminus.gif"; popBoxPopImage = "images/magplus.gif"; </script> <script type="text/javascript"> AC_FL_RunContent('codebase', 'http://download.macromedia.com/pub/shockwave/ cabs/flash/swflash.cab#version=9,0,28,0', 'width','675','height','445','title','Morpork', 'src','assets/flash/morepork','loop', 'false','quality','high','pluginspage', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash', 'wmode','transparent','movie','assets/flash/morepork'); </script> </head> <body background="images/fonto2.jpg" topmargin="0"> <table width="948" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="948" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="24"> </td> <td height="150" colspan="3"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="900" height="150"> <param name="movie" value="flash/top02.swf"> <param name="quality" value="high"> <param name="wmode" value="transparent"> <embed src="flash/top02.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="900" height="150"></embed></object></td> <td width="24" height="150"> </td> </tr> <tr> <td height="31" colspan="5" valign="middle"> <div align="center"> <script src="menu/xaramenu.js"></script> <script Webstyle4 src="menu/menu_.js"></script> </div></td> </tr> <tr> <td width="24"> </td> <td width="200" valign="top" background="images/GreenFasa.jpg"> <br> <table width="180" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td height="25" class="styles"> Makuti<br> <hr> </td> </tr> <tr> <td height="25" class="styles"> Fun Palm<br> <hr> </td> </tr> <tr> <td height="25" class="styles"> Alang-Alang<br> <hr> </td> </tr> <tr> <td height="25" class="styles"> Thatch<br> <hr> </td> </tr> <tr> <td height="25" class="styles"> <strong>Abaca</strong><br> <hr> </td> </tr> <tr> <td height="25" class="styles"> </td> </tr> </table></td> <td colspan="2" align="left" valign="top" bgcolor="#FBFAF4"> <div align="left"> <table width="680" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="600" height="40" class="titles">ÊáôáóêåõÝò - ÏìðñÝëåò - Abaca</td> <td width="50" align="right" valign="middle" class="titles"> <div align="right"><img src="images/uk-flag.jpg" width="30" height="17" border="0"></div></td> </tr> <tr> <td colspan="2" class="body"><p>Ç ïìðñÝëá <strong>Abaca</strong> Ýñ÷åôáé ùò Üîéïò áíôéêáôáóôÜôçò ôçò ïìðñÝëáò Rattan ðïõ åðß 15 ÷ñüíéá óôïëßæåé ôéò åëëçíéêÝò ðáñáëßåò. Ôï <strong>Abaca</strong> åßíáé Ýíá öõóéêü õëéêü ðéï <strong>áíèåêôéêü</strong> êáé ðéï üìïñöï áðü ôï Rattan. <br> Ðáñáäßäåôáé ìå <strong>îýëéíï êïñìü åìðïôéóìïý</strong> Ö8åê.<br> <br> </p> <table width="680" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="340" height="150" valign="middle"> <div align="left"><img src="images/Manufactures/Umbrelas/Abaca/AbacaUmbrela.jpg" width="328" height="500"></div></td> <td width="340" height="150" valign="bottom" class="body"> <table width="340" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="170" height="130"> <div align="center"><img src="images/Manufactures/Umbrelas/Abaca/1_Abaca02_s.jpg" width="152" height="101" class="PopBoxImageSmall" onclick="Pop (this,50,'PopBoxImageLarge');" title="ÌåãÝèõíóç" pbsrc="images/Manufactures/Umbrelas/Abaca/1_Abaca02.jpg" pbCaption="Abaca - ÏìðñÝëá ðáñáëßáò" popBoxCaptionBelow="true" /></div></td> <td width="170" height="130"> <div align="center"><img src="images/Manufactures/Umbrelas/Abaca/2_Abaca03_s.jpg" width="150" height="112" class="PopBoxImageSmall" onclick="Pop (this,50,'PopBoxImageLarge');" title="ÌåãÝèõíóç" pbsrc="images/Manufactures/Umbrelas/Abaca/2_Abaca03.jpg" pbCaption="Abaca - ÏìðñÝëá ðáñáëßáò" popBoxCaptionBelow="true" /></div></td> </tr> <tr> <td width="170" height="130"> <div align="center"><img src="images/Manufactures/Umbrelas/Abaca/3_Abaca01_s.jpg" width="150" height="112" class="PopBoxImageSmall" onclick="Pop (this,50,'PopBoxImageLarge');" title="ÌåãÝèõíóç" pbsrc="images/Manufactures/Umbrelas/Abaca/3_Abaca01.jpg" pbCaption="Abaca - ÏìðñÝëá ðáñáëßáò" popBoxCaptionBelow="true" /></div></td> <td width="170" height="130"> <div align="center"></div></td> </tr> <tr> <td width="170" height="130"> <div align="center"></div></td> <td width="170" height="130"> <div align="center"></div></td> </tr> <tr> <td width="170" height="130"> <div align="center"></div></td> <td width="170" height="130"> <div align="center"></div></td> </tr> </table></td> </tr> <tr> <td width="340" height="50" valign="top"> <p align="center"> </p></td> <td width="340" height="50" valign="top"> <div align="center" class="perigrafes">ÊëéêÜñåôáé ðÜíù óôéò öùôïãñáößåò ãéá ìåãÝèõíóç</div></td> </tr> <tr> <td width="340" valign="bottom"> <div align="center"> </div></td> <td width="340" valign="bottom"> <p align="center"> </p></td> </tr> <tr> <td width="340" valign="top"> <div align="center"></div></td> <td width="340" valign="top"> <p align="center"> </p></td> </tr> <tr> <td height="20" colspan="2" valign="top"> </td> </tr> </table></td> </tr> </table> <font color="#FFFFFF"></font></div></td> <td width="24" height="420"> </td> </tr> <tr> <td width="24"> </td> <td width="200"> </td> <td width="600"> </td> <td width="100"> </td> <td width="24"> </td> </tr> </table></td> </tr> <tr> <td height="22"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#007F3E"> <tr> <td height="25"> <div align="center" class="styles">All rights reserved ® Designed by CONTINENTAL ADVERTISING </div></td> </tr> </table></td> </tr> </table> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-12742174-1"); pageTracker._trackPageview(); } catch(err) {}</script> </body> </html>
Given that the cell you're talking about contains HTML, another table in fact, you can't do traditional termination checking ... or you'll get the content between the cell opening and the first </td> you find. Plus '.' isn't multi-line friendly, so unless your cell opens and terminates on the same line, you'll get no matches.
I'd say don't use regular expressions for this. Try an XML parser.
If you were just getting plain text, that'd be fine, but because you're returning HTML which contains your terminator, you'll need to use a parser with some kind of DOM depth awareness ... ... or find a way to count terminators in regex.

Categories