0

I am fairly new to php
but i do have some knowledge of it
im justnot sure how to code what i want.
if anyone can give me some idea of what code i need i'd really appriciate it :)

anyway


photo names will be uploaded to database with a catagory id "live music", "landscapes", "custom names".
a flah file will read an xml file which will then start echoing the correct data for the flash file to present.
i need it to go somthing like "if the catagory id = live music
echo all photos in that album with the catagory id then do the same for each one.

i have this so far

<gallery title="Tony's Photo Album" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name='Live Music'>
<?php
	include ("include/connect.php");
	$query = "SELECT * FROM photo ORDER BY id DESC"; 
	$result = mysql_query ($query)  or die ("Error in query: $query. ".mysql_error());
	
	   
   while ($row = mysql_fetch_array($result))  
   {                                       
         echo  "
		<image>
			<date>".$row['date']."</date>
			<title>".$row['title']."</title>
			<desc>".$row['description']."</desc>
			<thumb>".$row['thumb']."</thumb>
			<img>".$row['image']."</img>
		</image>
		
	";


   }                                                                             
   		 ?>
</category>
</gallery>
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by AdriftUniform
0

To display multiple images you have you use a secondary page which echo's out 1 image, and then use a while loop in the actual display page to ask for each individual image.

So first of all you need the page that will get the image from the database, because of the way php display images it will only be able to display the image file and nothing else. Here is the code for this page lets call it "getimage.php":

<?php

//get the image and category id/name from the display page
if($_GET['id']) && is_numeric($_GET['id']) && $_GET['category']) && is_numeric($_GET['category'])) 
{
    $query = mysql_query("SELECT image FROM photo WHERE id={$_GET['id']} AND  category= $_GET['category']}");
    header('Content-type: image/jpg');
    echo mysql_result($query, 0);
}
else
{
    echo 'Please use a real id number';
}
?>

This code above uses the $_GET variable to define the image and category that the user wants to view, this is sent to "getimage.php" through a link on your display page. This code then selects 1 image specified by ID that is within the specified category and echoes it.

Now we need to create the display page that will use a while loop to cycle through all images within that category and display them, lets call this "displayimage.php".

<?php
if(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $category = $_GET['id'];
}
echo "<br /><br />";
print "Category : " . $category . "<br />";

$sql = "SELECT id FROM photo WHERE category='$category'";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
while($row = mysql_fetch_assoc($result))
    {
    print '<img src="getimage.php?id=' . $row['id'] . '&amp;category=' . $category . '"/>';
    print "<br />";
    }
?>

This code first gets the category ID that you want from the url you have clicked on, I assume that you will have an "category.php" page that will have a list of all the categories available for the user to choose from, if so you will need to link to each category as so:

<a href="displayimage.php?id=1">Category1</a>
<a href="displayimage.php?id=2">Category2</a>
<a href="displayimage.php?id=3">Category3</a>
<a href="displayimage.php?id=4">Category4</a>

As you can see here I am putting the variable id in the URL and setting it a numeric value which is the same as the database ID for each category.

So going back to "displayimage.php" this uses the $_GET variable to get the category id that you want to view, it then sets this to $category for later use.

Next we query the database so that all image ID's are selected that are in the specified category, and using a while loop we print out:

'<img src="getimage.php?id=' . $row['id'] . '&amp;category=' . $category . '"/>';

As the while loop runs $row changes to the ID's of each image in the category, and the $category variable does not change as it is a set value.

I have tried to make the code suit your database names and page names the best I can, I hope this helps.

AdriftUniform.

Edited by AdriftUniform: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.