Hello everyone.
I ripped off a tutorial script for a picture gallery, and modifed bits and pieces. I can't get this bit to work right though.
Code:
<?
include("config.inc.php");
/********************************************************************/
// initialization
$result_array = array();
$counter = 0;
$cid = (int)($_GET['cid']);
$pid = (int)($_GET['pid']);
// Category Listing
if( empty($cid) && empty($pid) )
{
$number_of_categories_in_row = 4;
$result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id)
FROM gallery_category as c
LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
GROUP BY c.category_id" );
while( $row = mysql_fetch_array( $result ) )
{
/* Display categories. */
$result_array[] = "<a href='gallery_view.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
}
mysql_free_result( $result );
$result_final = "<tr>\n";
foreach($result_array as $category_link)
{
if($counter == $number_of_categories_in_row)
{
$counter = 1;
$result_final .= "\n</tr>\n<tr><td colspan=4> <br> </td></tr>\n<tr>\n";
}
else
$counter++;
/* Display each category and button to delete. */
$result_final .= "\t<td><font>".$category_link."<br>";
if($result_array[2] > 0){
$result_final .= "<form action=gallery_cat_del.php method=post>";
$result_final .= "<input type=hidden name=catid value=".$result_array[0].">";
$result_final .= "<input type=submit value=Delete>";
$result_final .= "</form>";
}
$result_final .= "</td>\n";
}
if($counter)
{
if($number_of_categories_in_row-$counter)
$result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n";
$result_final .= "</tr>";
}
}
// Thumbnail Listing
else if( $cid && empty( $pid ) )
{
$number_of_thumbs_in_row = 5;
$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" );
$nr = mysql_num_rows( $result );
if( empty( $nr ) )
{
$result_final = "\t<tr><td><font>No Category found</td></tr>\n";
}
else
{
while( $row = mysql_fetch_array( $result ) )
{
$result_array[] = "<a href='gallery_view.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
}
mysql_free_result( $result );
$result_final = "<tr>\n";
foreach($result_array as $thumbnail_link)
{
if($counter == $number_of_thumbs_in_row)
{
$counter = 1;
$result_final .= "\n</tr>\n<tr>\n";
}
else
$counter++;
$result_final .= "\t<td><font>".$thumbnail_link."</td>\n";
}
if($counter)
{
if($number_of_photos_in_row-$counter)
$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n";
$result_final .= "</tr>";
}
}
}
// Full Size View of Photo
else if( $pid )
{
$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
$nr = mysql_num_rows( $result );
mysql_free_result( $result );
if( empty( $nr ) )
{
$result_final = "\t<tr><td><font>No Photo found</td></tr>\n";
}
else
{
$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
list($category_name) = mysql_fetch_array( $result );
mysql_free_result( $result );
$result_final .= "<tr>\n\t<td><font>
<a href='gallery_view.php'>Categories</a> >
<a href='gallery_view.php?cid=$cid'>$category_name</a></td>\n</tr>\n";
$result_final .= "<tr>\n\t<td align='center'><font>
<br />
<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
<br />
$photo_caption
<br />
</td>
</tr>";
}
}
/******************************************/
/* Output page. */
include("includes/pagetop.html");
echo '<br><br><br>';
echo '<center><a href="control.php">Back to Control Panel</a></center>';
echo '<p>';
// Final Output
echo <<<__HTML_END
<center><table width='90%' border='0' align='center'>
$result_final
</table></center>
__HTML_END;
include("includes/pagebottom.html");
?>
And this is the file it is sent to for the deletion:
Code:
<?
include("config.inc.php");
include("includes/session.php");
if($session->logged_in){
if($session->isAdmin()){
echo '<center><font>';
// If logged in as admin...
/*************************************************/
function delete_category( $category_id )
{
global $images_dir;
$result = mysql_query( "SELECT photo_filename FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );
while( $row = @mysql_fetch_array( $result ))
{
unlink($images_dir."/".$row[0]);
}
mysql_query( "DELETE FROM gallery_photo WHERE photo_category='".addslashes( $category_id )."'" );
mysql_query( "DELETE FROM gallery_category WHERE category_id='".addslashes( $category_id )."'" );
}
include("includes/pagetop.html");
if(!delete_category($_REQUEST['catid'])){
echo '<font>';
echo '<center>';
echo '<h1>Failure!</h1>';
echo '<p>';
echo '<center>The gallery editing page will reload in a moment.';
echo '<meta http-equiv="refresh" content="1;url=mydomain/gallery_delete.php">';
}
else{
echo '<font>';
echo '<center>';
echo '<h1>Success!</h1>';
echo '<p>';
echo '<center>The gallery editing page will reload in a moment.';
echo '<meta http-equiv="refresh" content="1;url=mydomain/gallery_delete.php">';
}
include("includes/pagebottom.html");
/***************************************************/
}
else{
echo '<center><font>';
include("includes/pagetop.html");
include("includes/hidden.php");
include("includes/pagebottom.html");
}
}
else{
include("includes/pagetop.html");
include("includes/hidden.php");
include("includes/pagebottom.html");
}
?>
The part I added for deleting stuff is:
Code:
/* Display each category and button to delete. */
$result_final .= "\t<td><font>".$category_link."<br>";
if($result_array[2] > 0){
$result_final .= "<form action=gallery_cat_del.php method=post>";
$result_final .= "<input type=hidden name=catid value=".$result_array[0].">";
$result_final .= "<input type=submit value=Delete>";
$result_final .= "</form>";