Re: Re: [Linux dev] Undefined variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hendrik Visage wrote:
<snip useful explanation of what's wrong with no tips on how to do it right>

The rewrite below does not iterate around the array as you were in your for loop, so I'll mention a better way to do that here rather than in the snippet below... foreach - look it up in the PHP manual!

One way to rewrite it would be like this (with comments hopefully explaining what I've changed and why)...

<?php
   session_start();
   $folder = opendir('.');
// No need for a temporary array, check each file as you read it from the directory
   $names = array();
   while ($file = readdir($folder))
   {
if (in_array(strtolower(substr($file, -4)), array('.jpg', '.gif', 'jpeg', '.png')))
       {
// You can append an element to an array like this... no need for an index
           $names[] = $file;
       }
   }

   sort($names);

// Not sure why you're checking a session var here since you've just gone to the trouble of iterating
   // over the files - it's not saving much processing

   // Best practice to always check if a session var exists before using it
   // Avoids unsightly notices
   $Count = (isset($_SESSION['Picture']) ? $_SESSION['Picture'] : 0);
// Really not sure why this is here
   --$Count;
   //        echo 'Count = '.$Count.'  $tempvar = '.$tempvar;

   if ($Count == 0)
   {
       $Count = count($names1)-1;
   }

   $_SESSION['Picture'] = $Count;
   $slika = $names[$Count];
   // image dimensions
   $dimensions = GetImageSize($slika);
   if (isset($HTTP_GET_VARS["pic"]))
   {
// Strictly speaking the URL given in a location header should be absolute
       header ("Location: $slika");
   }
   else
   {
       echo "<img src=\"$slika\" $dimensions[3] />";
   }

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux