hi im not great at programming and have been trying to fix this for ages.

i am using a script to create thumbnails and display images it all works fine.

the problem is for each record on my database, eg each page on my site i have numerical image folders eg images/1
images/2 and so on.

on my php page i have the gallery which only shows the images from the one folder,

can anyone help me write in the variable which will show the correct folder based on the id passed from the row in the database?.

ie

<?php
//location of your images//
$images_dir = "images/1/";
$thumbs_dir = "images/1/thumbnails/";
?>

my understanding is it should be something like

$images_dir ="images/$id"
$thumbs_dir ="images/$id/thumbnails"

with the id being passed and set before, ie

$id = $get [id]

please help.

Recommended Answers

All 26 Replies

what you have done so far? post your code, and tell whats NOT happening.

what you have done so far? post your code, and tell whats NOT happening.

i have tried it a few times but without success,

having trouble with how to properly code the variable

ie
$images_dir = "images/?";
if anyone knows i can try it and post the result.

Wheres your data - mysql? it's stored as the rows unique id?

//connect to your mysql server
$mysql_query = "select `id` from `table` limit 1";
$mysql_result = mysql_query($mysql_query);
$mysql_array = mysql_fetch_assoc($mysql_result);

$images_dir = "images/{$mysql_array['id']}/thumbnails/";

or for multiple rows

//connect to your mysql server
$mysql_query = "select `id` from `table`";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($mysql_result)){
echo "images/{$mysql_array['id']}/thumbnails/";
}

Wheres your data - mysql? it's stored as the rows unique id?

//connect to your mysql server
$mysql_query = "select `id` from `table` limit 1";
$mysql_result = mysql_query($mysql_query);
$mysql_array = mysql_fetch_assoc($mysql_result);

$images_dir = "images/{$mysql_array['id']}/thumbnails/";

or for multiple rows

//connect to your mysql server
$mysql_query = "select `id` from `table`";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($mysql_result)){
echo "images/{$mysql_array['id']}/thumbnails/";
}

ok this looks good, yes the data is stored in mysql, i will try now

as for multiple rows i dont understand , yes i have multiple rows but i only want to display one folders contents , the name of the folder being the same as the rows id.

this should work, that is what i wasnt doing putting the id into a mysql query,

ok
here is what i done i think ive removed the brackets around the function so it wont work
any suggestions?.

here is my code and the error
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

it says error starts just at $images_dir, i now ive removed the brackets where should they go

<?php
$mysql_query = "select `id` from `table`";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($mysql_result))



$images_dir = "images/$mysql_array/";
$thumbs_dir = "images/$mysql_array/thumbnails/";
$thumbs_width = 150;
$images_per_row = 2;
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<a href="',$images_dir.$file,'" class="photo-link smoothbox" rel="gallery"><img src="',$thumbnail_image,'" /></a>';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}
?>

1)
You must use {} around varaibles in string when it has quote or array

$images_dir = "images/{$mysql_array['id']}/";
$thumbs_dir = "images/{$mysql_array['id']}/thumbnails/";

2)
also your while loop is with out braces and code.

Specify you want to read data from mysql or you want to read directory.

1)
You must use {} around varaibles in string when it has quote or array

$images_dir = "images/{$mysql_array['id']}/";
$thumbs_dir = "images/{$mysql_array['id']}/thumbnails/";

2)
also your while loop is with out braces and code.

Specify you want to read data from mysql or you want to read directory.

hey thanks, ive added the brackets on the image directory link, here is what it looks like
<?php


$mysql_query = "select `id` from `addrental`";
$mysql_result = mysql_query($mysql_query);
$mysql_array = mysql_fetch_assoc($mysql_result);


$images_dir = "images/{$mysql_array}/";
$thumbs_dir = "images/{$mysql_array}/thumbnails/";
$thumbs_width = 150;
$images_per_row = 2;
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<a href="',$images_dir.$file,'" class="photo-link smoothbox" rel="gallery"><img src="',$thumbnail_image,'" /></a>';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}
?>

hey
it has stopped showing error now , but only shows the one folder of images on all different pages.

i removed the while loop, was that a mistake, sorry for being so slow at this :(

you'll want to specify an id probably rather than just picking the first row, i've added messages at each step so you can see what's happening it's good practice to find these errors out yourself. The error you have is this line echo '<a href="',$images_dir.$file,'" class="photo-link smoothbox" rel="gallery"><img src="',$thumbnail_image,'" /></a>'; them comma's should be full stops

<?php
$id = $_GET['id'];
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";
$mysql_result = mysql_query($mysql_query);
$mysql_array = mysql_fetch_assoc($mysql_result);


$images_dir = "images/{$mysql_array['id']}/";
$thumbs_dir = "images/{$mysql_array['id']}/thumbnails/";
$thumbs_width = 150;
$images_per_row = 2;
$image_files = get_files($images_dir);
$messages = '';
if(count($image_files) > 0) {
	$messages .= 'image files is greater than 0'."<br/>\r\n";
	$index = 0;
	foreach($image_files as $key=>$file) {
		$messages .= "image file index: {$index} key: {$key} and file: {$file} <br/>\r\n";
		$thumbnail_image = $thumbs_dir.$value;
		if(!file_exists($thumbnail_image)) {
			$messages .= "thumbnail image doesnt exist: {$thumbnail_image} <br/>\r\n";
			$extension = get_file_extension($thumbnail_image);
			if($extension) {
				$messages .= "extension was true: {$extension} <br/>\r\n";
				make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
			}else{
				$messages .= "extension was false: {$extension} <br/>\r\n";
			}
		}else{
			$messages .= "thumbnail image exists: {$thumbnail_image} <br/>\r\n";
		}
		echo '<a href="'.$images_dir.$file.'" class="photo-link smoothbox" rel="gallery"><img src="'.$thumbnail_image.'" /></a>';
		if($index >= $images_per_row){//?
			$messages .= "index >= images per row: {$index}-{$images_per_row} <br/>\r\n";
			echo '<div class="clear"></div>';
			$index = 0;
		}else{
			$messages .= "index < images per row: {$index}-{$images_per_row} <br/>\r\n";
			$index++;
		}
	}
}else{
	$messages .= "no images in this gallery: {$images_dir} <br/>\r\n";
	$messages .= "image files content".var_dump($image_files);
	echo '<p>There are no images in this gallery.</p>';
}

echo "<br/>\r\n{$messages}";
?>

thanks for your help i will fix those later on, i take it that is why the smoothbox snt working properly.

unfortunatley it stll is only showing folder 1 on all pages.

do i need the while loop to make it work.

i think its getting closer now though.

thanks for your help i will fix those later on, i take it that is why the smoothbox snt working properly.

unfortunatley it stll is only showing folder 1 on all pages.

do i need the while loop to make it work.

i think its getting closer now though.

are you setting the id? The very top i added in so the script pulls a GET variable

<?php
$id = $_GET['id'];
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";

If the id is not set or isn't a number the id gets set to 1, thats to prevent mysql injection to set it you navigate to the page like so

mypage.php?id=5
mypage.php?id=7
mypage.php?id=drop table mysql

last one being a mysql injection attempt, it's not a number so will just be set to 1.

advantage of doing that is you can now have a gallery page and list out many galleries and link to each one without having to make a new page each time

hey biim did you just rewrite all that code?

i got it from a programmer called david walsh

hey biim did you just rewrite all that code?

i got it from a programmer called david walsh

Kinda, i copied it into Zend and just added bits and reformatted it. the $index variable was used twice so would of overwritten itself so i renamed one to $key and there was a weird if statement with a % in it i had never seen before.
When you want to debug something you always want to know what happens at each point and what's in the variables to narrow it down so I added the messages, that name does sound familiar

yes you can find it at his blog david walsh blog, as said will add your reworkings later once this works.

here is where im at , it is giving error...

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource


here is code

$id = $_GET['id'];
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($mysql_result)){

 $images_dir = "images/{$mysql_array['id']}/";
$thumbs_dir = "images/{$mysql_array['id']}/thumbnails/";
$thumbs_width = 150;
$images_per_row = 2;
$image_files = get_files($images_dir);
}

if i put in this line it shows id 1 on all pages

if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1

if i leave it out it gives the error
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

//connect to your mysql server
$mysql_query = "select `id` from `table`";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($mysql_result)){echo "images/{$mysql_array['id']}/thumbnails/";}

if i just use this code it uses the the id from the last row in the table,

but nearly works

yeah it sounds like you're missing my post before last with setting the id in the url

mypage.php?id=5
makes $_GET = '5'

mypage.php?id=7
makes $_GET = '7'

mypage.php?id=drop table mysql
makes $_GET = 'drop table mysql'

mypage.php
makes $_GET = ''


with that in mind the top lines makes sense

$id = $_GET;
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";

if it isn't set the query it is trying to run will be:
$mysql_query = "select `id` from `addrental` WHERE `id` = ";
otherwise it's
$mysql_query = "select `id` from `addrental` WHERE `id` = 1";

see by echoing it out

echo $mysql_query;

yeah it sounds like you're missing my post before last with setting the id in the url

mypage.php?id=5
makes $_GET = '5'

mypage.php?id=7
makes $_GET = '7'

mypage.php?id=drop table mysql
makes $_GET = 'drop table mysql'

mypage.php
makes $_GET = ''


with that in mind the top lines makes sense

$id = $_GET;
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";

if it isn't set the query it is trying to run will be:
$mysql_query = "select `id` from `addrental` WHERE `id` = ";
otherwise it's
$mysql_query = "select `id` from `addrental` WHERE `id` = 1";

see by echoing it out

echo $mysql_query;

so i need to pass the id in the url, here is the submit part of the selection form, is it here i need to echo the row id?

echo "<form action=\"villaview.php\" method=\"get\">";
             echo "<input class=\"button\" type=\"submit\" value = \"View\"/>";
             echo "<input type=\"hidden\" name=\"villa_search\" value =";
             echo "'$villa_search'";
             echo "/>";
           echo "</form>";

yeah, i don't know how that form is supposed to work but to get a pre-determined id through the form it would be like

$villa_id = 5;
echo "<form action=\"villaview.php\" method=\"get\">";
echo "<input class=\"button\" type=\"submit\" value = \"View\"/>";
echo "<input type=\"hidden\" name=\"id\" value='{$villa_id}'/>";
echo "<input type=\"hidden\" name=\"villa_search\" value='{$villa_search}'/>";
echo "</form>";

when i echo it out it says id 1, on all pages
select `id` from `addrental` WHERE `id` = 1

i changed the form to what you said,
do i need to change the mysql query to reflect that aswell
on the first line
ie

$id = $_GET;

to

$id = $_GET;

no the name attribute on a input determines how to access it and it is case sensitive
name=\"id\"


im not sure how you are running this but the page url in your address bar just needs to have a id=4 in it eg.


mypage.php?id=7
mypage.php?somevar=test&id=5

for example say you have a database of villas, 2 in costa del sol, 3 in rio de janeiro and 1 in blackpool
and each of them have a gallery of a few pictures each

<?php
$query = "SELECT * FROM `villas` ORDER BY `location` ASC";
$result = mysql_query($query);
echo '<table><tr><td>Location</td><td>Name</td><td>Gallery</td></tr>
';
while($villaA = mysql_fetch_assoc($result)){
echo "<tr><td>{$villaA['location']}</td><td>{$villaA['name']}</td><td><a href='gallery.php?id={$villaA['id']}'>View gallery</a></td></tr>\r\n";
}
echo '</table>';
?>
will show a table like
blackpool villa6 view gallery(gallery.php?id=6)
costa del sol villa1 view gallery(gallery.php?id=1)
costa del sol villa2 view gallery(gallery.php?id=2)
Rio de janeiro villa3 view gallery(gallery.php?id=3)
Rio de janeiro villa4 view gallery(gallery.php?id=4)
Rio de janeiro villa5 view gallery(gallery.php?id=5)

gallery.php being the page you already got

at the minute the url comes up with

villaview.php?id=&villa search

so it shows id but no number after the = .

sorry this is terrible , thanks for your help, but i think i will give up for the day.

Yeah it can get very painful when things don't run and you cant understand why, it still happens to me as well. If you stick to it, it starts to just become natural and you understand what each thing does.

it looks like the id isn't being pulled

from a query you mentioned earlier

$mysql_query = "select `id` from `table`";

that "select `id`" means that if you use mysql_fetch_assoc() the variable in the array will be called id

so

$mysql_query = "select `id` from `table`";
$result = mysql_query($mysql_query);
$mysql_array = mysql_fetch_assoc($result);

$villa_id = $mysql_array['id'];
echo "<form action=\"villaview.php\" method=\"get\">";
echo "<input class=\"button\" type=\"submit\" value = \"View\"/>";
echo "<input type=\"hidden\" name=\"id\" value='{$villa_id}'/>";
echo "<input type=\"hidden\" name=\"villa_search\" value='{$villa_search}'/>";
echo "</form>";

or if its a loop

$mysql_query = "select `id` from `table`";
$result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_assoc($result)){
$villa_id = $mysql_array['id'];
echo "<form action=\"villaview.php\" method=\"get\">";
echo "<input class=\"button\" type=\"submit\" value = \"View\"/>";
echo "<input type=\"hidden\" name=\"id\" value='{$villa_id}'/>";
echo "<input type=\"hidden\" name=\"villa_search\" value='{$villa_search}'/>";
echo "</form>";
}

hey ,
i think we're confused,
those two code snippets are from two different pages, the image directory which is on the main villa details page, and the second one is the submit form from the villa search function.
i would have posted all the code and pages but i thought it would be to much for people to read through it all to get to the one part we are looking at. i find it really hard to describe code so im sorry if this is hard because of me.

will try again tommorow

has anyone any idea how i can make this array work,

should i use msql_fetch_array instead of fetch assoc ? does fetch assoc work with numbers

should i use msql_fetch_array instead of fetch assoc ? does fetch assoc work with numbers

ok i fixed it thanks for your help everyone

$id = $row['id'];
if(!ctype_digit($id)){$id = 1;}//if id is not a number set it to 1
$mysql_query = "select `id` from `addrental` WHERE `id` = {$id}";
$mysql_result = mysql_query($mysql_query);
while($mysql_array = mysql_fetch_array($mysql_result)){



 $images_dir = "images/{$mysql_array['id']}/";
$thumbs_dir = "images/{$mysql_array['id']}/thumbnails/";
$thumbs_width = 150;
$images_per_row = 2;
$image_files = get_files($images_dir);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.