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);
?>
Related
I creating a PDF for print the address of users. I have the below HTML structure:
I have tried with the below code:
<?php $i=0; ?>
<tr valign="top">
foreach ($users as $user): ?>
<?php if ($i % 2 == 1) {?>
<tr valign="top">
<?php } ?>
But it's now work as expect.
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
<tr valign="top">
<td width="48%" align="left" valign="top" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%" height="25" valign="bottom">Name</td>
<td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom"> </td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom">Phone</td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
</table></td>
<td width="4%" align="left"> </td>
<td width="48%" align="left" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%" height="25" valign="bottom">Name</td>
<td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom"> </td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom">Phone</td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
</table></td>
</tr>
<tr valign="top">
<td height="15" align="left"></td>
<td height="15" align="left"></td>
<td height="15" align="left"></td>
</tr>
</table>
In the abolve table I want to repeat the TD in each TR 2 times and added the blank TR after every TR.
The above HTML out should be the below image:
Could any one can help me out this.
If I get properly what you're looking for : I want to repeat in one TR repeat two TD
Then this code:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
echo"<table>";
for ($i = 1; $i <= 10; $i++) {
if(($i % 2) == 1) // odd -> start TR
{ echo "<tr><td class=\"dark\">$i</td>"; }
else // even -> close TR
{ echo "<td class=\"red\">$i</td></tr><tr><td colspan=\"2\">whatever here</tr>"; }
}
echo"</table>";
?>
will give you this output :
<table>
<tr><td class="dark">1</td><td class="red">2</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">3</td><td class="red">4</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">5</td><td class="red">6</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">7</td><td class="red">8</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">9</td><td class="red">10</td></tr><tr><td colspan="2">whatever here</tr>
</table>
EDIT after you comment : added one 'blank' TR
Try below solution you missed out to close
<?php
$i=0;
foreach ($users as $user){
if ($i % 2 == 0) {?>
<tr valign="top">
<?php } ?>
<td width="48%" align="left" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%" height="25" valign="bottom">Name</td>
<td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom"> </td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
<tr>
<td height="40" valign="bottom">Phone</td>
<td valign="bottom" style="border-bottom:1px dashed #ccc;"> </td>
</tr>
</table></td>
<?php if ($i % 2 == 0) {?>
</tr>
<?php } }?>
this code not work in table with mPDF php class
<table>
<tr>
<td class="contentDetails">
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</table>
i triad use
.contentDetails > h3 {display: block;}
but not work and between td it's html from Editor tinymce
this full code from script
and when do output found content the td text align left not right or center
<?php
$html = '
<h1>mPDF</h1>
<table style="border-collapse: collapse;
font-size: 12px;
font-weight: 700;
margin-top: 5px;
border-top: 1px solid #777;
width: 100%;">
<tbody>
<tr>
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</tbody>
</table>';
include("mpdf.php");
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
Try the below code. I think it will help you.
<table width="100%">
<tr>
<td class="contentDetails">
<th align="left"> <h3><strong>text align right</strong></h3></th>
<th align="center"> <h3><strong>text align center</strong></h3></th>
<th align="right"> <h3><strong>text align left</strong></h3></th>
</td>
</tr>
</table>
using extra tags in this.
I have prepared a lot of hours a content to print (mPdf v.6). Now i can give an advice to all. Don't use tables to align content or simply hold the content. Almost all css properties of elements work fine. But - but must not be nested in table.
The problem is the width of the container which is fitting with the content. Set table width to 100%
table {
width: 100%;
}
<table>
<tr>
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</table>
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
Why do the borders of my tables not appear when I print my pdf to the screen?
I'm using the mPDF library.
I create the tables in html and for some reason it is not showing.
I already tried using inline CSS but it didn't work, for me.
<?php
require_once 'MPDF57\MPDF57\mpdf.php';
class TablePDF {
public function printPDF() {
$mpdf = new mPDF();
$border = 2 ;
$mpdf->SetHeader("Tabela");
$mpdf->SetFooter("Tabela");
$htmlpdf= "
<html>
<head>
<link type='text/css' rel=' rel='heet' href=' href='tstrap.min.css'>
<link type='text/css' rel='stylesheet' href='css/estilo.css'>
<style type='.text/css.'>
<title>Table</title>
</style>
.td{
style='border: 1px solid';
}
</head>
<body>
<table class='tabela' border=".$border." style='border:10px solid;'>
<tr style='border:10px solid black'>
<th width='150' style='border:10px solid black; '>GE</th>
<td width='200' style='border:10px solid black; '>0</td>
<td width='200' style='border:10px solid black; '><strong>Nome Curto</strong></td>
<td width='200' style='border:10px solid black; '>0</td>
</tr>
</table>
<table class='tabela' border=".$border." style='border:10px solid black'>
<tr>
<th width='150'>Valor</th>
<td width='200'>0</td>
<td width='200'><strong>FG</strong></td>
<td width='200'>0</td>
</tr>
</table>
<table class='tabela' border=".$border." style='border:10px solid black'>
<tr>
<th width='150'> Jugular </th>
<td width='117'>0</td>
<td width='117'>0</td>
<td width='117'>0</td>
<td width='118'>0</td>
<td width='119'>0</td>
</tr>
</table>
<table class='tabela' border=".$border." style='border:10px solid black'>
<tr>
<th width='150'>EXP3D</th>
<td width='200'>0</td>
<td width='200'>0</td>
<td width='200'>0</td>
</tr>
</table>
<table class='tabela' border=".$border." style='border:10px solid black'>
<tr>
<th width='150'>EXP28D</th>
<td width='200'>0</td>
<td width='200'>0</td>
<td width='200'>0</td>
</tr>
</table>
<table class='tabela' border=".$border." style='border:10px solid black'>
<tr>
<th width='150'>Carotida</th>
<td width='303'>0</td>
<td width='303'>0</td>
</tr>
</table>
</body>
</html>";
$mpdf -> useOnlyCoreFonts = true;
$mpdf->WriteHTML($htmlpdf);
$mpdf->Output();
}
}
?>
Then I use this to use this function to print the pdf:
<?php
require_once 'TablePDF.php';
$var = new TablePDF();
$var->printPDF();
?>
EDIT: the problem has been solved, i just started deleting the table class in the TABLE tag line because for some reason it wastn recognizing it as a class.
thanks alot folks.
td {
border: 1px solid;
}
You don't have to create style every time that you need to use a td in your page, you only call the style.
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;
}