Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2008, 11:13 AM   PM User | #1
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
Limiting the creation of <td> entries in a while loop

Hi there

I have the following code:

PHP Code:
<? 
//online? if so display. If not, message.
$online_sql=mysql_query("MY QUERY HERE");
?>
<table width="100%" 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 ).

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
jpmad4it is offline   Reply With Quote
Old 03-05-2008, 11:30 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
PHP Formatting Data from mySQL


Change if(isset($online_sql)) to
PHP Code:
if(mysql_num_rows($online_sql)) 
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 03-05-2008 at 11:35 AM..
abduraooft is offline   Reply With Quote
Old 03-05-2008, 12:18 PM   PM User | #3
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
PHP Formatting Data from mySQL


Change if(isset($online_sql)) to
PHP Code:
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:

PHP Code:
<? 
//online?
$online_sql=mysql_query("MY QUERY HERE");
?>
<table width="100%" 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?:

PHP Code:
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
jpmad4it is offline   Reply With Quote
Old 03-05-2008, 12:33 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Have you checked that link (a previous thread in this forum)?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-05-2008, 12:47 PM   PM User | #5
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
Have you checked that link (a previous thread in this forum)?
Ah i didnt see that. That fixed the issue perfectly , but now I have moved onto the other code. How could I implement that code into this

Could anyone give me some pointers?
jpmad4it is offline   Reply With Quote
Old 03-05-2008, 12:56 PM   PM User | #6
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
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:

PHP Code:
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?
jpmad4it is offline   Reply With Quote
Old 03-05-2008, 01:34 PM   PM User | #7
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
PHP Code:
<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) == && $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>
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-05-2008, 02:07 PM   PM User | #8
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
PHP Code:
<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) == && $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 is offline   Reply With Quote
Old 03-05-2008, 05:35 PM   PM User | #9
jpmad4it
New Coder

 
Join Date: Jan 2006
Posts: 85
Thanks: 2
Thanked 0 Times in 0 Posts
jpmad4it is an unknown quantity at this point
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:

PHP 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
jpmad4it is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:09 PM.


Advertisement
Log in to turn off these ads.