PHP SELECT where - php

First of all sorry for my bad english.
I have a php script that display a list of products from sql:
$cerereSQL = 'SELECT * FROM `produse`';
$rezultat = mysql_query($cerereSQL);
while($rand = mysql_fetch_array($rezultat)){
echo '
<form action="editare.php" method="post">
<table width="100%" cellpadding="0" cellspace="0">
<tr style="text-align:center;" height="25">
<td style="border:1px solid black;" width="7%" height="10"><i> '.$rand['indice'].' </i></td>
<td style="border:1px solid black;" width="40%"><i> '.$rand['denumire_produs'].' </i></td>
<td style="border:1px solid black;" width="20%"><i> '.$rand['producator'].' </i></td>
<td style="border:1px solid black;" width="10%"><i> '.$rand['pret'].' </i></td>
<td style="border:1px solid black;" width="6%"><i> '.$rand['valuta'].' </i></td>
<td style="border:1px solid black;" ><i> '.$rand['cod'].' </i></td>
<td width="5%"><input type="submit" name="submit" value="Edit"></td>
</tr>
</table>
</form>';
I want the button (value="Edit") from the end of each row to select exactly the product from the same row, to be edited.
I tried next code but did't work (i have a list with 5000 lines):
switch($_GET['actiune'])
{
case '':
$cerereSQL = 'SELECT * FROM `produse` WHERE denumire_produs="'.$rand['denumire_produs'].'"';
I know i have to use: $cerereSQL = 'SELECT * FROMproduseWHERE denumire_produs="......"; but i don't know how to do this, how to make the edit button, open the exactly product from the same row.
Can someone please help me with a suggestion?

Perhaps you mean to do this:
$cerereSQL = 'SELECT * FROM `produse`';
$rezultat = mysql_query($cerereSQL);
while($rand = mysql_fetch_array($rezultat)){
echo '
<form action="editare.php" method="post">
<table width="100%" cellpadding="0" cellspace="0">
<tr style="text-align:center;" height="25">
<td style="border:1px solid black;" width="7%" height="10"><i> '.$rand['indice'].' </i></td>
<td style="border:1px solid black;" width="40%"><i> '.$rand['denumire_produs'].' </i></td>
<td style="border:1px solid black;" width="20%"><i> '.$rand['producator'].' </i></td>
<td style="border:1px solid black;" width="10%"><i> '.$rand['pret'].' </i></td>
<td style="border:1px solid black;" width="6%"><i> '.$rand['valuta'].' </i></td>
<td style="border:1px solid black;" ><i> '.$rand['cod'].' </i></td>
<td width="5%"><input type="hidden" name="denumire_produs" value="'.$rand['denumire_produs'].'"><input type="submit" name="submit" value="Edit"></td>
</tr>
</table>
</form>';
Then, snag the denumire_produs from $_POST:
$denumire_produs = intval($_POST['denumire_produs']); // is this an INTeger? Otherwise, use `mysql_real_escape_string` -- or use PDO prepared statements.
$cerereSQL = 'SELECT * FROM `produse` WHERE denumire_produs="'.$denumire_produs.'"';
Is this what you're trying to do?

Four Things
First You dun need single Quotes around table name
$cerereSQL = 'SELECT * FROM produse';
Second Dont use mysql it is deprecated unless you really need to do
third you need to do this while you are echo any thing like that method=\"POST\" your code will not execute due to that
four you need hidden field
like that
<input type=\"hidden\" name=\"demuire\" value=\"'.$ran['whatever'].'\"/>
<td width="5%"><input type="submit" name="submit" value="Edit"></td>
then you can get this by simple
$_POST['demuire']

Related

How to add contents in new page using TCPDF?

I am using TCPDF stuff, I have created a PDF page with some Information. But i need to show some data to next page deliberately.
pdf -> AddPage();
above code is used to create a new page which i know. But i wanna add specific data to this particular page would be the problem.
Help me to come out on this issue.
I have written my code like this
while($row = mysql_fetch_array($query)){
$inv = $row["INVNUM"];
$weight = $row["WEIGHT"];
if($i<4){
if($row["SIZECODE"] == "02"){
$temp .= '<tr nobr="true">
<td class="size1" style="border:1px solid #000">'.$json_a[$i]["S"].'</td>
<td class="size2" style="border:1px solid #000">'.$json_a[$i]["M"].'</td>
<td class="size3" style="border:1px solid #000">'.$json_a[$i]["L"].'</td>
<td class="size4" style="border:1px solid #000">'.$json_a[$i]["XL"].'</td>
<td class="size5" style="border:1px solid #000">'.$json_a[$i]["2X"].'</td>
<td class="size6" style="border:1px solid #000">'.$json_a[$i]["3X"].'</td>
</tr>';
}
else if($row["SIZECODE"] == "03"){
$temp .= '<tr>
<td style="border:1px solid #000">'.$json_a[$i]["32"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["34"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["36"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["38"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["40"].'</td>
<td style="border:1px solid #000">'.$json_a[$i]["42"].'</td>
</tr>';
}
$i = $i+1;
}
else{
$pdf ->AddPage();
$i = 0;
$temp.='<tr nobr="true">
<td colspan="18" style="border:1px solid #000">All sales are Final.</td>
<td colspan="2" style="border:1px solid #000">SUB TOTAL:</td>
<td style="border:1px solid #000"></td>
<td style="border:1px solid #000"></td>
</tr>
<tr>
<td colspan="18" rowspan="2" style="border:1px solid #000">Number # : '.$rowFix["Num"].' </td>
</tr>
<tr>
<td colspan="2">TOTAL DUE: $</td>
<td style="border:1px solid #000"></td>
<td style="border:1px solid #000"></td>
</tr>';
}
}
I have to bring the table rows to the next page when the $i value greater than 4.
$pdf->AddPage('P', 'A4');
$pdf->Cell(0, 0, 'A4 PORTRAIT', 1, 1, 'C');
First, output the content for the first page. Then:
pdf -> AddPage();
Then, output for the second page. Anything sent after this command will be outputted onto the second page.

Getting value of specific td in selected row

I have the way to get the value of the td's at the time the row is selected using
$(this).find('.servid').val()
However I cannot find the way to get this value later.
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
I need to later, after filling out more of the form, get the val() of the selected tr -> servid td
I have tried various things but they are not working
$('#servicetable .selected > td:nth-child(2)').val();
$('#servicetable').find('.selected > td:nth-child(2)').val();
$('#servicetable').find('tr.selected').find('.servid').val();
Below goes your sollution
alert($('#servicetable tr.selected > td.servid').attr('value'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
One more thing you can not put value like this as it will not validate as per w3c, recommanded to put data-value, if HTML5.
And in jquery also you can put like
alert($('#servicetable tr.selected > td.servid').attr('data-value'));
$('#value1').text($('#servicetable .selected > td:nth-child(2)').attr('value'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
<label id="value1"/>
IDEAL WAY
use data attribute as shown below,
<td class="servid" data-value="4004072">72569000</td>
$('#value1').text($('#servicetable .selected > td:nth- child(2)').data('value'));
It might be better if you tag the TR with the id you need and then have the "attributes" in the TDs.
You can then parse TDs based on TR
<tr class="selected" id="98989">
<td class="city">Lisbon</td>
.....
_tr = $("#servicetable .selected");
_trId=_tr.attr("id");
_trCity = _tr.find(".city").text();
.....

PHP Table Hyperlink

I have a table that returns information from mysql. However, I want to hyperlink each postcode so that when the user clicks on one it will redirect them to an appropriate page. Here is the code that is in the table:
<table style="border:2px solid black; border-collapse:collapse;" cellspacing="2" cellpadding="2" align="center">
<tr align="center" width="auto">
<td bgcolor="#66CCFF" style="border:2px solid black;"><b> Consultation Date </b></td>
<td bgcolor="#66CCFF" style="border:2px solid black;"><b> Patient Health Care Number </b></td>
<td bgcolor="#66CCFF" style="border:2px solid black;"><b> Location </b></td>
<td bgcolor="#66CCFF" style="border:2px solid black;"><b> Post Code </b></td>
<td bgcolor="#66CCFF" style="border:2px solid black;"><b> Update Record </b></td>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"Consultation Date");
$f2=mysql_result($result,$i,"Patient HCN");
$f3=mysql_result($result,$i,"Location");
$f4=mysql_result($result,$i,"Post Code");
?>
<tr>
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><?php echo $f1; ?></td>
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><b><?php echo $f2; ?></b></td>
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><?php echo $f3; ?></td>
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><?php echo "<a href=\"coveragehcp.php?patient= ". $f2 ."\">"?><b><?php echo $f4; ?></b></td>
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><?php echo "<button onclick=\"window.location.href='hcp_form_update.php?patient=$f2'\">"?>Update Record</button></td> <!-- This button redirects the user to the auto-populated form that holds the patients information which allows the user to update it -->
</tr>
<?php
$i++;
}
?>
However when the user click on the link below I want it to redirect them to another page according to the post code clicked.
<td align="center" bgcolor="#FFFFFF" style="border:2px solid black;"><?php echo "<a href=\"coveragehcp.php?patient= ". $f2 ."\">"?><b><?php echo $f4; ?></b></td>
What I would do is to use the post code as a parameter in the GET, and then use the $_GET['postcode'] to achieve which page should be presented for the user.
so your PHP code for the link would look something like:
<a href=\"coveragehcp.php?patient= ".$f2."&postcode=".$f4."\">"?><b><?php echo $f4; ?></b></td>
and on the coveragehcp.php page use this
$postcode = $_GET['postcode'];
if(!empty($postcode))
{
if($postcode == 90210) {
//Echo beverly hills page
}
else {
//Echo default page
}
}

Sending particular input value among input array into JavaScript

I am using a query to populate a list of messages by using a loop, here's my code:
<?php
$sql_i_msg_sent_waiting="SELECT t1.i_message_id,t2.username,t2.name,t2.propic,t2.age,t2.dob,t3.religion,t3.caste
FROM candidate_i_message as t1, candidate_register_table as t2, candidate_social_table as t3 WHERE t1.from_username='$_SESSION[logged_user]' AND t1.to_username=t2.username AND t2.username=t3.username AND t1.status='0'";
$result_i_msg_sent_waiting=mysql_query($sql_i_msg_sent_waiting,$con);
$count=mysql_num_rows($result_i_msg_sent_waiting);
echo $count;
?>
<div id="section_i_message_sent_waiting" style="width:650px; overflow:auto;">
<h2>Awaiting Sent Request</h2>
<?php
if(mysql_num_rows($result_i_msg_sent_waiting)==0)
{
?>
<div style="width:650px; border-bottom:1px solid #CCCCCC" align="center">
<table border='0' cellspacing='0' cellpadding='0' width='550px' align='center'>
<tr style='padding-botton:5px; border-bottom:1px solid #CCCCCC'>
<td width='50px'/>
<td><FONT COLOR=red FACE='Geneva, Arial' SIZE=2>
No Messages Found.</FONT></td>
<td width='50px'/></tr></table>
</div>
<?php
}
else
{
while($row_i_msg_sent_waiting=mysql_fetch_array($result_i_msg_sent_waiting))
{
?>
<div style="width:650px; border-bottom:1px solid #CCCCCC" align="center">
<table width="550px" cellpadding="0" cellspacing="0" border="2" align="center" style="vertical-align:middle">
<tr style='vertical-align:middle'>
<td style='vertical-align:middle' width="100px">
<form method='post' action='id.php' name='showid' id='showid'>
<input type='hidden' name='pro_username' id='pro_username'
value="<?php echo $row_i_msg_sent_waiting['username'];?>"/>
<input type='image' src='<?php echo $row_i_msg_sent_waiting['propic'];?> ' style="width:100px; vertical-align:middle"/>
</form>
</td>
<td style='vertical-align:middle;border-right:1px solid #CCCCCC;border-top:1px solid #CCCCCC' width="450px" >
<table width="450px" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="10px"/>
<td width="100px" align="left">Name</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['name'];?>
</td>
<td width="10px">
<td width="100px" align="left">Age</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['age'];?>
</td>
<td width="10px"/>
</tr>
<tr>
<td width="10px"/>
<td width="100px" align="left">Date Of Birth</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['dob'];?>
</td>
<td width="10px">
<td width="100px" align="left">Religion</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['religion'];?>
</td>
<td width="10px"/>
</tr>
<tr>
<td width="10px"/>
<td width="100px" align="left">Caste</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['caste'];?>
</td>
<td width="10px">
<td width="100px" align="left">Religion</td>
<td width="10px">:</td>
<td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['religion'];?>
</td>
<td width="10px"/>
</tr>
<tr>
<td width="10px"/>
<td width="100px" align="left">Action</td>
<td width="10px">:</td>
<td width="100px" align="left">
<form method="post" name="cancel_request_form" id="cancel_request_form"
action="javascript:cancel_request(document.getElementById('cancel_request_form'));">
<input type="text" name="no_of_msg" id="no_of_msg" value="<?php echo $count;?>"/>
<input type="text" name="cancel_request[]" id="cancel_request[]"
value="<?php echo $row_i_msg_sent_waiting['i_message_id'];?>"/>
<input type="submit" name="cancel" id="cancel" class="button" style="width: 100px" value="Cancel Request"/>
</form>
</td>
<td width="10px">
<td width="100px" align="left"></td>
<td width="10px">:</td>
<td width="100px" align="left">
</td>
<td width="10px"/>
</tr>
</table>
</td>
</tr>
</table>
</div>
<?php
}
}?>
</div>
the line
<input type="text" name="cancel_request[]" id="cancel_request[]"
value="<?php echo $row_i_msg_sent_waiting['i_message_id'];?>"/>
creates a array of input type text, and each input text contains corressponding message_id...since each input text is contained within a form, and since the form is within a loop, the form is also repeatative..when the submit button will be clicked corressponding to a particular form, the input type text[] within that form will fire the value to get accepted in the javascript i give below...
<script>
function cancel_request(obj) {
alert(document.getElementByName('cancel_request[]').value);
}
</script>
But the script is showing problem, it's not working at all. I don't know what's wrong. It's not displaying anything. There is message_id 1 and 2: one text type input[] contains value 1, another value 2.
When I click the submit button corresponding to value 1, the value of that text type input will be send to JavaScript and displayed. What's the problem with my script?
The function is getElementsByName - Elements in the plural. You then access it like an array.

print html table using php dom document

Need help,
I would like to print this html code in our web page using php domdocument, but struggling
I have also write the code with below. Could you please help me.
<table width="610" id="machine" border="0" cellSpacing="10"
cellPadding="0">
<TBODY>
<TR>
<TD colSpan=2 align=left>
<DIV id=breadcrumbs><A href="/new-and-used-machinery.php">All
Machines</A> > Air Compressors
> </DIV>
<DIV id=ForSale>FOR SALE:</DIV>
<H1>5 CFM Used Speedaire Air Compressor, Single Phase, 60 Gallon
tank, 1.5 H.P. #A1194</H1>
</TD>
</TR>
<TR>
<TD id=photos vAlign=top width=270 align=middle>
<DIV id=ClickPhotos>Click On Photos For Larger Image:</DIV>
<!-- PHOTOS DB div style="margin-bottom:12px"><img src="thumb.php?id=A1194.jpg&w=250&h=250"></div --><!-- ORIG PHOTOS div style="margin-bottom:12px"><img src="thumb.php?f=A1194.jpg&w=250&h=250"></div -->
<DIV style="MARGIN-BOTTOM: 12px"><A
href="/photos.php?id=7135&p=1"><IMG
title="Used Speedaire Air Compressor"
alt="Used Speedaire Air Compressor"
src="/class/class_Image.php?load=cGhvdG9zL0ExMTk0LmpwZ3wyNTB8MjUwfDF8"></A>
</DIV>
<!-- PHOTOS DB div style="margin-bottom:12px"><img src="thumb.php?id=A1194_1.jpg&w=250&h=250"></div --><!-- ORIG PHOTOS div style="margin-bottom:12px"><img src="thumb.php?f=A1194_1.jpg&w=250&h=250"></div -->
<DIV style="MARGIN-BOTTOM: 12px"><A
href="/photos.php?id=7135&p=2"><IMG
title="Used Speedaire Air Compressor"
alt="Used Speedaire Air Compressor"
src="/class/class_Image.php?load=cGhvdG9zL0ExMTk0XzEuanBnfDI1MHwyNTB8MXw="></A>
</DIV>
</TD>
<TD id=details vAlign=top width=310 align=left>
<TABLE
style="BORDER-BOTTOM: #cdcdcd 1px solid; BORDER-LEFT: #cdcdcd 1px solid; PADDING-BOTTOM: 10px; BACKGROUND-COLOR: #eee; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; BORDER-TOP: #cdcdcd 1px solid; BORDER-RIGHT: #cdcdcd 1px solid; PADDING-TOP: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px"
id=machine_buttons border=0 cellSpacing=3 cellPadding=0>
<TBODY>
<TR>
<TD><A onmouseover="swapImg('Manufact', true)"
onmouseout="swapImg('Manufact', false)"
href="/manufacturer.php?brand=Speedaire"><IMG id=icon_Manufact
border=0 alt="" src="/images/machine_button_Manufact.gif"
width=66 height=33><BR>
Manufacturer Info</A></TD>
</TR>
<TR>
<TD><A onmouseover="swapImg('Category', true)"
onmouseout="swapImg('Category', false)"
href="/listing.php?cat=Air+Compressors"><IMG id=icon_Category
border=0 alt="" src="/images/machine_button_Category.gif"
width=66 height=33><BR>
Similar Machines</A></TD>
</TR>
<TR>
<TD><A onmouseover="swapImg('Brochure', true)"
onmouseout="swapImg('Brochure', false)"
href="/manufacturer.php?brand=Speedaire#brochures" target=_blank><IMG
id=icon_Brochure border=0 alt=""
src="/images/machine_button_Brochure.gif" width=66 height=33><BR>
OEM Brochure</A></TD>
</TR>
<TR>
<TD><A style="CURSOR: pointer" id=shadowbox_email class=sb
title="Used Speedaire Air Compressor" href="/share.php?id=7135"
rel=shadowbox;width=520;height=500 shadowboxCacheKey="1"
onMouseOver="swapImg('Email', true)"
onMouseOut="swapImg('Email', false)" jQuery1316148993734="2"><IMG
id=icon_Email border=0 alt=""
src="/images/machine_button_Email.gif" width=66 height=33
jQuery1316148993734="3"><BR>
Email A Friend</A></TD>
</TR>
<TR>
<TD><A onmouseover="swapImg('Ship', true)"
onmouseout="swapImg('Ship', false)" href="/shipping.php?id=7135"><IMG
id=icon_Ship border=0 alt=""
src="/images/machine_button_Ship.gif" width=66 height=33><BR>
Shipping</A></TD>
</TR>
</TBODY>
</TABLE>
<DIV id=QuickQuote><A href="/machine-print.php?id=7135" target=_blank><IMG
title="Print a Quick Quote!" border=0 alt="Print a Quick Quote!"
align=absMiddle
src="http://www.sterlingmachinery.com/images/quick_quote.jpg"> </A></DIV>
<DIV style="MARGIN: 5px 0px 0px 45px">
<FORM method=post name=addmachine action=/request-quote.php
jQuery1316148993734="5"><INPUT id=add
title="Add Machine to Quote Cart" value="Add Machine to Quote Cart"
alt="Add Machine to Quote Cart"
src="http://www.sterlingmachinery.com/images/quick_add.jpg"
type=image name=add jQuery1316148993734="7"> <IMG
style="DISPLAY: none; CURSOR: pointer" id=remove
title="Remove from quote cart" name=remove
alt="Remove from quote cart" src="/images/remove.gif" width=16
height=16 jQuery1316148993734="8">
<DIV style="MARGIN-TOP: 5px"><INPUT id=view title="View Quote Cart"
value="View Quote Cart" alt="View Quote Cart"
src="http://www.sterlingmachinery.com/images/quick_view.jpg"
type=image name=view></DIV>
</FORM>
</DIV>
<BR>
<TABLE id=machine_stats border=0 cellSpacing=5 cellPadding=0
width=300>
<TBODY>
<TR>
<TD class=field vAlign=top align=right>Stock #</TD>
<TD vAlign=top align=left>A1194</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Manufacturer:</TD>
<TD vAlign=top align=left>Speedaire</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Model:</TD>
<TD vAlign=top align=left></TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Capacity:</TD>
<TD vAlign=top align=left>5 CFM</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Price:</TD>
<TD vAlign=top align=left><SPAN style="BACKGROUND-COLOR: #ffff00">$350</SPAN></TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Category:</TD>
<TD vAlign=top align=left>Air Compressors</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Condition:</TD>
<TD vAlign=top align=left>Used Machinery</TD>
</TR>
</TBODY>
</TABLE>
<FIELDSET><LEGEND>Full Description</LEGEND>
<TABLE id=machine_specs border=0 cellSpacing=0 cellPadding=4
width=300>
<TBODY>
<TR>
<TD
style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px"
class=field vAlign=top align=right>CFM Rating:</TD>
<TD
style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px"
vAlign=top align=left>5</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Tank Size:</TD>
<TD vAlign=top align=left>60 Gallon</TD>
</TR>
<TR>
<TD class=field vAlign=top align=right>Weight:</TD>
<TD vAlign=top align=left>400 LBS.</TD>
</TR>
</TBODY>
</TABLE>
</FIELDSET>
<FIELDSET><LEGEND>Equipped With</LEGEND>Single Phase Motor<BR>
Horsepower: 1 1/2</FIELDSET>
<DIV class=privates>* Temporary specifications subject to
verification.<BR>
All quotations subject to prior sale.</DIV>
</TD>
</TR>
</TBODY>
</table>
I have write this code but its not getting properly.
Outer foreach loops runs 17 times but should be only two time because the outer table has only two rows.
$xml = new DOMDocument();
$xml->validateOnParse = true;
$xml->loadHTMLFile($url);
$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[#id='machine']")->item(0);
echo "<table border=2>";
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $row) //there i only two rows.
{
echo "<tr>";
$cells = $row -> getElementsByTagName('td');
foreach ($cells as $cell)
{
echo "<td>";
foreach ($cell->getElementsByTagName('table') as $innerTable )
{
foreach ( $innerTable->getElementsByTagName("tr") as $innerTrs)
{
foreach ( $innerTrs->getElementsByTagName('td') as $innerCell)
{
foreach ($innerCell->getElementsByTagName('a') as $a )
{
echo '<br>'.$a->nodeValue.'';
foreach ($a->getElementsByTagName('img') as $img )
echo '<br> <img src="'.GetMainBaseFromURL($url).$img->getAttribute('src').'">';
}
}
}
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Then how can i print as it is html table in my web page using php dom document.
I am new in php dom.
Thank you.
Thanks a lot.
getElementsByTagName works on the whole document. The document has 17 tr elements, so it will return them all regardless where those are placed.
As you want to look for specific tr elements, or more specifically for images inside a specific table, I suggest you let xpath do most of the work:
$xpath = new DOMXPath($xml);
$nodes =$xpath->query("//table[#id='machine']/tbody/tr/td/div/a/img");
echo 'Found ', $nodes->length, ":\n";
foreach($nodes as $node)
{
$img = $node;
$a = $node->parentNode;
$aHref = $a->getAttribute('href');
$imgSrc = $img->getAttribute('src');
printf("Element:\n + href: %s;\n +- src: %s\n\n", $aHref, $imgSrc);
}
This will list 3 elements already (not 2). You need to further on decide which ones to drop, but this should make it more easy. Demo & Output:
Found 3:
Element:
+ href: /photos.php?id=7135&p=1;
+- src: /class/class_Image.php?load=cGhvdG9zL0ExMTk0LmpwZ3wyNTB8MjUwfDF8
Element:
+ href: /photos.php?id=7135&p=2;
+- src: /class/class_Image.php?load=cGhvdG9zL0ExMTk0XzEuanBnfDI1MHwyNTB8MXw=
Element:
+ href: /machine-print.php?id=7135;
+- src: http://www.sterlingmachinery.com/images/quick_quote.jpg
Another tip: First normalize your data into a structure like an array. You can then use your array to generate the output. That done you can separate the parsing from the output which will make it more easy for you to achieve your goals.
Parse the document into an array.
Modify values as you need them in that array (e.g. GetMainBaseFromURL).
Generate the output based on the array.
The foreach loop iterates 17 just because you have 17 tags in the HTML code you posted.
Actually the $rows = $table->getElementsByTagName("tr"); extracts any tag at any depth in the tree.
To get only the two outer you can do something like
$tbody = $table->childNodes;
$outerTr = $tbody->childNodes;
An the iterate on $outerTr
You can also build an Xpath expression to get only the two outer
I did not test yout code however, in the others foreach, keep in mind the same problem of getElementsByTagName("tr") (or other tags td,a ..)
If you just want to copy the table you can do this:
<?php
$xml = new DOMDocument();
$xml->validateOnParse = true;
$xml->loadHTMLFile($url);
$table = $xml->getElementById('machine');
echo $xml->saveXML($table);
?>

Categories