Problem with for loop and a include menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nilsgoe
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 3:05 pm

Problem with for loop and a include menu

Post by nilsgoe »

Hi all

I'm pretty new to php and i really struggle to work my way around it. I'm looking desperately for some help with a for loop and a include for a menu. I have a php template homepage with a menu that pulls the links via a php include link into the index.php page. I have a menu point called gallery where i have made a gallery that fetches the thumbnails and the bigger pictures via a for loop. The for loop fetches all the thumbnails the right way, but by clicking on them to activate the bigger picture, the first page of the index.php (which in my case is home.php) gets loaded in the include section instead of the bigger picture from the gallery.
I hope this makes sense, it's my first week with php, so please bare over with me ;-)

This is my include php code:

Code: Select all

 
<?php
$page = $_GET['file'];
if (strlen($page) == 0)
       $page = "home.php";
include($page);
?>
 
And this is my for loop for the gallery:

Code: Select all

 
<?php
 
$dir = 'pic/';
$thumbs = array("small_hamburg1.jpg", "small_hamburg2.jpg", "small_hamburg3.jpg",);
$gallery = '<ul id="img_nav">' . "\n";
 
 
for($i=0; $i<count($thumbs); $i++){ 
    
    $thumb = $thumbs[$i];  
    $find ="/small_/"; 
    $replace ="big_"; 
    $big = preg_replace ($find, $replace, $thumb); 
    $gallery .= '<li><a href="?pic='. $big .'">'; 
    $gallery .= '<img src="' . $dir . $thumb . '" alt=" " /></a></li>'; 
}
 
 
$gallery .= "\n </ul> \n ";
if(isset($_GET['pic'])){
    
   $img = $_GET['pic'];
   $gallery .= '<p><img src="' . $dir . $img . '" alt="img " /></p>';
}else{
    
   $gallery .= '<p>Pick a pic from the thumbs!</p>';
}
 
echo $gallery;
?>
 
Here is a link to my site i'm working on so you can see my problem in action ;-)

http://www.365posts.com/school/include/ ... allery.php

I Hope you can help me.

Nils
davegmpd
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 9:42 am
Location: London

Re: Problem with for loop and a include menu

Post by davegmpd »

Two things:

1. The way you've made it, you need to keep the "file=gallery.php" part of the URL; eg:

<a href="?file=gallery.php&pic='. $big .'">

2. The idea of doing an "include" on a variable sent via _GET is a *bad* idea, because I can choose to include *any* file I wish; eg:

http://www.365posts.com/school/include/ ... etc/passwd

Basically, you need to fix point#2 immediately!

Dave
nilsgoe
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 3:05 pm

Re: Problem with for loop and a include menu

Post by nilsgoe »

Hi Dave

Thanks four your help.
I have removed the _GET include, and included the navigation in every single page (i know this might not be the best way for a solution, but the only way i can get it to work probably :wink: ) and now the gallery loop is working the way it is supposed to :D

So thanks for your time.

Nils
Post Reply