...

View Full Version : Limiting the creation of <td> entries in a while loop



jpmad4it
03-05-2008, 12:13 PM
Hi there

I have the following code:


<?
//online? if so display. If not, message.
$online_sql=mysql_query("MY QUERY HERE");
?>
<table width="100&#37;" border="0" cellpadding="0" cellspacing="0" class="border"><tr><td height="20" align="center" bgcolor="#D83C85"><strong>Online / Available NOW!</strong></td></tr></table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="border"><tr>
<?
while ($online = mysql_fetch_array($online_sql)) {
if(isset($online_sql)){
$is_online='<td align="center" style=" font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#FFFFFF;"><br />
<a href="'.processLink($online["product_id"], $online["name"]).'"><img border=0 src="makethumb.php?pic=product_images/'.$online["filename"].'&amp;w=145&amp;sq=N" /><p align="center" style=" padding-bottom:5px; padding-top:5px; color:#FFFFFF;"><strong>'.$online["name"].'</strong></p></a></td>';
print $is_online;
} else {
$is_online ='<td align="center" style=" font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#FFFFFF;">Sorry, no-one is currently online</td>';
print $is_online;
}
}
?>
</tr></table><br />
<?

I have a few problems here.

1) If there are people online (if $online_sql holds a value), I want to limit the creation of the table cells <td>'s to 3, and then if there are more than 3 people online, I want to create a new table row <tr>. And continue this process until all database entries are gathered. If that makes sense? So if there are 9 people online there would be 3 table rows each with 3 table cells in them. Can that be done? Then possibly in the future I could create pages after the number of results hit a certain value (but not yet :rolleyes:).

2)The } else { statement doesnt seem to be working. If no-one is online I want the message to be produced, but at the moment nothing is being displayed? Can I use an if-else statement within the while loop??

Any help will be much appreciated!

Kind regards
Jp

abduraooft
03-05-2008, 12:30 PM
PHP Formatting Data from mySQL (http://codingforums.com/showthread.php?t=134416)


Change if(isset($online_sql)) to

if(mysql_num_rows($online_sql))

jpmad4it
03-05-2008, 01:18 PM
PHP Formatting Data from mySQL (http://codingforums.com/showthread.php?t=134416)


Change if(isset($online_sql)) to

if(mysql_num_rows($online_sql))

hmmmm, no that didn't work either.

I've found some nifty code from webmonkey. Its allowing me to limit the number of photos to 3 pictures per row. I have 4 entries in the database and they are being displayed correctly, but my problem is that the while loop is throwing out the first entry of the database so I am getting 4 duplicate pictures, instead of the 2nd, 3rd, and 4th picture. Here is the code:


<?
//online?
$online_sql=mysql_query("MY QUERY HERE");
?>
<table width="100&#37;" border="0" cellpadding="0" cellspacing="0" class="border"><tr><td height="20" align="center" bgcolor="#D83C85"><strong>Online / Available NOW!</strong></td></tr></table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="border">
<?
$num_rows = 3;
$photos_per_row = 3;
$photos_per_page = $num_rows * $photos_per_row;
//check to see if the start variable exists in the URL.
//If not, then the user is on the first page - set start to 0
if(!isset($start)){
$start = 0;
}

//init i to where it needs to start in the photos array
$i = $start;
$prev_start = $start - $photos_per_page;
$next_start = $start + $photos_per_page;
$total_photos = mysql_num_rows($online_sql);

while ($online = mysql_fetch_array($online_sql)) {
$is_online='<td align="center" style=" font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#FFFFFF;"><br /><a href="'.processLink($online["product_id"], $online["name"]).'"><img border=0 src="makethumb.php?pic=product_images/'.$online["filename"].'&amp;w=145&amp;sq=N" /><p align="center" style=" padding-bottom:5px; padding-top:5px; color:#FFFFFF;"><strong>'.$online["name"].'</strong></p></a></td>';
for ($row=0; $row < $num_rows; $row++){
print("<tr>\n");
for ($col=0; $col < $photos_per_row; $col++){
if($i < $total_photos){
print $is_online;
} else {
print("<td></td>\n");
}
$i++;
}
print("</tr>\n");
}
}
//end table
?>
</table>

I don't know where else to place the while loop, or if there is anyway around this?

To be more relevant, its the position editing of this code that I think will solve the problem?:


while ($online = mysql_fetch_array($online_sql)) {
$is_online='<td align="center" style=" font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#FFFFFF;"><br /><a href="'.processLink($online["product_id"], $online["name"]).'"><img border=0 src="makethumb.php?pic=product_images/'.$online["filename"].'&amp;w=145&amp;sq=N" /><p align="center" style=" padding-bottom:5px; padding-top:5px; color:#FFFFFF;"><strong>'.$online["name"].'</strong></p></a></td>';
print $is_online;
}


Please help!

regards
Jp

abduraooft
03-05-2008, 01:33 PM
Have you checked that link (a previous thread in this forum)?

jpmad4it
03-05-2008, 01:47 PM
Have you checked that link (a previous thread in this forum)?

Ah i didnt see that. That fixed the issue perfectly :thumbsup:, but now I have moved onto the other code. How could I implement that code into this :confused::confused:

Could anyone give me some pointers?

jpmad4it
03-05-2008, 01:56 PM
Actually, thinking about it I dont need that now do i!!!!

Thanks!

There is another problem I have though. These images are created on the index.php page, where this code is also located:


if (!isset($_GET["id"])) $_GET["id"]='';
if ($_GET["id"]!=''){
include "profile.php";
} else {
$engine->load();
}

What this does is load the profile for the person depending which image is clicked upon. But the profile is being loaded directly below the images. What I want to do is clear the images from the page and load the profile "cleanly" on its own. Can that be done?

abduraooft
03-05-2008, 02:34 PM
<table width="535">
<tr>
<?php

//SQL Connection...

$result = $online_sql=mysql_query("Your QUERY HERE") or die(mysql_error());
$i = 0;
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
if(($i % 3) == 0 && $i != 0)
{
echo "</tr>\n<tr>\n";
}
echo '<td>
<a href="'.processLink($online["product_id"], $online["name"]).'">';
echo "<img border=0 src=\"makethumb.php?pic=product_images/".$online["filename"]."&amp;w=145&amp;sq=N\" />";
echo '</a></td>';
$i++;
}
}
else
{
echo "<td>Sorry, no-one is currently online</td>\n";
}
?>

</tr></table>

jpmad4it
03-05-2008, 03:07 PM
<table width="535">
<tr>
<?php

//SQL Connection...

$result = $online_sql=mysql_query("Your QUERY HERE") or die(mysql_error());
$i = 0;
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
if(($i % 3) == 0 && $i != 0)
{
echo "</tr>\n<tr>\n";
}
echo '<td>
<a href="'.processLink($online["product_id"], $online["name"]).'">';
echo "<img border=0 src=\"makethumb.php?pic=product_images/".$online["filename"]."&amp;w=145&amp;sq=N\" />";
echo '</a></td>';
$i++;
}
}
else
{
echo "<td>Sorry, no-one is currently online</td>\n";
}
?>

</tr></table>

Will that refresh the page? I'm not sure how that works, but it didnt seem to do anything?

jpmad4it
03-05-2008, 06:35 PM
I didnt really explain properly.

On the index.php file I have all my code which gets the images and links. Then directly below that I have the code:


if (!isset($_GET["id"])) $_GET["id"]='';
if ($_GET["id"]!=''){
require "profile.php";
} else {
$engine->load();
}

Once an image is clicked it brings up the profile for that certain image/person. This is done by $engine->load(); in the above code, which calls a module and essentially includes a PHP file (which holds the profile for the person). The problem is that the profile is being loaded directly into the index.php file underneath all the images. What I need to do is try to clear the images first, and then load the profile, all within the index.php page, or on a separate page.

I tried to add some kind of page counter, but realised that wouldn't work. Could I not add the above code to the link that is processed using 'onclick' and call a separate file called profile.php? My problem is getting the data into a link format.

Or does anyone have any better ideas? I'm sure there must be something else I can do, for example, is there no function to load a php file in the else statement, or redirect to another file?

regards
Jp



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum