print php result in a nice html table - php

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>

Related

PHP Nesting do-while loop

Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.
I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<tr>
<td width="14">
<td width="313"><?php echo $row_RegionList['details']; ?> </tr>
<?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.
This is my attempt;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php $details = $row_RegionList['details'];
do { ?>
<tr>
<td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<?php } while ($row_RegionList['details'] == $details);
$details = $row_RegionList['details'];
} while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?

While loop to get PHP data into two different tables in HTML

Using a while loop to get my data out from PHP will only show all my data is vertical in a table form.
How can I show the data in horizontal form:
<?php
while ($display = mysqli_fetch_array($link)) {
if ($display['see_id'] % 2 == 0) {
?>
<table border='0' width='550px'>
<tr height='50px' style="text-align:center">
<td><?php echo $display['name']; ?></td>
</tr>
<tr>
<td><?php echo $display['video']; ?></td>
</tr>
<tr height='50px'>
<td><?php echo $display['description']; ?></td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>
<?php }}
?>
You can put some style in your table: style="width:50%; float:left"
With while loop, of course.
<table border='0' style="width:50%; float:left">
<tr height='50px' style="text-align:center">
<td>Name</td>
</tr>
<tr>
<td>Video</td>
</tr>
<tr height='50px'>
<td>Description</td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>
<table border='0' style="width:50%; float:left">
<tr height='50px' style="text-align:center">
<td>Name</td>
</tr>
<tr>
<td>Video</td>
</tr>
<tr height='50px'>
<td>Description</td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>

Show Data Horizontally in Table using PHP & MySQL

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

store part of the page in variable

i have a page contains a table and this table contains a lot of coding like this
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
<? $res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){ ?>
<tr style="width:700px">
<td class="tabletext"><?= ++$serial;?></td>
<td class="tabletext" ><?= $resArr[stName];?></td>
<td class="tabletext"><?= $resArr['date'];?></td>
<td class="tabletext" ><?= $resArr[matName];?></td>
<td class="tabletext" ><? if($resArr[exam]==1) echo "work";else echo "final";?></td>
<td class="tabletext" ><? if($resArr[exam_type]==1) echo "prac";else echo "test";?></td>
</tr>
<? }?>
</tbody>
</table>
as you see the table has php coding
now i want to store the whole table in variable so i can send it to pdf printing library tcpdf
You can use heredoc syntax, but you need to move the conditionals outside:
<?php
if($resArr['exam']==1) $exam = "work"; else $exam = "final";
if($resArr["exam_type"]==1) $examtype = "prac";else $examtype = "test";
$var = <<<EOT
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
$res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){
<tr style="width:700px">
<td class="tabletext">{++$serial}</td>
<td class="tabletext" >{$resArr["stName"]}</td>
<td class="tabletext">{$resArr["date"]}</td>
<td class="tabletext" >{$resArr["matName"]}</td>
<td class="tabletext" >{$exam}</td>
<td class="tabletext" >{$examtype}</td>
</tr>
<? }?>
</tbody>
</table>
EOT;
echo $var;
Another way, if you already have all of this code and want to save it:
Before the table:
<?php ob_start(); ?>
After the table:
<?php $output = ob_get_contents(); ?>
The table will still be displayed and you can use $output to send to the PDF.

Adding PHP alternate table row colours to existing HTML table

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;
}

Categories