Page 1 of 1

php dynamic url??

Posted: Fri May 15, 2009 2:12 pm
by thoque
Hi All I am new php programmer and new to this forum. In the code I can not understand how it
echo "<td><a href=\"$scriptname?page=$page&photo=" . urlencode($image) . "\"><img src=\"" . $thumb->Thumblocation.$thumb->Thumbprefix.$image. "\" border=\"0\"></a></td>";
works?

The program is a simple image gallery where you click on a thumbnail and the large picture appears.

I will appreciate any sort of help. Thanks all.

Code: Select all

// Initialize thumbnail class
$thumb = new thumbnail;
 
// Options for EasyPhpAlbum Lite
$thumb->Thumbsize               = 140;                                              // The max. width or height
$thumb->Framewidth          = 10;                                                   // The frame width around the photo
$thumb->Shadow                  = true;                                             // Drop a shadow around the thumbnail   
$thumb->Backgroundcolor = '#FFFFFF';                                    // The background color (needed for the sahdow effect)
$thumb->Framecolor          = '#FFFFFF';                                    // The frame color
$thumb->Chmodlevel          = '0755';                                           // The chmod level for saving the thumbnails
$thumb->Thumblocation       = 'thumbs/';                                    // The directory path for the thumbnails
$thumb->Thumbprefix         = 'thumb_';                                     // The prefix for the thumbnails
$tableformat                        = '3x3';                                            // The table size: columns x rows
 
// This scripts name
$scriptname=basename(__FILE__);
if ($scriptname=='') {$scriptname='index.php';}
 
// Read all image files in directory
$images=array();$newthumbs=array();
if ($dir=@opendir('./')) {
  while ($filename=@readdir($dir)) {
    if (($filename!='.') && ($filename!='..') && !is_dir($filename)) {
      $extension=strtolower(substr($filename,strrpos($filename,'.')+1,strlen($filename)));
      if ($extension=='jpg' || $extension=='jpeg' || $extension=='png' || $extension=='gif') {
                $images[]=$filename;
                if (!file_exists($thumb->Thumblocation.$thumb->Thumbprefix.$filename))
                    $newthumbs[]=$filename;
      }
    }
  }
}
 
// Create and save new thumbs if required
if (count($newthumbs)>0) {
    if (!file_exists($thumb->Thumblocation)) {@mkdir($thumb->Thumblocation);@chmod($thumb->Thumblocation,octdec($thumb->Chmodlevel));}
    $thumb -> Createthumb($newthumbs);
}
 
// CSS style
echo "<style type=\"text/css\">\n";
    echo "a:link,a:visited,a:hover {\n";
    echo "  font-family : Helvetica, Arial;\n";
    echo "  font-size : 12px;\n";
    echo "  color : #000000;\n";
    echo "  text-decoration : none;\n";
    echo "}\n";
echo "</style>\n";
 
// The main table
echo "<table width=\"100%\" height=\"100%\"><tr><td align=\"center\" valign=\"middle\">\n";
 
// Display the thumbs or photo in an inner table
$column=0;
$table=explode('x',$tableformat);
$tablesize=$table[0]*$table[1];
if (isset($_REQUEST['page'])) {$page=intval($_REQUEST['page']);} else {$page=1;}
if (isset($_REQUEST['photo'])) {$photo=$_REQUEST['photo'];} else {$photo='';}
echo "<table>\n";
if ($photo=='') { 
    foreach($images as $key => $image) {
        if ($key>=($tablesize*($page-1)) && $key<($tablesize*$page)) {
            if ($column==0) {echo "<tr>";}
            echo "<td><a href=\"$scriptname?page=$page&photo=" . urlencode($image) . "\"><img src=\"" . $thumb->Thumblocation.$thumb->Thumbprefix.$image. "\" border=\"0\"></a></td>";
            $column+=1;
            if ($column==$table[0]) {$column=0;echo "</tr>\n";}
        }
    }
    if ($column<$table[0] && $column>0)
        echo str_repeat('<td>&nbsp;</td>',$table[0]-$column) . "</tr>\n" . "</table>\n";
    else
        echo "</table>\n";
} else {
    echo "<tr><td><a href=\"$scriptname?page=$page\"><img src=\"" . basename($photo) . "\" border=\"0\"></a></td></tr>\n". "</table>\n";
}
 
// Display the amount of pages in a seperate table
if ($tablesize<count($images) && $photo=='') {
    echo "<table><tr><td align=\"center\">";
    for ($i=1;$i<=ceil(count($images)/$tablesize);$i++) {
        if ($page==$i) {echo " <a href=\"$scriptname?page=$i\"><u>$i</u></a>";} else {echo " <a href=\"$scriptname?page=$i\">$i</a>";}
    }
    echo "</td></tr></table>\n";
}
 
// Close the main table
echo "</td></tr></table>\n";
 

Re: php dynamic url??

Posted: Fri May 15, 2009 3:21 pm
by nyoka
What bit are you not understanding or is it just not working for you?

In the line you mentioned (line 64 in your code list) my understanding is:
It prints to screen a single table cell containing a single link who's link address is the "urlencode()" value of $image. The link contains an image who's location is the value of the variables $thumb->Thumblocation and $thumb->Thumbprefix.$image.

Re: php dynamic url??

Posted: Fri May 15, 2009 3:50 pm
by thoque
Thanks nyoka for your reply.

I understand that what you are saying. The code is working perfect. What it does is if it finds images in the root folder then it creates thumbnails for those (I didn't past the function for the thumbnail part or the class for the thumbnail here in the posting). You can see 3x3 table with thumb images and if you clik on any of those images then you can see the full image. if you click on the full image (for example: http://localhost/TestProject/index.php? ... Leaves.jpg) then it goes back to the thumbnail image page (http://localhost/TestProject/index.php?page=1).

I don't understand how the link is working. I know how to use the dynamic url in php. However, I don't get how it is working in this way?

Re: php dynamic url??

Posted: Fri May 15, 2009 4:25 pm
by Darhazer
The key is in this check in the if statement, and the else block... when you click on an image, the else is executed:

Code: Select all

 
if ($photo=='') {
    foreach($images as $key => $image) {
        if ($key>=($tablesize*($page-1)) && $key<($tablesize*$page)) {
            if ($column==0) {echo "<tr>";}
            echo "<td><a href=\"$scriptname?page=$page&photo=" . urlencode($image) . "\"><img src=\"" . $thumb->Thumblocation.$thumb->Thumbprefix.$image. "\" border=\"0\"></a></td>";
            $column+=1;
            if ($column==$table[0]) {$column=0;echo "</tr>\n";}
        }
    }
    if ($column<$table[0] && $column>0)
        echo str_repeat('<td>&nbsp;</td>',$table[0]-$column) . "</tr>\n" . "</table>\n";
    else
        echo "</table>\n";
} else {
    echo "<tr><td><a href=\"$scriptname?page=$page\"><img src=\"" . basename($photo) . "\" border=\"0\"></a></td></tr>\n". "</table>\n";
}
 
You can see the initialization of $page and $photo at lines 57, 58

Re: php dynamic url??

Posted: Fri May 15, 2009 4:34 pm
by thoque
Ok then

Code: Select all

echo "<tr><td><a href=\"$scriptname?page=$page\"><img src=\"" . basename($photo) . "\" border=\"0\"></a></td></tr>\n". "</table>\n";
this line executes after I click on a thumbnail. But I am not getting how the url thing is working. I am novice PHP programmer. I only know simple linking. But this onw seems to me as a little bit different. There is only one file called Index.php......then where do i go everytime I click thumbnails. and when i click on the large picture i come back to the tuhmbnail page again. But there is only one php file called Index.php. The only thing I see the URL changes something like http://localhost/TestProject/index.php? ... Leaves.jpg when I click on a thumbnail. When i come back to the thumbnail page by clicking the large picture I see the URL http://localhost/TestProject/index.php?page=1.

Can you explain it to me how it works?

Re: php dynamic url??

Posted: Fri May 15, 2009 5:12 pm
by Darhazer
Hi,

This is when PHP comes actually instead of simple HTML.
The PHP is a dynamic page - this means that every time you are opening it, it can display different things. This can be based on data in a database or in files at the server, on parameters the page receives, or any combination of those.

So actually you are passing a parameter in the URL and the PHP program decides what to output.
If you are familiar with any other programming language, this is like having a set of functions, and asking a user what to do?

Or having a calc program... it operates with 10 and 5... user can pass parameter +,-,* or / and get 15,5,50 or 2.

I hope this make thing clear.