
12-30-2003, 04:38 PM
|
|
Web Hosting Guru
|
|
Join Date: Aug 2003
Posts: 266
|
|
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> </td>
<td width=70>pic002.jpg</td>
<td width=15> </td>
<td width=70>pic003.jpg</td>
<td width=15> </td>
<td width=70>pic004.jpg</td>
<td width=15> </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
|

12-30-2003, 06:14 PM
|
|
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. 
|

12-30-2003, 07:57 PM
|
|
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 <= 4 ) {
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.
|

12-30-2003, 08:55 PM
|
|
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>';
|

12-30-2003, 09:14 PM
|
|
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.
|

12-30-2003, 09:28 PM
|
|
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).
|

12-31-2003, 03:30 AM
|
|
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??
|

12-31-2003, 04:19 AM
|
|
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!
|

12-31-2003, 06:07 AM
|
|
Newbie
|
|
Join Date: Dec 2003
Posts: 5
|
|
Thanks for the tip. 
|

12-31-2003, 06:40 AM
|
|
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.
|

12-31-2003, 09:50 AM
|
|
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.
|

12-31-2003, 12:16 PM
|
|
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.
|

12-31-2003, 01:51 PM
|
|
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 % 4 = 0) {
echo "<td width=75><img src='" . $file_base . leading_zero($x, 3, 0) . ".jpg' alt='Thumbnails' border=0></td>" . "<td width=15> </td></tr><tr>"; //print with new row
$x++; //increase pic #
}
else {
echo "<td width=75><img src='" . $file_base . leading_zero($x, 3, 0) . ".jpg' alt='Thumbnails' border=0></td>" . "<td width=15> </td>"; //print without new row
$x++;
}
else {
$x--; //x is now one greater than the number of files, so bump it down
$y = 4 - ($x % 4); //find the number of empty rows that need filling
while ($y > 0 ) {
echo "<td width=75> </td><td width=15> </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 + -1 - 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?
|

12-31-2003, 02:13 PM
|
|
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($i, 3, '0', STR_PAD_LEFT) .".jpg' alt='Thumbnails' border='0'></td>\n";
echo " <td width=15> </td>\n";
echo $newrow;
$i++;
}
$i--;
while ($i % $cols) {
echo " <td width=70> </td>\n";
echo " <td width=15> </td>\n";
$i++;
}
?>
</tr>
</table>
|

12-31-2003, 02:30 PM
|
|
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 
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|