Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2012, 03:14 AM   PM User | #1
gippu
New to the CF scene

 
Join Date: Mar 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
gippu is an unknown quantity at this point
PHP image galley hw help

Howdy. Anyone want to help a newbie with some HW!? I'm currently enrolled in a introductory web programing course and I'm having some minor issues with this assignment about php image galleries using array. The assignment is as follows ;

- Do not modify the data in data.php.
-The index file should create a thumbnail gallery with the appropriate caption (title) under each image. Each thumbnail should link to "detail.php" and pass the ID number of the image (which is the index of the array).
-Detail.php should show the larger image and display title, description and year.


-I think I got it up and running, although I can't get my images to display
in the browser, thoughts?
-Also, would you say this setup matches the criteria ?

The three .php files are as follows ;

Code:
index.php :
<? include("data.php"); ?>
<? foreach($imagedata as $id => $image) { ?>
	  <div class="thumbnail">
        <a href="detail.php?f=<? echo $image; ?>">
        <img src="thumbs/<? echo $image; ?>" alt="A kitten" />
        </a>
		<? echo "<p>" . $image["title"];   ?>
		</div>
        
         <? } ?>
Code:
data.php
<?

	$imagedata = array();
	
	$imagedata[0] = array(
		"title" 		=> "Kittens in Cups",
		"filename" 		=> "kitten1.jpeg",
		"description" 	=> "They're in cups! Adorable.",
		"year_taken" 	=> "2006",
	);

	$imagedata[1] = array(
		"title" 		=> "Pink Kitten",
		"filename" 		=> "kitten2.jpeg",
		"description" 	=> "Warning: This may be too sweet for you.",
		"year_taken" 	=> "1999",
	);

	$imagedata[2] = array(
		"title" 		=> "Sniper kitten",
		"filename" 		=> "kitten3.jpeg",
		"description" 	=> "Do not mess with her.",
		"year_taken" 	=> "2010",
	);

	$imagedata[3] = array(
		"title" 		=> "Meow",
		"filename" 		=> "kitten4.jpeg",
		"description" 	=> "A kitten",
		"year_taken" 	=> "2012",
	);

	$imagedata[4] = array(
		"title" 		=> "Kitten and Parrot",
		"filename" 		=> "kitten5.jpeg",
		"description" 	=> "Old-timey engraving of a kitten with a parrot.",
		"year_taken" 	=> "1896",
	);

	$imagedata[5] = array(
		"title" 		=> "Three White Kittens",
		"filename" 		=> "kitten6.jpeg",
		"description" 	=> "Illustration from the cover of sheet music.",
		"year_taken" 	=> "1912",
	);

?>
Code:
detail.php ;

<? include("data.php"); ?>

<?
	
	$filename = $_GET["f"];

?>

    
    <img src="images/<? echo $filename; ?>" alt="A kitten" />

    <p><a href="index.php">Back to gallery</a></p>
Thanks!! ^^
gippu is offline   Reply With Quote
Old 03-31-2012, 05:11 AM   PM User | #2
Zangeel
Regular Coder

 
Zangeel's Avatar
 
Join Date: Oct 2007
Location: public_html/
Posts: 638
Thanks: 17
Thanked 79 Times in 79 Posts
Zangeel will become famous soon enough
Let's take a step back here! It's nice you're learning this versatile language but to maximize your ability, start off with the very basics, clean code, and up to date code. For example using shorthand php tags (<?) is unwise, if you were to import that on a server which didn't allow shorthand tags, imagine, a large website having to find all of the shorthand tags and replace them with the standard <?php ?>.

Second, I just think it's better practice to choose are you going to use double or single quotes, double quotes don't need concatenation for variables, in other words:

PHP Code:
echo "Hello $sUsername"
Works just the same as

PHP Code:
echo 'Hello ' $sUsername
So to concatenate double quotes is extraneous.

One of your problems in the code right from the get-go is you're trying to echo an array, which isn't really possible you want the keys/values from the array not the array itself! so, echo $image; won't work, because it's simply an array. You also don't need to use the $key => $value structure in the foreach array, $imagedata as $image is just fine. I've altered your index.php as per these recommendations and it works fine for me.

PHP Code:
<?php include('data.php'); ?>

<div class="thumbnail">
    <?php
     
foreach ($imagedata as $image)
{

    echo 
'<a href="detail.php?f=' $image['filename'] . '">';
    echo 
'<img src="thumbs/' $image['filename'] . '" alt="A kitten" /></a>';
    echo 
'<p>' $image["title"] . '</p>';
}
    
?>
</div>
Viola, nice running clean code. However although this works, it does not meet the requirements of your homework, as the criteria for the url argument was not the file name although it works but rather the id, which would be like 0,1,2,3 - as per it's position in the array.

The solution is easy, and will require you to use $key => $value in the foreach like you had before Then echoing the $id value in the <a> tags.

Then there will be a little modifying to do on the display page; nothing you can't figure out, try it out.
__________________
PHP Code:
$aString is_string((string)array()) ? true false// true :D 
[/CENTER]
Zangeel is offline   Reply With Quote
Users who have thanked Zangeel for this post:
gippu (03-31-2012)
Old 03-31-2012, 05:37 AM   PM User | #3
gippu
New to the CF scene

 
Join Date: Mar 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
gippu is an unknown quantity at this point
Zangeel, thank you very much for the your help, greatly appreciated ^^!
gippu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:12 AM.


Advertisement
Log in to turn off these ads.