SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkboxes for multi select for delete option

    Hi,

    I wonder how to use checkboxes to multi select rows for deletion instead of having the standard delete link for every row like:
    <a href='deleteauthor.php?id=$id'>

    How is this done on the "display" page where you want to multi select for deletion and also how it is received on the "delete" page.

    Would be grateful for info about this.

    Thanks!

    Bjorn

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Give every checkbox the same name, then the values of the checked boxes will be passed in an array of that name.

    So in your form you put something like this

    HTML Code:
    <input type="checkbox" name="variable[]" value="box1" />
    <input type="checkbox" name="variable[]" value="box2" />
    <input type="checkbox" name="variable[]" value="box3" />
    <input type="checkbox" name="variable[]" value="box4" />
    Then in your receiving script you have an array containing the values of the selected checkbox.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your reply.

    I'm a beginner so I'm not sure I understand what you mean. Maybe you can help me fill in the blanks if I show you some code.

    Basically I'm just displaying rows of names where every row has it's own 'id' ($id)from the database table. At the moment each row is displaying the name and also a 'delete' link that will pass on the 'id'($id) of that row to the 'delete' file. And in the delete file I use $_GET['id'] to receive the 'id'.

    The while loop is displaying the names in the display file.

    I'm only showing part of the display file, but I think you know what I mean.

    Maybe you could fill in the blanks in these two files

    Display file:

    PHP Code:
    while ($author mysql_fetch_array($authors)){
     
    $id $author['id'];
    $name $author['name'];

    echo 
    "<td>$name</td><td><a href='deleteauthor.php?id=$id'>Delete</a></td>"
    Delete file:

    PHP Code:
    <?php 

    //DELETE AUTHOR
    //FIRST GET ID PASSED FROM DELETE LINK

    $id $_GET['id'];

    $sql "DELETE FROM author WHERE id='$id'";
    if (@
    mysql_query($sql)){
       echo 
    'Author deleted';
    } else {
       echo 
    'Unable to delete author' mysql_error();
    }
    ?>

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something along the lines of:

    PHP Code:
    if(isset($_POST['submit']))
    {
    $delcount count($_POST['variable']);
    if(
    $delcount 0)
    {
    $idlist implode(',',$_POST['variable']);
    $sql "DELETE FROM table WHERE id IN($idlist)";
    }
    }
    <
    form method="post" action="">
    <
    input type="hidden" name="submit" value="1">
    while (
    $author mysql_fetch_array($authors)){ 
    $id $author['id']; 
    $name $author['name'];
    echo 
    "<td>$name</td><td><input type="checkbox" name="variable[]" value="$id"></td>"
    Silly

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while ($author mysql_fetch_array($authors)){ 
       
    $id $author['id']; 
       
    $name $author['name']; 
       echo 
    '<tr><td>' $name '</td><td><input type="checkbox" name="delete[]" value="' $id '" /></td></tr>'
    // end of while 
    The above code put inside your form tags will produce a row in your table for each author, with a checkbox beside the name. I'm assuming that you'll use POST to send your form data.

    PHP Code:
    <?php 

    foreach($_POST['delete'] AS $id){ 
      
    $sql "DELETE FROM author WHERE id='$id'"
      if (@
    mysql_query($sql)){ 
       echo 
    'Author deleted'
      } else { 
       echo 
    'Unable to delete author' mysql_error(); 
      } 
    }
    ?>
    This code takes the array passed to it from the form and steps through each element one by one using the id number to delete that record from the database.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes View Post
    PHP Code:
    while ($author mysql_fetch_array($authors)){ 
       
    $id $author['id']; 
       
    $name $author['name']; 
       echo 
    '<tr><td>' $name '</td><td><input type="checkbox" name="delete[]" value="' $id '" /></td></tr>'
    // end of while 
    The above code put inside your form tags will produce a row in your table for each author, with a checkbox beside the name. I'm assuming that you'll use POST to send your form data.

    PHP Code:
    <?php 
     
    foreach($_POST['delete'] AS $id){ 
      
    $sql "DELETE FROM author WHERE id='$id'"
      if (@
    mysql_query($sql)){ 
       echo 
    'Author deleted'
      } else { 
       echo 
    'Unable to delete author' mysql_error(); 
      } 
    }
    ?>
    This code takes the array passed to it from the form and steps through each element one by one using the id number to delete that record from the database.
    The reason why I use IN is because I been told not to use sql statement in a loop. But to each their own. Both ways do the same thing.

    Silly

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for both your answers. I tried one of them and it worked and I'm sure the other one works too.

    Why is one not supposed to use SQL statements in a loop?

    Bjorn

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sillysoft View Post
    The reason why I use IN is because I been told not to use sql statement in a loop. But to each their own. Both ways do the same thing.

    Silly
    I totally agree, you should not really use a query inside a loop, for one it is asking for trouble if the loop goes astray, and secondly (and more importantly) its ineffiecient coding having to call the database multiple times. I chose to start this way as I felt it was the easiest way to show a beginner the concept of passing an array through a POST variable
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


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
  •