Creating an Event System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DarkLightning
Forum Newbie
Posts: 3
Joined: Tue Jul 01, 2003 3:51 am
Location: Washington State
Contact:

Creating an Event System

Post by DarkLightning »

I have tried for a while with no success to get this to work and I have tried countless methods and ideas, but I'm hitting roadblocks that are stopping my ideas.

Here is the situation:

I am trying to create a system for adding and deleting events, etc. I have the adding part down fine, but the script for deleting events that I am trying to make is more complicated.

I have the script display the events that meet the criteria that you set and I want there to be a checkbox by each one so you can check which events you want to delete. I have that part down. The part I am having trouble with is having the script delete multiple events that are checked. What I am trying to do is send the values of the check boxes and everything else in the form to another page where I would retrieve the info and have it delete the checked events from the previous page. I don't know if there is a better way to do this, or any way to do it. I'm hitting a blank wall. I'm sure there must be some way to do it, but I would appreciate it if someone could point me in the right direction. If I wasn't clear enough or you need clarification, I will be happy to explain the best I can, of even give you the link to the page I am working on if it would help.

Thanks in advance.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Name the checkboxes del[] ie:

Code: Select all

<input type="checkbox" name="deleteї]" value="$id">
Put in the value part the UNIQUE key.

Then the code sould sound like:

Code: Select all

if(isset($_POST["delete"])){
  for($t1=0;$t1<sizeof($_POST["delete"]));$t1++){
    $query = "DELETE FROM `table` WHERE `id`='".$_POST["delete"][$t1]."'";
    if(!mysql_query($query)){
      echo mysql_error()."\n";
      echo $query;
      exit;
    }
  }
}
DarkLightning
Forum Newbie
Posts: 3
Joined: Tue Jul 01, 2003 3:51 am
Location: Washington State
Contact:

Thank you

Post by DarkLightning »

Thanx for your help. After correcting two slight errors in your script, it works now. Thank you so much.

Maybe you could help me with another question I had. If not, don't worry about it. But, is there an easy way to create a query that will select all events that match your search options taking into account the fact that if a field is left blank, then it should ignore the field. For example.

If I leave the month blank and select the day as the 28th, it would know to search for all events in any month that occur on the 28th. Or if I leave all the fields blank except for the year. You would show all events for that year. I can do this if I make some 12 if-then statements for every situation, but that is definitely not practical. Do you know of an alternative?
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

[]InTeR[] wrote:

Code: Select all

if(isset($_POST["delete"])){
  for($t1=0;$t1<sizeof($_POST["delete"]));$t1++){
    $query = "DELETE FROM `table` WHERE `id`='".$_POST["delete"][$t1]."'";
    if(!mysql_query($query)){
      echo mysql_error()."\n";
      echo $query;
      exit;
    }
  }
}
?>
Personally I would:

Code: Select all

<?php
if(isset($_POST['delete'])){
  foreach ($_POST['delete'] as $DeleteID){
    $whereCondition .= ($whereCondition != '' ? ' or' : '') . ' id= '''.addslashes($DeleteID).'''';
  }
  mysql_query('delete from table WHERE ' . $whereCondition) or die('Deletion query error: ' . mysql_error());
}
?>
I think it looks less messy then, and it makes one query instead of one for each event. :roll:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Heavy wrote:it makes one query instead of one for each event. :roll:
You can then use mysql_affected_rows() to check that the number of records you expected to have been deleted have been.

Mac
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

DarkLightning, I spent some time with checkboxes. You might want to check the result out:
Post Reply