PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
speedy33417
Forum Contributor
Posts: 128 Joined: Sun Jul 23, 2006 1:14 pm
Post
by speedy33417 » Sun Sep 17, 2006 11:39 am
I'm working on a photo album and I have a small problem with an if statement that use when I'm displaying the thumbs of my pics. There should be 4 pics in a row. My code works fine when I have this:
Code: Select all
<?php
$numcol = 1;
?>
blah blah
<div align="center">
<table border="0" width="648" cellpadding="5" cellspacing="2">
<tr>
<?php
while ($numcol < 5) {
echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">";
echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>";
echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>";
echo "</p></td>";
$numcol++;
}
?>
</tr>
</table>
</div>
This code will work as long as I have exactly 4 pics left to display. However when I add an if statement to see if I ran out out of pics to display is when my problem starts.
Here's the code:
Code: Select all
<?php
$numpicsleft=2;
$numcol = 1;
?>
blah blah
<div align="center">
<table border="0" width="648" cellpadding="5" cellspacing="2">
<tr>
<?php
while ($numcol < 5) {
if($numpicsleft > 1) {
echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">";
echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>";
echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>";
echo "</p></td>";
$numcol++;
$numpicsleft--;
} else {
echo "<td width=\"162\"></td>";
}
}
?>
</tr>
</table>
</div>
When I run the page it seems like it goes into an eternal loop or something. I just can't see why.
Thanks.
Li0rE
Forum Commoner
Posts: 41 Joined: Wed Jun 07, 2006 6:26 am
Post
by Li0rE » Sun Sep 17, 2006 11:45 am
ill show you how to solve your problem and im going to use a for loop rather than a while loop.
Code: Select all
<table><tr>
<?php
for($num=1; $num<count($photos); $num++)
{ ?>
<td>photo/style info whatever</td>
<?php
if($num==4)
{
echo('</tr><tr>');
}
}
?>
</table>
speedy33417
Forum Contributor
Posts: 128 Joined: Sun Jul 23, 2006 1:14 pm
Post
by speedy33417 » Sun Sep 17, 2006 12:08 pm
Thanks for your replay.
This is the way I try to display my pics:
1234
5678
9
I just started on the code and I'm only working on the first row with 4 pics (1234). I want to make this one work before adding another statement for the rest of the rows. When I display no picture I want to add an empty <td></td> to fill up the row to four <td>'s
That's all I'm trying to achieve
Your code works for a
1
2
3
4
display.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 17, 2006 12:12 pm
Read the first link in Useful Posts.
rami
Forum Contributor
Posts: 217 Joined: Thu Sep 15, 2005 8:55 am
Post
by rami » Sun Sep 17, 2006 12:21 pm
once may be one year back i also made a query for this and got the hint and i made this which is working till now
may be it will help you
as i have edited some details make sure that it may be missed some ' or ;
but the idea works
Code: Select all
require_once ('../../dbconnect.php');
$page_title = 'view';
$query = "SELECT * FROM tables order by property_id desc limit 6";
$result= mysql_query($query);
echo '<div align="left" class="headline"><Strong><br> Recently Added </a></strong></div>';
echo '<table border="0" width="100%" cellspacing="2" cellpadding="0">';
$i = 0;
$N = 2; //this is how many you want in row
while ( $row = mysql_fetch_array( $result ) ) {
if ( $i % $N == 0 ) {
echo '<tr>';
}
echo "<td bgcolor=\"#F9F9F9\" valign=\"top\"><table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolorlight=\"#DDDDDD\" bordercolordark=\"#FFFFFF\" bordercolor=\"#FFFFFF\">
<tr><td colspan=\"2\" align=\"center\">
</td></tr>";
echo ' <tr><td width="35%">Price</td><td>' . $row['price'] . '</td></tr>
</table></td>';
if ( $i % $N == $N-1 ) {
echo '</tr>';
}
++$i;
}
echo '</table>';
speedy33417
Forum Contributor
Posts: 128 Joined: Sun Jul 23, 2006 1:14 pm
Post
by speedy33417 » Sun Sep 17, 2006 12:46 pm
They're both great, but they're a little too complex for me. Especially feyd's example.
Is there anyway we could debug my very basic code, puh-please...
rami
Forum Contributor
Posts: 217 Joined: Thu Sep 15, 2005 8:55 am
Post
by rami » Sun Sep 17, 2006 1:29 pm
speedy33417 wrote: They're both great, but they're a little too complex for me. Especially feyd's example.
Is there anyway we could debug my very basic code, puh-please...
well make sure where should be queries and after that tr part and echo are just simple
edit it and if i can i will help
here is basic skeleton
Code: Select all
require_once ('../../dbconnect.php'); //connect to database file
$query = "SELECT * FROM tables order by property_id desc limit 6";//query to db
$result= mysql_query($query);
echo 'Recently Added ';
//here it starts
echo '<table border="0" width="100%" cellspacing="2" cellpadding="0">';
$i = 0;
$N = 2; //this is how many you want in 1 row it may be 3 ,4..
while ( $row = mysql_fetch_array( $result ) ) {
if ( $i % $N == 0 ) {
echo '<tr>';
}
echo "<td><table width="100%" border="1" cellpadding="2" cellspacing="0">
<tr>
<td>
</td></tr>";
echo ' <tr><td width="35%">Price</td><td>' . $row['price'] . '</td></tr>
</table></td>'; //this is display part
if ( $i % $N == $N-1 ) {
echo '</tr>';
}
++$i; //these three lines makes that break
}
echo '</table>';
so may be you can adjust your queires and displays there
hope helps
speedy33417
Forum Contributor
Posts: 128 Joined: Sun Jul 23, 2006 1:14 pm
Post
by speedy33417 » Sun Sep 17, 2006 2:43 pm
I'm not sure what I'm describing wrong, but I don't need a tr in my code. The part I need debuged is the code I have on top, but I'll post it again:
Code: Select all
<?php
$numpicsleft=2;
$numcol = 1;
?>
blah blah
<div align="center">
<table border="0" width="648" cellpadding="5" cellspacing="2">
<tr>
<?php
while ($numcol < 5) {
if($numpicsleft > 0) {
echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">";
echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>";
echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>";
echo "</p></td>";
$numcol++;
$numpicsleft--;
} else {
echo "<td width=\"162\"></td>";
}
}
?>
</tr>
</table>
</div>
Basically the idea is that there's 4 cells in the row. Ideally there's a pic and some description in each cell. That's when $numpicsleft=4. However if $numpicsleft=2, then only the first two cells are posting pictures, but the last two cells are still created only it's empty (<td></td>)
Please don't post totally new coding, because I'm new to php. I only intend to find out what I did wrong in mine.
Thanks again.