I'm trying to display data horizontally in a table by using PHP & MySQL. The code that I'm currently using is (Obviously all values will be called from MySQL but currently I have put static values):-
$query = "select * from bonusdetails where BonusType='Free Money' order by Validity ASC limit 0,3;";
$result = mysql_query($query);
echo '<table width="150" border="0" cellspacing="0" cellpadding="0" bgcolor="#941010" style="font-family:Arial, Helvetica, sans-serif; float:left; color:#ffffff;">';
while ($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td align="center" style="padding:2px 0 0 0;"><img src="abc.jpg" width="124" height="64"/></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center" style="font-size:15px; padding:8px 0 0 0;">No Deposit Bonus</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="font-size:17px; padding:5px 0 0 0;" align="center" >CODE: STAR75</td>';
echo '</tr>';
echo '<tr>';
echo ' <td align="center" style="padding:8px 0 5px 0;"><a href="#"><input name="more-details" type="button" style="background:url(images/more-details.png) no-repeat; width:102px;
height:27px; text-decoration:none; border:none; cursor:pointer;" /></a></td>';
echo '</tr>';
}
echo '</table>';
I'm getting output in this format: http://www.casinobonustips.com/submitbonus/bonus.php. is there a way to display the same data horizontally?
I will highly appreciate your assistance on this.
Thanks and Regards,
Devjeet
What you have:
<table>
<tr>
<td>A1</td>
</tr>
<tr>
<td>B1</td>
</tr>
<tr>
<td>C1</td>
</tr>
<tr>
<td>A2</td>
</tr>
<tr>
<td>B2</td>
</tr>
<tr>
<td>C2</td>
</tr>
<tr>
<td>A3</td>
</tr>
<tr>
<td>B3</td>
</tr>
<tr>
<td>C3</td>
</tr>
</table>
What you should have:
<table>
<tr>
<td>
<div>A1</div>
<div>B1</div>
<div>C1</div>
</td>
<td>
<div>A2</div>
<div>B2</div>
<div>C2</div>
</td>
<td>
<div>A3</div>
<div>B3</div>
<div>C3</div>
</td>
</tr>
</table>
Example: http://jsfiddle.net/ePMmx/1/
Do your styling to suit your needs.
You need to group <td></td> in one <tr></tr> to display them "horizontally", on the other hand, you are mixing "view" stuff with processing and retrieving the data, you maybe have to read about MVC pattern for better programming
Related
i create a report. i try print preview this report.
but i want show 6 data/page page size a4 (landscape).
i already try loop 24 data. and try print preview in google chrome
there 1 data must be down to page 2
see my picture
[![enter image description here][1]][1]
then i check page 2 already same.
this page 2
[![enter image description here][2]][2]
How to fix this, i want report show only 6 data/page
this is my full code, you can try in localhost.
<style>
#caption
{
color:white;
font-weight:bold;
text-align:center;
background-color: #4CAF50;
}
table
{
border-collapse: collapse;
}
th, td
{
padding: 0px;
font-family:arial;
font-size:13px
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
<table align="center" width="100%" border="1">
<tr>
<td colspan="7" style="vertical-align: top;" id="caption"><font size="3">Data Pendaftar</font></td>
</tr>
<?php
$x = 1;
while($x <= 24)
{
?>
<tr>
<td id="caption">Foto Siswa/i</td>
<td id="caption">Nama Lengkap Siswa/i</td>
<td id="caption">Tempat Lahir</td>
<td id="caption">Tanggal Lahir</td>
<td id="caption">Usia</td>
<td id="caption">L/P</td>
<td id="caption">Agama</td>
</tr>
<tr>
<td align="center" rowspan="3">
<img border="1" src="https://akphoto4.ask.fm/769/854/890/910003014-1s093lj-dc47ceo09kenpt5/original/avatar.jpg"style="width:80px;height:80px;">
</td>
<td align="center">Vasco Da Gama</td>
<td align="center">Spain</td>
<td align="center">01 February 1800</td>
<td align="center">79</td>
<td align="center">L</td>
<td align="center">Kristen</td>
</tr>
<tr>
<td id="caption" colspan="2">Alamat</td>
<td id="caption" colspan="2">No. Telpon | No. Hp </td>
<td id="caption" >Tanggal Daftar</td>
<td id="caption">Status</td>
</tr>
<tr>
<td colspan="2">Spain</td>
<td colspan="2" align="center">0800000000</td>
<td >07 Februari 2000 13:15:33</td>
<td align="center">Pending</td>
</tr>
<?php
$x++;
}
?>
</table>
In this situation, you could create a separate table per page with only 6 rows. Only display the "Data Pendaftar" on the first page, add class="break" to every table except the table on the last page and add a CSS rule table.break{page-break-after:always}. I added a <br /> after each table, so it looks good in the browser before it prints. This will always print 6 rows per page, even when you're accidentally in portrait mode...
<style>
#caption{color:white;font-weight:bold;text-align:center;background-color:#4CAF50}
table{border-collapse:collapse}
th, td{padding: 0px;font-family:arial;font-size:13px}
tr:nth-child(even){background-color:#f2f2f2}
th{background-color:#4CAF50;color:white}
table.break{page-break-after:always}
</style>
<?php $i = 1; while ($i <= 4) { ?>
<table align="center" width="100%" border="1"<?php if ($i < 4) { ?> class="break" <? } ?>>
<?php if ($i == 1) { ?>
<tr>
<td colspan="7" style="vertical-align: top;" id="caption"><font size="3">Data Pendaftar</font></td>
</tr>
<?php } $x = 1; while ($x <= 6) { ?>
<tr>
<td id="caption">Foto Siswa/i</td>
<td id="caption">Nama Lengkap Siswa/i</td>
<td id="caption">Tempat Lahir</td>
<td id="caption">Tanggal Lahir</td>
<td id="caption">Usia</td>
<td id="caption">L/P</td>
<td id="caption">Agama</td>
</tr>
<tr>
<td align="center" rowspan="3">
<img border="1" src="https://akphoto4.ask.fm/769/854/890/910003014-1s093lj-dc47ceo09kenpt5/original/avatar.jpg"style="width:80px;height:80px;">
</td>
<td align="center">Vasco Da Gama</td>
<td align="center">Span</td>
<td align="center">01 Feb 1800</td>
<td align="center">79</td>
<td align="center">L</td>
<td align="center">Kristen</td>
</tr>
<tr>
<td id="caption" colspan="2">Alamat</td>
<td id="caption" colspan="2">No. Telpon | No. Hp </td>
<td id="caption" >Tanggal Daftar</td>
<td id="caption">Status</td>
</tr>
<tr>
<td colspan="2">Spain</td>
<td colspan="2" align="center">021000000 | 0800000000</td>
<td >07 Februari 2000 13:15:33</td>
<td align="center">Pending</td>
</tr>
<?php $x++; } ?>
</table>
<br />
<?php $i++; } ?>
I have a Table already written in PHP that echos out data called from the database like so:
<TABLE cellSpacing=1 cellPadding=2 align=center bgColor=#aaaaaa border=0 width="100%" class="logintbl">
<TR>
<TD bgColor=whitesmoke colSpan=0><B>Pages</B></td>
</tr>
<tr>
<td>
<table align="center" cellSpacing=0 cellPadding=2 border="0" width="100%">
<tr>
<td align="center" valign="bottom"> <font color="#4d71a1"><b>Page Name</b></font> </td>
</tr>
<?php while ($row = mssql_fetch_array($result)) { ?>
<tr bgcolor="#eeeeee">
<td align="center"><?php echo $row["PageURL"]; ?></td>
<td align="center">
<img src="images/0013-pen.gif" width="16" height="16" alt="" border="0">
</td>
</tr>
<?php } ?>
<tr><td colspan="7"> </td></tr>
<tr>
<td colspan="7" align="center">
</td>
</tr>
</table>
</td>
</tr>
</table>
I have been trying to alternate the colours of the rows, using a snippet of PHP and after some research implemented this:
<tr bgcolor="<?php echo ($clrCounter++ % 2 == 0 ? '#000000' : '#ffffff'); ?>">
It doesn't seem to work correctly, so I feel I am going wrong somewhere, I know there is longer ways to implement this that I could implement. I was just hoping for something simple. Am I wasting effort trying to implement it this way?
I integrated it as follows:
<TABLE cellSpacing=1 cellPadding=2 align=center bgColor=#aaaaaa border=0 width="100%" class="logintbl">
<TR>
<td bgColor=whitesmoke colSpan=0><B>Pages</B></td>
</tr>
<tr>
<td>
<table align="center" cellSpacing=0 cellPadding=2 border="0" width="100%">
<tr bgcolor="#3A7525">
<td align="center" valign="bottom"> <font color="#4d71a1"><b>Page Name</b></font> </td>
</tr>
<?php while ($row = mssql_fetch_array($result)) { ?>
<tr bgcolor="<?php echo ($clrCounter++ % 2 == 0 ? '#C2C2C2' : '#ffffff'); ?>">
<td align="center"><?php echo $row["PageURL"]; ?></td>
<td align="center">
<img src="images/0013-pen.gif" width="16" height="16" alt="" border="0">
</td>
</tr>
<?php } ?>
<tr>
<td colspan="7" align="center">
</td>
</tr>
</table>
</td>
</tr>
</table>
It sort of works, but for some reason the very first entry is blue? When I've specified white and grey.
You could use the following when looping through the results returned from your db:
<?php
// Define row colors
$color1 = "#FFFFFF";
$color2 = "#F4F9FF";
// Set row counter
$row_count = 0;
while ($row = mssql_fetch_array($result)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr bgcolor="<?php echo $row_color; ?>">
<td align="center"><?php echo $row["PageURL"]; ?></td>
<td align="center">
<img src="images/0013-pen.gif" width="16" height="16" alt="" border="0">
</td>
</tr>
<?php
$row_count++;
}
?>
Alternatively, you could replace the bgcolor tags and assign a CSS class to each row.
Use the CSS selector :nth-of-type( ).
By putting different styles for both the :nth-of-type(even) and :nth-of-type(odd) the browser does the alternating styling for you, so you won't have to worry about it.
See the W3Schools entry on this.
Try This:
<tr <?php if($i%2){?>bgcolor="#eeeeee"<?php } else{ ?>bgcolor="red" <?php } $i++; ?>>
Thanks to Bas van den Heuvel for the great answer using CSS. If you encountered extra line spacing like I did, and want to remove it, use the following example code. This will make the alternating color lines be tighter together. (I used light grey and white)
p:nth-of-type(odd)
{
background:#e2e2e2;
margin: 0px;
padding: 0px;
}
p:nth-of-type(even)
{
background:#ffffff;
margin: 0px;
padding: 0px;
}
my aim is to pull data from mysql and print it in html table. asumming that 1,2,3...8 are the data
<table style="width: 100%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</table>
this is the code i have so far but this will only print out the column but no the row. plz help. thank you
<table style="width: 100%; color:aqua">
<?php
$showFoto = getFoto();
echo '<tr>';
foreach($showFoto as $Foto){
echo '<td class="afs"><img alt="" src="img/'.$Foto['img'].'.'.$Foto['ext'].'"><br>'.$Foto['about'].'</td>';
}
echo '</tr>';
?>
</table>
TRY
<table width ="100%" style="color:aqua" cellpadding="2" cellspacing="2">
<tr>
<?php
$showFoto = getFoto();
$i=0;
foreach($showFoto as $Foto){
++$i;
echo ($i%4==0) ? '</tr><tr>' :'';
echo '<td class="afs">
<img alt="" src="img/'.$Foto['img'].'.'.$Foto['ext'].'">'.$Foto['about'].
'</td>';
}
?>
</tr>
</table>
I'm trying to echo data from an SQLdatabase into a table that is somewhat decent-looking. I can already echo the data properly into 5 separate basic tables, but when I can't figure out how to style it without it completely messing up. Here is the code I already have:
// OUTPUTS RESULTS //
$resultfirst = mysql_query("SELECT * FROM Students WHERE FirstPeriod='$_POST[firstperiod]'");
$resultsecond = mysql_query("SELECT * FROM Students WHERE SecondPeriod='$_POST[secondperiod]'");
$resultthird = mysql_query("SELECT * FROM Students WHERE ThirdPeriod='$_POST[thirdperiod]'");
$resultfourth = mysql_query("SELECT * FROM Students WHERE FourthPeriod='$_POST[fourthperiod]'");
$resultfifth = mysql_query("SELECT * FROM Students WHERE FifthPeriod='$_POST[fifthperiod]'");
// 1st PERIOD
echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>First Period</th>
</tr>";
while($row = mysql_fetch_array($resultfirst))
{
echo "<tr>";
echo "<td>" . $row['StudentName'] . "</td>";
echo "</tr>";
}
echo "</table>";
// 2nd PERIOD
echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Second Period</th>
</tr>";
while($row = mysql_fetch_array($resultsecond))
{
echo "<tr>";
echo "<td>" . $row['StudentName'] . "</td>";
echo "</tr>";
}
echo "</table>";
// 3rd PERIOD
echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Third Period</th>
</tr>";
while($row = mysql_fetch_array($resultthird))
{
echo "<tr>";
echo "<td>" . $row['StudentName'] . "</td>";
echo "</tr>";
}
echo "</table>";
// 4th PERIOD
echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Fourth Period</th>
</tr>";
while($row = mysql_fetch_array($resultfourth))
{
echo "<tr>";
echo "<td>" . $row['StudentName'] . "</td>";
echo "</tr>";
}
echo "</table>";
// 5th PERIOD
echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Fifth Period</th>
</tr>";
while($row = mysql_fetch_array($resultfifth))
{
echo "<tr>";
echo "<td>" . $row['StudentName'] . "</td>";
echo "</tr>";
}
echo "</table>";
If the code above is unclear- my goal is to compare the first period teachers in which are stored in the database, and output the students names that share the same period/teacher.
This code works fine. But the tables look very bland. I would like to echo the data into a table such as this.
<table width="368" border="0" cellspacing="0" cellpadding="2" align="center" height="100">
<tr valign="middle">
<td bgcolor="#000066" width="120" height="20">
<div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web
Services</font></b></font> </div>
</td>
<td width="4" height="20"></td>
<td bgcolor="#990000" width="120" height="20">
<div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web
Products</font></b></font></div>
</td>
<td width="4" height="20"></td>
<td bgcolor="#C89800" width="120" height="20">
<div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web
Resources</font></b></font> </div>
</td>
</tr>
<tr valign="top">
<td bgcolor="#000066" width="120">
<table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
<tr bgcolor="#FFFFFF" valign="top">
<td>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Professional
and cost effective web design, development and promotion services
</font></p>
</td>
</tr>
</table>
</td>
<td width="4"></td>
<td bgcolor="#990000" width="120">
<table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
<tr bgcolor="#FFFFFF" valign="top">
<td>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Interactive
Web Products - Flash Survey, poll, Guest book, Instant Quote
</font></p>
</td>
</tr>
</table>
</td>
<td width="4"></td>
<td bgcolor="#C89800" width="120">
<table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
<tr bgcolor="#FFFFFF" valign="top">
<td>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Free
web resources - articles, tutorials, tips and tricks.
</font></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
If anyone can offer any suggestions of any sort- I would greatly appreciate it.
I see a few areas that can be improved:
As others have said, don't use tables to manage layout. However, tables are perfectly acceptable for displaying tabular data
You really want to use CSS to style your page, it's a lot easier to control your style and makes the markup easier to read. See this w3schools' artcile on CSS Tables
You can combine your multiple queries into one for better performance (though, that seems to be outside the scope of your question)
Here is an example markup and CSS for your table: http://jsfiddle.net/vxzJV/
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);
?>