hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP looping & Tables.
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP looping & Tables.

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-30-2003, 04:38 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266

PHP looping & Tables.


Hello,

I'm having some trouble with the syntax for creating this table.

Code:
<table width="562" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width=220 align="left" bgcolor="#FFFFFF">
	<?php include ($pic) ?>
	</td>
    <td width=342 align="left" bgcolor="#FFFFFF">
	
	<table width="342" border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td width=70>pic001.jpg</td>
		<td width=15>&nbsp;</td>
		<td width=70>pic002.jpg</td>
		<td width=15>&nbsp;</td>
		<td width=70>pic003.jpg</td>
		<td width=15>&nbsp;</td>
		<td width=70>pic004.jpg</td>
		<td width=15>&nbsp;</td>
	  </tr>
	</table>
	</td>
  </tr>
</table>
Where you see the pic001, etc table that is what I'm focussing on. What I want to do is write a script that will loop through pic001 - picXXX writing a table like the one above.

I would specify in a variable how many pictures there are and then the program would continue to loop through creating more and more rows and increasing the number on the end of the file name until the last picture is included. At this point, I need it to identify how many more column I need to complete the row and put those in (just blank).

Can anyone help? I may be hiring a PHP coder in the future for a larger project so I'd appreciate this as a sort of test-project

Reply With Quote


Sponsored Links
  #2  
Old 12-30-2003, 06:14 PM
Phil Mossop Phil Mossop is offline
Newbie
 
Join Date: Dec 2003
Posts: 5
Resuming you have already printed the HTML for opening/closing the table, just stick this in where you want the cells to appear:

PHP Code:
<?php

$no 
4// Set to the max number

$i 0;

while ( ! 
$i $no ) {

    echo 
"<td width=70>pic00"$i .".jpg</td>";
    echo 
"<td width=15> </td>";

    
$i++;

}

?>
HTH.

Reply With Quote
  #3  
Old 12-30-2003, 07:57 PM
Jakiao Jakiao is offline
Community Guide
 
Join Date: Jun 2003
Location: Janesville, Wi
Posts: 1,509
If you're shooting for least code needed, this will work (very similar the phantom's, but it removes a step).

PHP Code:
<table width="342" border="0" cellspacing="0" cellpadding="0">
      <tr>
      
      <?php
      
          $x 
1;
        
        while ( 
$x <= ) {
      
              print 
"
      
                <td width=70>pic00
$x.jpg</td>
                <td width=15> </td>
            
            "
;
            
            
$x++;
            
        }
        
    
?>
        
      </tr>
    </table>
That removes two php steps previously found in the first post.

Reply With Quote
Sponsored Links
  #4  
Old 12-30-2003, 08:55 PM
CyberAlien CyberAlien is offline
Aspiring Evangelist
 
Join Date: Sep 2002
Posts: 389
> echo "<td width=70>pic00". $i .".jpg</td>";

Why you use double quotes and dot in echo()? Its awfully slow. When using double quotes php parses that line slowly - use single quotes. And there is no point in using dot because in code above php contecates string and then echoes it - use comma instead and php will echo each item separately which is much faster.

So that line should look like this:
echo '<td width=70>pic00', $i, '.jpg</td>';

__________________
CyberAlien.
phpBB styles, responsive XenForo styles

Reply With Quote
  #5  
Old 12-30-2003, 09:14 PM
silhouette silhouette is offline
Web Hosting Master
 
Join Date: Mar 2003
Posts: 878
Quote:
Originally posted by CyberAlien
Why you use double quotes and dot in echo()? Its awfully slow. When using double quotes php parses that line slowly - use single quotes.
So there is difference when PHP handles ' vs "
How does PHP handle ' vs " ??
Ignorant me always thought they are just different notations for strings.

Reply With Quote
  #6  
Old 12-30-2003, 09:28 PM
Jakiao Jakiao is offline
Community Guide
 
Join Date: Jun 2003
Location: Janesville, Wi
Posts: 1,509
When you have quotes in use, single quotes can be used inside of it. Basically, single quotes wont kill full quotes when used inside eachother. An example:

mysql_query("SELECT * FROM blah WHERE some='blah' LIMIT 1", $int);

See how that works?

The use of full quotes inside of full quotes would be:

print "Text goes here yada yada " . $variable . " blah blah";

However, I only do the above when I need to use variables like $blah[$x] or $blah[2] or function(something).

Reply With Quote
  #7  
Old 12-31-2003, 03:30 AM
silhouette silhouette is offline
Web Hosting Master
 
Join Date: Mar 2003
Posts: 878
Quote:
Originally posted by jakiao
Basically, single quotes wont kill full quotes when used inside eachother
So how does using single quotes(') replacing double quotes("), make the parsing of your PHP script faster as CyberAlien mentioned??

Reply With Quote
  #8  
Old 12-31-2003, 04:19 AM
hiryuu hiryuu is offline
Web Hosting Master
 
Join Date: Jan 2003
Posts: 1,715
Double-quoted strings are parsed for variables (and some other special characters, like "\n"), so

echo "<td width=70>pic00$i.jpg</td>";

would also produce the desired result (as in jakiao's code). That does get a little broken if the next character could be part of a variable name, though. Like pic00?s.jpg and
echo "<td width=70>pic00$is.jpg</td>";

As long as we're picking at nanoseconds, though, you really should merge the increment op for something like
echo '<td width=70>pic00', $i++, '.jpg</td>';

I mean, why waste a return value?

__________________
Game Servers are the next hot market!
Slim margins, heavy support, fickle customers, and moronic suppliers!
Start your own today!

Reply With Quote
  #9  
Old 12-31-2003, 06:07 AM
Phil Mossop Phil Mossop is offline
Newbie
 
Join Date: Dec 2003
Posts: 5
Thanks for the tip.

Reply With Quote
  #10  
Old 12-31-2003, 06:40 AM
CyberAlien CyberAlien is offline
Aspiring Evangelist
 
Join Date: Sep 2002
Posts: 389
Actually its not nanoseconds - difference of performance with single quotes and double quotes are about 20-30 times. And if you have large script it is always better to optimize it for speed and that's where single quotes come in.

I did lots of speed tests about half a year ago when was optimizing phpBB template engine. Difference in speed is huge.

__________________
CyberAlien.
phpBB styles, responsive XenForo styles

Reply With Quote
  #11  
Old 12-31-2003, 09:50 AM
Jakiao Jakiao is offline
Community Guide
 
Join Date: Jun 2003
Location: Janesville, Wi
Posts: 1,509
I know how the difference in usage works. I prefer avoiding the use of ' in non-needed enviroments. I guess it is just based on preference.

Reply With Quote
  #12  
Old 12-31-2003, 12:16 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
This is a start guys, I may just have to figure this one out on my own. The part we're missing from the code is that every 4 lines it'll start a new row (so something like if $X MOD 4 is a whole number...) and then at the end we have to see how many lines are left (prolly using X MOD 4 again to see how many more columns I need to fill before closing the table.

Thanks though.

Reply With Quote
  #13  
Old 12-31-2003, 01:51 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
Ok guys, here is what I've come up with:

PHP Code:
<table width="562" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width=220 align="left" bgcolor="#FFFFFF">
    pic
    </td>
    <td width=342 align="left" bgcolor="#FFFFFF">
    
    <table width="342" border="0" cellspacing="0" cellpadding="0">
      <tr>
    <?php
        
/* VARIABLES:
        $num_pics = number of pictures in teh folder
        $x = a counter
        $file_base = filename without the 3-digit number extension */
        
$num_pics 71;
        
$file_base "images/Whitby/Hockey/Atom AAA/2003-2004/thumbnails/atomaaa";        
        
$x 1;
        
        if (
$num_pics >= $x) {
            if (
$x 0) {
                echo 
"<td width=75><img src='" $file_base leading_zero($x30) . ".jpg' alt='Thumbnails' border=0></td>" "<td width=15>&nbsp;</td></tr><tr>"//print with new row
                
$x++; //increase pic #
            
}
            else {
                echo 
"<td width=75><img src='" $file_base leading_zero($x30) . ".jpg' alt='Thumbnails' border=0></td>" "<td width=15>&nbsp;</td>"//print without new row
                
$x++;
            }
        else {
            
$x--; //x is now one greater than the number of files, so bump it down
            
$y - ($x 4); //find the number of empty rows that need filling
            
            
while ($y ) {
                echo 
"<td width=75>&nbsp;</td><td width=15>&nbsp;</td>";
                
$y--;
            }
        }
    
?>
      </tr>
    </table>

    
    </td>
  </tr>
</table>
<?php
function leading_zero$aNumber$intPart$floatPart=NULL$dec_point=NULL$thousands_sep=NULL) {        //Note: The $thousands_sep has no real function because it will be "disturbed" by plain leading zeros -> the main goal of the function
  
$formattedNumber $aNumber;
  if (!
is_null($floatPart)) {    //without 3rd parameters the "float part" of the float shouldn't be touched
   
$formattedNumber number_format($formattedNumber$floatPart$dec_point$thousands_sep);
   }
  
//if ($intPart > floor(log10($formattedNumber)))
   
$formattedNumber str_repeat("0",($intPart + -floor(log10($formattedNumber)))).$formattedNumber;
  return 
$formattedNumber;
  }
  
?>
I'm getting a parse error on line 20 (if ($x % 4 = 0)...) Am I using the modulo division (remainder division) correctly? $x % 4 = 0 so if the number is divisible by 4 it should return 0. Anyone see the problem?

Reply With Quote
  #14  
Old 12-31-2003, 02:13 PM
Zwockel Zwockel is offline
Newbie
 
Join Date: Dec 2003
Posts: 16
if ($x % 4 = 0).....

Try using.... if ($x % 4 == 0)

or..... if (!($x % 4))

I came up with the following, untested but should work....

PHP Code:
    <table width="342" border="0" cellspacing="0" cellpadding="0">
      <tr>
<?php
$cols 
4// Set to the max image cols
$num_pics 71;
$file_base "images/Whitby/Hockey/Atom AAA/2003-2004/thumbnails/atomaaa";
$i 1;

while ( 
$i <= $num_pics ) {
    
    
$newrow = !($i $cols) ? "      </tr>\n      <tr>\n" ''//

    
echo "            <td width=70><img src='{$file_base}"str_pad($i3'0'STR_PAD_LEFT) .".jpg' alt='Thumbnails' border='0'></td>\n";
    echo 
"            <td width=15>&nbsp;</td>\n";
    echo 
$newrow;

    
$i++;

}
$i--;
while (
$i $cols) {
    
    echo 
"            <td width=70>&nbsp;</td>\n";
    echo 
"            <td width=15>&nbsp;</td>\n";
    
    
$i++;
}

?> 
      </tr>
    </table>

Reply With Quote
  #15  
Old 12-31-2003, 02:30 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
thanks for the code. It seems to be working. I just have a few questions so that I can understand a learna little something:

$newrow = !($i % $cols) ? " </tr>\n <tr>\n" : ''; //
What does this line do? I can see it has something to do with deciding whether or not we need a new row but I'm not sure how it work, and not sure what the '?' does.

Similarily, I need to know what echo $newrow does (I guess this will be answered with the answer to my first question )

Thanks

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Amazon Web Services Reduces Price of DynamoDB NoSQL, Adds Reserved Capacity Web Hosting News 2013-03-08 15:19:40
Web Host Go Daddy Says Internal Network Events, Not Hacker, Caused Service Outage Web Hosting News 2012-09-12 11:56:11
Hackers Post 450,000 Yahoo! Voices User Login Credentials Online Web Hosting News 2012-07-12 10:38:07
New on WHIR TV: Open-Xchange CEO Rafael Laguna at the OX Partner Summit Blog 2011-12-02 16:10:28
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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 Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?