TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Maybe someone can explane me this code (http://www.talkphp.com/absolute-beginners/2071-maybe-someone-can-explane-me-code.html)

karq 01-23-2008 09:28 PM

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?

Orc 01-23-2008 09:46 PM

Quote:

Originally Posted by karq (Post 9415)
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.

TlcAndres 01-23-2008 10:51 PM

Quote:

Originally Posted by karq (Post 9415)
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.

Orc 01-23-2008 11:00 PM

Quote:

Originally Posted by TlcAndres (Post 9458)
$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


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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0