PHP: working with tr and td with while() - php

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while ($pF = mysql_fetch_array($stringVisits)) {
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if (checkStatus($BuID) == 1) {
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
echo $getByProfile["photo_thumb"];
} else {
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php } ?>
</tr>
</table>
This is what i have right now it displays the visiter´s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?

Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while($pF = mysql_fetch_array($stringVisits)){
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>
<?php } ?>
</tr>
</table>

Here's a way to do it with minimal changes to your existing code:
<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array(); // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){
$names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php }
// ADDITION #3:
echo "</tr>
<tr>";
foreach ($names as $username)
echo "<td class=\"username\"><p>$username</p></td>";
?>
</tr>
</table>
Also note that the way you had your code, the </tr> was inside the while loop.

this might not look nice but it should work why do you want 2 loops?
<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";
}
echo "</table>";
?>

Related

How do i put a <td> in my <table> with php?

I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!
First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';
There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>
There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.

CakePHP - send mails

I hav run into a problem - I am not that familiar with cakephp - but I have taken over a project (A webshop project) - and now I have a bit of a problem - I have a field (tracking id for the package) which writes to DB - that works great - but here is the trouble - when I click the button and save the tracking number to the DB - I wan't the system to send an email to the customer with the tracking id.
I can get the system to send a mail - but there is no tracking number on so I guess the email is send before the DB i updated.
Hope someone can point me in the right direction
public function send($order_id = null, $is_resolved = true)
{
if($this->request->is('post'))
{
$this->Order->id = $this->request->data['Order']['id'];
$this->Order->saveField('state', 'resolved');
$this->Order->saveField('date_resolved', date('Y-m-d H:i:s'));
$this->Order->saveField('delivery_label', $this->request->data['Order']['delivery_label']);
$this->Session->setFlash('Ordren er nu sat til "Sendt".'.$this->Session->read('Message.success.message'), null, array(), 'success');
$this->redirect(array('controller' => 'orders', 'action' => 'index'));
}
$order = $this->Order->find('first', array('conditions' => array('Order.id' => $order_id, 'Order.state' => 'packed'), 'recursive' => 4));
// Does the item exist?
if(empty($order))
{
// Tell the user this item did not exist
$this->Session->setFlash('Ordren findes ikke.'.$this->Session->read('Message.warning.message'), null, array(), 'warning');
return $this->redirect($this->referer());
}
if($is_resolved) {
// Send the email receipt
$Email = new CakeEmail('receipt');
$Email->to($order['Customer']['email_address']);
$Email->template('receipt_send')->viewVars( array('order' => $order));
$Email->send();
}
$this->set('order', $order);
}
Here is the receipt_send code
<table style="width:100%; margin-bottom:30px; border-collapse: collapse; color:#777;">
<tr style="vertical-align:middle; border-bottom:1px solid #777;">
<td colspan="3" style="width:45%; padding:10px 0;"><?php echo $this->Html->image('logo.png', array('style' => 'height:50px; margin-top:-15px; vertical-align:middle;', 'fullBase' => true)); ?></td>
</tr>
<tr style="font-size:1em; border-bottom:1px solid #777;">
<td style="padding: 5px 0;"><strong>Kvittering</strong> fra Bundgaards Garn</td>
<td style="padding: 5px 0;">Ordre nr. <strong><?php echo $order['Order']['id']; ?></strong></td>
</tr>
<p style="width:50%; float:right;">Din ordre er afsendt og har fået trackingnummer: <strong><?php echo $order['Order']['delivery_label']; ?></strong></p>
<table style="text-align: left; width:100%; border-collapse: collapse;">
<tr style="border-bottom:2px solid #dddddd;">
<th style="width:35%; padding: 10px 0;">Vare</th>
<th style="width:25%; padding: 10px 0;">Antal</th>
<th style="width:20%; padding: 10px 0; text-align:right;">Pris</th>
<th style="width:20%; padding: 10px 0; text-align:right;">Total</th>
</tr>
<?php foreach ($order['OrderItem'] as $key => $order_item): ?>
<tr style="border-bottom:1px solid #dddddd;">
<!-- PRODCUT LINK -->
<td style="padding: 10px 0; vertical-align: middle;">
<?php if($order_item['yarn_batch_id'] != 0) : ?>
<?php echo $order_item['YarnBatch']['YarnVariant']['Yarn']['name'] . ' - ' . $order_item['YarnBatch']['YarnVariant']['color_code'] . '-' . $order_item['YarnBatch']['batch_code']; ?>
<?php elseif($order_item['needle_variant_id'] != 0) : ?>
<?php echo $order_item['NeedleVariant']['Needle']['name'] . ' - ' . $order_item['NeedleVariant']['product_code']; ?>
<?php elseif($order_item['recipe_id'] != 0) : ?>
Print af <?php echo $order_item['Recipe']['name']; ?>
<?php elseif($order_item['color_sample_id'] != 0) : ?>
Farveprøve af <?php echo $order_item['ColorSample']['name']; ?>
<?php endif; ?>
</td>
<!-- PRODCUT LINK COLLAPSE -->
<td style="padding: 10px 0; vertical-align: middle;">
<?php echo $order_item['amount']; ?>
</td>
<td style="padding: 10px 0; vertical-align: middle; text-align:right;">
<?php if($order_item['yarn_batch_id'] != 0) : ?>
<?php echo $this->Number->currency($order_item['YarnBatch']['price'], 'DKK');?>
<?php elseif($order_item['needle_variant_id'] != 0) : ?>
<?php echo $this->Number->currency($order_item['NeedleVariant']['price'], 'DKK');?>
<?php elseif($order_item['recipe_id'] != 0) : ?>
<?php echo $this->Number->currency($order_item['Recipe']['price'], 'DKK');?>
<?php elseif($order_item['color_sample_id'] != 0) : ?>
<?php echo $this->Number->currency($order_item['ColorSample']['price'], 'DKK');?>
<?php endif; ?>
</td>
<td style="padding: 10px 0; vertical-align: middle; text-align:right;" >
<?php echo $this->Number->currency($order_item['price'], 'DKK');?>
<?php if($order_item['saving'] > 0) : ?>
<br/>
<span style="color:#5cb85c;"><?php echo $this->Number->currency($order_item['saving'], 'DKK');?>´</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="2"/>
<td style="text-align:right;">
<p>Levering</p>
<?php if($order['Order']['saving'] > 0) : ?>
<p>Rabat</p>
<?php endif; ?>
<p><strong>Subtotal</strong></p>
<p><strong>Moms</strong></p>
</td>
<td style="text-align:right;">
<p><?php echo $this->Number->currency($order['Order']['shipping_price'], 'DKK');?></p>
<?php if($order['Order']['saving'] > 0) : ?>
<p style="color:#5cb85c;"><strong><?php echo $this->Number->currency($order['Order']['saving'], 'DKK');?></strong></p>
<?php endif; ?>
<p><strong><?php echo $this->Number->currency($order['Order']['sub_total'], 'DKK');?></strong></p>
<p><strong><?php echo $this->Number->currency($order['Order']['tax'], 'DKK');?></strong></p>
</td>
</tr>
<tr>
<td colspan="2"/>
<td style="text-align:right;">
<h4>Total:</h4>
</td>
<td style="text-align:right; color:#900000;">
<h4><strong><?php echo $this->Number->currency($order['Order']['price'], 'DKK');?></strong></h4>
</td>
</tr>
</table>
<div style="width:50%; float:left;">
<h4>Ordren sendes til:</h4>
<p>
<strong><?php echo $order['Customer']['first_name'] . ' ' . $order['Customer']['last_name']; ?></strong><br>
<?php echo $order['Customer']['ShippingAddress']['street'] . ' ' .
$order['Customer']['ShippingAddress']['zip_code'] . ' ' .
$order['Customer']['ShippingAddress']['city_name']?><br>
<?php echo $order['Customer']['email_address']; ?><br>
<?php echo $order['Customer']['phone_number']; ?><br>
</p>
</div>
<div style="width:50%; float:left;">
<?php if(!empty($order['Order']['customer_note'])) : ?>
<h4>Note til forhandleren:</h4>
<p>
<?php echo $order['Order']['customer_note']; ?>
</p>
<?php endif; ?>
</div>
<hr style="width:100%; border: 0; border-bottom: 1px solid #ddd;">
<p style="color:#999; font-size:0.8em;">
<strong>Bundggards Garn</strong> <br/>
<a style="color:#777;" href="https://bundgaardsgarn.dk">bundgaardsgarn.dk</a> <br/>
kontakt#bundgaardsgarn.dk <br/>
Saltumvej 46 9700 Brønderslev <br/>
30369522 <br/>
<i>CVR:</i> 34526826 <br/>
</p>
Got it to work by creating a controller and make a button to the information.... Works like a charm.

Next and Previous Buttons with php

I have been having a problem getting next and previous buttons to work using a php webpage connected to mysql database. I am not sure what is wrong but i think it may have something to do with the offset. Right now the button are not working at all you click them and nothing happens. Also the form is made as a search page.
Here is the search code:
<?php
include("../web-admin/dbconfiginc.php");
$result = mysql_query("SELECT * FROM videos WHERE ID = '$ID'");
$row = mysql_fetch_array($result);
?><strong></strong>
<title>Search </title>
</head>
<body>
<table align="center" width="1024" border="0">
<tr>
<td valign="top" align="center" width="70%" style="padding-top:10px;" class="lhc">
<div align="center" style="margin-top: 50px; padding-bottom: 50px;">
<?php
$Keyword = $_POST['Keyword'];
$GuestName = $_POST['GuestName'];
$Day = $_POST['Day'];
$Month = $_POST['Month'];
$Year = $_POST['Year'];
if(isset($cancel))
{
header("Location:index.php");
exit;
}
$qry_string = "SELECT * FROM videos ";
$search = "";
if(!empty($Keyword))
{
$End_String = "(Blurb LIKE '%$Keyword%' OR Title LIKE '%$Keyword%')";
$search .="&Keyword=$Keyword";
}
if(!empty($GuestName))
{
if(isset($End_String))
{
$End_String .= " AND (GuestName LIKE '%$GuestName%')";
}
else
{
$End_String = "(GuestName LIKE '%$GuestName%')";
}
$search .="&GuestName=$GuestName";
}
if(!empty($Day))
{
if(isset($End_String))
{
$End_String .= " AND (DAYOFMONTH(Publish_Date) = '$Day')";
}
else
{
$End_String = "(DAYOFMONTH(Publish_Date) = '$Day')";
}
$search .="&Day=$Day";
}
if(!empty($Month))
{
if(isset($End_String))
{
$End_String .= " AND (MONTH(Publish_Date) = '$Month')";
}
else
{
$End_String = "(MONTH(Publish_Date) = '$Month')";
}
$search .="&Month=$Month";
}
if(!empty($Year))
{
if(isset($End_String))
{
$End_String .= " AND (YEAR(Publish_Date) = '$Year')";
}
else
{
$End_String = "(YEAR(Publish_Date) = '$Year')";
}
$search .="&Year=$Year";
}
if(!empty($Active))
{
if(isset($End_String))
{
$End_String .= " AND (GuestName LIKE '%$GuestName%')";
}
else
{
$End_String = "(GuestName LIKE '%$GuestName%')";
}
$search .="&GuestName=$GuestName";
}
if (!isset($offset)) $offset=0;
if(isset($End_String))
{
$qry_string = $qry_string." WHERE ".$End_String . "ORDER BY Publish_Date DESC LIMIT $offset, 101";
}
else
{
$qry_string = $qry_string."ORDER BY Publish_Date DESC LIMIT $offset, 101";
}
$result = mysql_query($qry_string);
echo mysql_error();
?>
And here is the code that displays the results from the database
<?php
$num_records = mysql_num_rows($result);
if (!isset($search))
$search = "";
?>
<table width="100%" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0"><tr><td>
</td></tr></table>
<table width="95%" align="center" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td width=25%>
<?php
if ($offset > 99) {
echo '<a href='.$_SERVER['PHP_SELF'].'?$offset='.($offset-100).'>Previous 100</a>';
} else {
echo 'Previous 100';
}
?>
</td>
<td align=center width=10%>
<span class="content1"><a href=search.php>Search </a></span>
</td>
<td align=right width=25%>
<?php
if($num_records == 101) {
echo 'Next 100';
} else {
echo 'Next 100';
}
?>
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="3" cellspacing="1" bgcolor="#666666" style="margin: 5px 15px; 5px 10px;">
<tr style="background: #9ae6f1; font-family: Verdana; font-weight: bold; font-size: 18px;">
<td style="width: 65%; padding: 5px;">
Video
</td>
<td align="center" style="width: 10%; padding: 5px;">
Guest Name
</td>
<td align="center" style="width: 10%; padding: 5px;">
Date
</td>
</tr>
<?php
$count = 1;
$bgc = 1;
while($row = mysql_fetch_array($result))
{
if ($count > 100) break;
echo '<tr style="background: ';
if (($bgc == 0 ? $bgc=1 : $bgc=0) == 0) echo "#FFFFFF";
else echo "#E6E6E6";
echo ';">';
echo '<td><div style="padding: 3px;" class="content"><a href=../../watch/watch.php?ID='.$row[ID].'>'.$row[Title].'</a></div></td>';
echo '<td><div style="padding: 3px;" class="content">'.$row[GuestName].'</div></td>';
echo '<td><div align="center" style="padding: 3px;" class="content">'.$row[Publish_Date].'</div></td></tr>';
$count++;
}
?>
</table>
<table width="95%" align="center" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td width=25%>
<?php
if($offset > 99) {
echo '<span class="content"><a href='.$_SERVER['PHP_SELF'].'?offset='.($offset-100).$search.'>Previous 100</a></span>';
} else {
echo 'Previous 100';
}
?>
</td>
<td align=center width=10%>
<span class="content1"><a href=search.php>Search </a></span>
</td>
<td align=right width=25%>
<?php
if($num_records == 101) {
echo '<span class="content"><a href='.$_SERVER['PHP_SELF'].'?offset='.($offset+100).$search.'>Next 100</a></span>';
} else {
echo 'Next 100';
}
?>
</td>
</tr>
</table>
Would really appreciate some help trying to figure this out or some points in the right direction. Thank you!

how to select and echo an image from mysql

I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>

How to restrict number of images in a row to three?

I am populating the images from my database in a table, how to restrict the images to three per row?
<table border="0" align="center" height="25%" width="25%" >
<tr><td align="center" width="50px" bgcolor="#4b2d0e"><strong><font color="#FFFFFF">Friend List</font></strong></td></tr>
<? foreach($Selected as $row)
{
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
?>
<tr><td bgcolor="#999999">
<strong ><?=$row['dFrindName'].'</br>';?></strong>
<? if($value!="") { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
<? } else { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
<? }?>
</td></tr>
<? }}?>
</table>
This is my table
<?php
$x=0;
foreach($Selected as $row){
if($x%3 == 0)
echo '<tr>';
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
?>
<td style="background-color:#999999;">
<strong ><?=$row['dFrindName'].'</br>';?></strong>
<?php if($value!="") {?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
<?php } else { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
<?php }?>
</td>
<?php
if($x%3 == 0)
echo '</tr>';
x++;
}
if(($x-1)%3 != 0)
echo '</tr>'; //Prints out the last '<tr>' if one isn't printed.
?>
You need to use an if with a modulus operator.
Here, I cleaned up your invalid HTML, used CSS, and used a more recommended PHP coding style.
Please note: you need to be aware that if $Selected contains user-inputted (or otherwise non-HTML-safe) data, you need to wrap your output in htmlspecialchars or be open to XSS vulnerabilities.
It was a little unclear what you meant by wanting to "restrict the images to three per row" considering that it was only showing 1 per row currently. If I am to assume that you want to show 3 per row rather than 1, than you need to use the modulus operator and only open a new <tr> after every third element, and then close it at the right time. Like this:
<style type="text/css">
a img { border: none; }
.friend-list { border: none; width: 25%; height: 25%; margin: 0 auto; }
.friend-list th { text-align: center; background-color: #4b2d0e; color: #fff; font-weight: bold; }
.friend-list td { background-color: #999999; }
</style>
<?php
$numCols = 3;
$colCount = -1;
?>
<table class="friend-list">
<tr>
<th colspan="<?php echo $numCols; ?>">Friend List</th>
</tr>
<?php
foreach($Selected as $row) {
$value = $row['dPath'];
$imgp = ($value) ? base_url().'images/'.$value : '../images/us.png';
if(++$colCount % $numCols == 0) {
echo '<tr>';
}
?>
<td>
<strong><?php echo $row['dFriendName']; ?></strong><br />
<a class="Tab_Link" href="<?php echo site_url(); ?>/friends/View_FProfile/<?php echo $row['dMember_Id']; ?>">
<img src="<?php echo $imgp; ?>" width="90" height="80" />
</a>
</td>
<?php
if(($colCount + 1) % $numCols == 0) {
echo '</tr>';
} elseif (($colCount + 1) == count($Selected)) {
// if 16 elements are to fit in 3 columns, print 2 empty <td>s before closing <tr>
$extraTDs = $numCols - (($colCount + 1) % $numCols);
for ($i = 0; $i < $extraTDs; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
}
?>
</table>
Tables should be reserved for situations where columns and rows have meaning... A better solution is to use floated block elements instead of table cells. When you float a bunch of similar blocks, they wrap automatically, so the key is making their parent container just wide enough to hold 3 of them.
You don't need to do anything special with php to create new rows, so i'll just display the html and css, letting you write the php to make it happen.
html:
<div id="replacesTable">
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="clear"> </div>
</div>
css:
#replacesTable{
width: 300px;
}
div.replacesTableCell{
float:left;
width: 100px;
/* styles below are just to make it easier to see what's happening */
text-align:center;
font-size: 10px;
margin: 20px 0;
}
/* this just stretches the parent container #replacesTable around the entries*/
.clear{
clear:both;
height:1px;
overflow:hidden;
}
You can use CSS as an alernative if the images are of a fixed width and you can do without the tables - create a wrapper div with a fixed width around your entire image list, and simply float each image left.
Here is a simplified example without the extraneous styling information to show the principal. Every 3rd image we want to write the opening and closing tags (though not at the same time).
So we loop through the list of images and use the moduulus operator to know when we should print the <tr> tags.
<?php
$column_count = 3;
$image_list = get_images();
?>
<table>
<?php
for($i=0; $i < sizeof($image_list); i++) {
$cur_img = $image_list[$i];
$img_url = $cur_img['url'];
// open a we row every 3rd column
if($i % $column_count == 0) {
print '<tr>';
}
// for every image we want a table cell, and an image tag
print "<td> <img src='$img_url'> </td>";
// close the row every 3rd column, but offset by 3 from the opening tag
if($i % $column_count == $column_count - 1) {
print '<tr>';
}
}
// what if the number of images are not div by 3? Then there will be less
// than 3 items in the last row, and no closing tag. So we look for that and
// print a closing tag here if needed
if(sizeof($image_list) % $column_count != 0) {
print '</tr>';
}
?>
</table>
You can try using http://www.php.net/manual/en/function.array-chunk.php
Hey, would you try this.
Notice that I replaced the if...else with a ternary operator. I prefer it compact.
Hope it helps you and anyone else interested.:)
<table border="0" align="center" height="25%" width="25%" >
<tr>
<td colspan="3" align="center" width="50px" bgcolor="#4b2d0e">
<strong><font color="#FFFFFF">Friend List</font></strong>
</td>
</tr>
<?
$imgs=0;
foreach($Selected as $row){
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
if($imgs%3 == 0){
echo '<tr>';
}
?>
<td bgcolor="#999999">
<strong ><?=$row['dFrindName'].'</br>';?></strong><a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$value!=""? $imgp: '../images/us.png' ?>" name="b1" width="90" height="80" border="0"/></a>
</td>
<?
$imgs++;
if($imgs%3 == 0){
echo "</tr>\n";
}
}//end loop
echo '</tr>';//end last row
?>
</table>

Categories