TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-23-2008, 09:28 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 12
Thanks: 0
karq is on a distinguished road
Default Maybe someone can explane me this code

So I'm trying to make a simple php gallery. So far I made the uploading form and file. But when I searched the internet for "How to get those pics on the page" and I found this code;

PHP Code:
<?php
$ava
=opendir("failid/");
$i=0;
while (
$lisa=readdir($ava)){
if (
$lisa !="." && $lisa !="..") {
$files[]=$lisa;
echo 
'<tr><td><img src="failid/'.$files[$i].'" height="150" width="200"></tr></td>';
$i=$i+1;
}
}
closedir($ava);
?>
It works, but I dont understand it, maybe someone can explane?
karq is offline  
Reply With Quote
Old 01-23-2008, 09:46 PM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by karq View Post
So I'm trying to make a simple php gallery. So far I made the uploading form and file. But when I searched the internet for "How to get those pics on the page" and I found this code;

PHP Code:
<?php
$ava
=opendir("failid/");
$i=0;
while (
$lisa=readdir($ava)){
if (
$lisa !="." && $lisa !="..") {
$files[]=$lisa;
echo 
'<tr><td><img src="failid/'.$files[$i].'" height="150" width="200"></tr></td>';
$i=$i+1;
}
}
closedir($ava);
?>
It works, but I dont understand it, maybe someone can explane?
It's incrementing $i for $files, which is an array, so it's getting the key, which each key is set within each file of $lisa, also you can do an if ( $lisa !="." ) continue; and it should both of them out with lesser bytes of code.

You can do print_r($files) and it should show you each key set between the value.

To me it should look like this:
Code:
array = (
  "0" => file,
  "1" => file,
  "2" => file,
  "3" => file
);
To me you should do a foreach iteration upon the array, with $key => $value and then display, though I cannot test it, I'm more experimental type guy, so go for it!

To me it's basically putting all the files names inside an Array, then you can do that foreach and it should output the image from using $files[$i] as since $i is incrementing.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:51 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Quote:
Originally Posted by karq View Post
So I'm trying to make a simple php gallery. So far I made the uploading form and file. But when I searched the internet for "How to get those pics on the page" and I found this code;

PHP Code:
<?php
$ava
=opendir("failid/");
$i=0;
while (
$lisa=readdir($ava)){
if (
$lisa !="." && $lisa !="..") {
$files[]=$lisa;
echo 
'<tr><td><img src="failid/'.$files[$i].'" height="150" width="200"></tr></td>';
$i=$i+1;
}
}
closedir($ava);
?>
It works, but I dont understand it, maybe someone can explane?



$ava is handle created with the opendir function that contains the path to where the pictures are stored, the script then loops through the directory using checking to make sure the file string is not either of the dots, then it echos a formated text string with the array, the method used is unnecesary this is a better example


PHP Code:
<?php
$dir 
opendir('/path/to/pictures/');
$i = -1;
while(
$file readdir($dir))
{
   if(
$file != "." && $file != "..")
   {
     echo 
'<tr><td><img src="failid/'.$files[++$i].'" height="150" width="200"></tr></td>';
   }
}
closedir($dir);

you could ofcourse use the object oriented method using dir() or glob() as wildhoney is so fond of.
TlcAndres is offline  
Reply With Quote
Old 01-23-2008, 11:00 PM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
$ava is handle created with the opendir function that contains the path to where the pictures are stored, the script then loops through the directory using checking to make sure the file string is not either of the dots, then it echos a formated text string with the array, the method used is unnecesary this is a better example


PHP Code:
<?php
$dir 
opendir('/path/to/pictures/');
$i = -1;
while(
$file readdir($dir))
{
   if(
$file != "." && $file != "..")
   {
     echo 
'<tr><td><img src="failid/'.$files[++$i].'" height="150" width="200"></tr></td>';
   }
}
closedir($dir);

you could ofcourse use the object oriented method using dir() or glob() as wildhoney is so fond of.
Yeah, I love the Dir associated-class :D
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:38 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design