SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Unlink Groups of Files Based On ID

    I have two tables:

    Table: photo_gallery_cat
    phcat_id: 1
    category: Beautiful Estates

    phcat_id: 2
    category: Ugly Estates

    Table: photo_gallery
    pg_id: 1
    phcat_id: 1
    larg_img: lib_jog.jpg

    pg_id: 2
    phcat_id: 1
    larg_img: wea_sel.jpg

    What I would like to do is to be able to delete a set of images based upon a category selected from photo_gallery and also the ability to delete the category as well from the photo_gal_cat table.

    This what I have so far:
    PHP Code:
    $colname_getGallery "-1";
    if (isset(
    $_GET['phcat_id'])) {
      
    $colname_getGallery $_GET['phcat_id'];
            
    mysql_select_db(myconnectionstuff);
    $query_getGallery sprintf("SELECT photo_gallery_cat.category,  photo_gallery.pg_id, photo_gallery.phcat_id, photo_gallery.larg_img FROM photo_gallery JOIN photo_gallery_cat ON photo_gallery.phcat_id = photo_gallery_cat.phcat_id WHERE photo_gallery.phcat_id = %s"GetSQLValueString($colname_getGallery"int"));
    $getGallery mysql_query($query_getGallery$CVconn) or die(mysql_error());
    $row_getGallery mysql_fetch_assoc($getGallery);
    $totalRows_getGallery mysql_num_rows($getGallery);

    if (
    array_key_exists('cancel'$_POST)) {
        
    header('Location: list.php');
        exit;
    }

    if ((isset(
    $_POST['phcat_id'])) && ($_POST['phcat_id'] != "")) {
      
    $deleteSQL sprintf("DELETE FROM photo_gallery WHERE phcat_id=%s",
                           
    GetSQLValueString($_POST['phcat_id'], "int"));
                                                                                                
    unlink("../../photos/"$row_getGallery['larg_img']); 

      
    mysql_select_db($database_CVconn$CVconn);
      
    $Result1 mysql_query($deleteSQL$CVconn) or die(mysql_error());

      
    $deleteGoTo "delete_gallery_list.php";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $deleteGoTo .= (strpos($deleteGoTo'?')) ? "&" "?";
        
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$deleteGoTo)); 

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This demo will delete the image files from disk and the associated category records from the category and images tables.

    I haven't tested the code, but you should in general see what I am doing.

    Code:
     
    <?php
    $catId = $_GET['phcat_id'];
     
    $isCatDeleted = deleteCat($catId);
     
    function deleteCat($catId) {
    $catId = mysql_real_escape_string($catId,$conn);
     
    //delete the image files from disk
    $query = 'select larg_img from photo_gallery where phcat_id = "'.$catId .'"';
    $rs = @mysql_query($query,$conn);
    if(!rs) return false;
    while($row=mysql_fetch_assoc($rs)) {
          if(file_exists($row['larg_img'])) {
                unlink($row['larg_img']);
          }
    }
    @mysql_free_result($rs);
     
    //now delete the records in the 2 tables
    $query = 'delete from photo_gallery where phcat_id = "'.$catId .'"';
    if(!mysql_query($query,$conn)) return false;
    $query = 'delete from photo_gallery_cat where phcat_id = "'.$catId .'"';
    if(!mysql_query($query,$conn)) return false;
    return true;
    }
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks for your time, Kalon. I'll look into this!

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope you got the latest code I posted.

    I wrapped the original code in php code tags and got some weird "email=......" next to some of the @ chars.

    I reposted the code wrapped in [code] tags and it looks ok now.

    If you need more help, post back

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw - I was assuming you only have 1 layer of categories in your data model.

    If you have subdirectories as well then obviously you would also need to delete the images in all the sub directories of the category selected for deletion and the sub-directories as well (unless you promote them all by 1 level in your tree structure)

    Deleting sub directories and their images is a simple task if you are using a nested set model as your data model.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •