09-05-2009, 21:42
|
#1
|
|
cf.mega poster
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 37
Services: Canal Digital: 50/10
Posts: 7,577
|
PHP Issues.
I'm having some issues using PHP to dynamically create a table...
Basically it reads imagenames from a folder, then creates a table and has 3 items per row. It creates a new row every 3 items, or should... That's the problem, the "<tr>"'s and "</tr>"'s never get printed...
Here's the code:
PHP Code:
<?php $i = "0"; $Folder = "./Photos/"; $DirHandle = @opendir($Folder) or die($Folder." could not be opened."); print ("<table width=40% align=center valign=center>"); while ($Filename = readdir($DirHandle)) { if (i == 2) print ("<tr>"); if (($Filename != ".") AND ($Filename != "..")) print ("<td width=200 height=200><img src=\"$Folder$Filename\" height=95% width=95%>$i</img></td>"); if (i == 2) print ("<\tr>"); if (($Filename != ".") AND ($Filename != "..")) ++$i;; } closedir($DirHandle); print ("</table>"); ?>
Any ideas?
Also, sorry for any particularly bad code, i'm just learning
Also, this can be tested at: http://revts.ath.cx while i am online (Pass: Password)
EDIT: Also, the 0 never shows beneath the first image...
EDIT2: Fixed the 0 never being shown... Code above amended (used i == instead of i =)
|
|
|
09-05-2009, 22:06
|
#2
|
|
cf.mega poster
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 37
Services: Canal Digital: 50/10
Posts: 7,577
|
Re: PHP Issues.
if (($Filename != ".") AND ($Filename != "..")) ++$i;;
I do in that line, it works fine, and the numbers do count up, yet the html doesn't get written... 
Fixed declaring as a string now too, thanks 
EDIT: Could have sworn i saw a post by punky above this...
---------- Post added at 00:06 ---------- Previous post was yesterday at 23:54 ----------
Ah man i'm a dunce... Forgot to use the dollar sign $ before the i in the if statments to print the html...
Think i'm too tired...
|
|
|
09-05-2009, 22:15
|
#3
|
|
Inactive
Join Date: Jun 2003
Age: 44
Posts: 14,750
|
Re: PHP Issues.
Yeah, I did post, but I spotted the ++$i after mine and before yours, so I deleted it. Alas too late.
The <tr>s don't get printer is because you have i instead of $i
The code is really weird though.
---------- Post added at 23:15 ---------- Previous post was at 23:13 ----------
Also, another prob I spotted... your <tr> closing tag should be </tr> not <\tr>.
A backslash within a string is a special character. In this case \t = tab.
|
|
|
09-05-2009, 22:18
|
#4
|
|
cf.mega poster
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 37
Services: Canal Digital: 50/10
Posts: 7,577
|
Re: PHP Issues.
Fully functional (abeit badly written) code:
Code:
<?php
$i = 0;
$Folder = "./Photos/";
$DirHandle = @opendir($Folder) or die($Folder." could not be opened.");
print ("<table width=40% align=center valign=center>");
while ($Filename = readdir($DirHandle))
{
if ($i == 2) print ("<tr>");
if (($Filename != ".") AND ($Filename != "..")) print ("<td width=200 height=200><img src=\"$Folder$Filename\" height=95% width=95%>$i</img></td>");
if ($i == 2) print ("<//\tr>");
if (($Filename != ".") AND ($Filename != "..")) ++$i;;
}
closedir($DirHandle);
print ("</table>");
?>
Cheers for the pointers
---------- Post added at 00:18 ---------- Previous post was at 00:15 ----------
Quote:
Originally Posted by punky
Also, another prob I spotted... your <tr> closing tag should be </tr> not <\tr>.
A backslash within a string is a special character. In this case \t = tab.
|
That causes a problem, it seems to then spit out a </tr> tag every iteration, rather then when it should be (every 2nd).
EDIT:
Also changed the closing TR tag code to:
Code:
if ($i == 4) print ("<//\tr>") AND $i = 0;
(And modified the opening tags, it now is every 4 rather than just the first 4)
|
|
|
10-05-2009, 15:52
|
#5
|
|
cf.mega poster
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 37
Services: Canal Digital: 50/10
Posts: 7,577
|
Re: PHP Issues.
Can anyone just take a peek at it? So i can get some feedback...
http://revts.ath.cx
Password: Authentic8
|
|
|
10-05-2009, 16:14
|
#6
|
|
Dr Pepper Addict
Cable Forum Admin
Join Date: Oct 2003
Location: Nottingham
Age: 63
Services: IDNet FTTP (1000M), Sky Q TV, Sky Mobile, Flextel SIP
Posts: 30,583
|
Re: PHP Issues.
This makes no sense, what is it supposed to do ?
Quote:
|
if ($i == 4) print ("<//\tr>") AND $i = 0;
|
__________________
Baby, I was born this way.
|
|
|
10-05-2009, 16:15
|
#7
|
|
cf.mega poster
Join Date: Mar 2006
Location: Oslo, Norway.
Age: 37
Services: Canal Digital: 50/10
Posts: 7,577
|
Re: PHP Issues.
Quote:
Originally Posted by Paul M
This makes no sense, what is it supposed to do ?
|
If the i is equal to 4 (it's looping through, increasing by 1 each time) print the tag <\tr> and reset i to 0.
Works fine, though it looks odd.
|
|
|
10-05-2009, 16:19
|
#8
|
|
Inactive
Join Date: Oct 2005
Location: Merseyside
Age: 37
Services: BT Infinity Option 2, HH5, synced at maximum 80Mbps/20Mbps.
Posts: 2,221
|
Re: PHP Issues.
Your coding of PHP is far different to mine thats for sure... Its not structured like I structure PHP at all. No wonder you're finding it difficult. Each function should be on its own line ideally. It makes it easy to troubleshoot.
|
|
|
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
|
|
|
All times are GMT. The time now is 21:54.
|