PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 21st, 2003, 03:57 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
delete posts checkboxes & PHP

Okay I am trying to make some posts deletable with checkboxes. I have already set up the page so it does a while loop and displays the post, and I even set it up with individual links for deletion where it posts the id through the url, but I want to let users be able to check all and then delete them. Can this be done with PHP easily? It seems difficult since the page is doing a loop, so it is basically getting information, posting it, and then replacing that same info with new info to post, thus when I use the $_POST check it displays the last info retrieved. Any ideas... i tried arrays but I'm new to those and they don't seem to work...
thanks
__________________
hey it's the CHARKING

Reply With Quote
  #2  
Old July 21st, 2003, 07:03 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Any time I've done this, I've named the checkbox something like delete_00001, where 00001 is the id number of the given post. If no other fields are named delete_<number>, you can loop through the $_POST array and, for any field name that has the format delete_<number>, do a preg_replace (converting "delete_" to "") and issue a delete statement for the id in question. You'll want to be careful about validating user input here, though, lest somebody just send you a big POST or GET that maliciously deletes all your data. Maybe check your referer or run intval() on each id before executing the query to make sure there's no extra stuff being passed. You might also consider limiting the number of deletions that can be performed at once to the number of items displayed per page as an extra precaution. Something else you might consider is not deleting, but adding a flag to your database that you set to false if somebody performs a deletion.

Reply With Quote
  #3  
Old July 21st, 2003, 12:05 PM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
how can I do this? I can't figure out how to loop through results that are made dynamically like that... this is my code...

PHP Code:
<form action="index.php?page=news" method="post">
<?
PHP 

// Configuration part 
$dbhost "localhost"// Database host 
$dbname "users"// Database name 
$dbuser "dbuser"// Database username 
$dbpass ""// Database password 

$db mysql_connect($dbhost$dbuser$dbpass) or die("Error: Couldn't connect to database"); 
mysql_select_db($dbname$db) or die("Error: Couldn't select database."); 

//trying to figure out what to do with the post...
if($_POST)
    { echo 
$_POST['chkDelete'];
    exit; }

$query "SELECT * FROM tag"
$tag mysql_query($query); 

while(
$tagRow mysql_fetch_array($tagMYSQL_ASSOC)) { 
$tags[] = $tagRow['tag']; 
$replaceTag[] = $tagRow['replace'];
$spacer[] = ' ';
$liner[] = '_';

?>

<?php

include("secure.php");        
            
if(
$sessionid $userSess)
    { if(
$userLvl == 5)
        { 
?>
<center>
<a href="index.php?page=addnews&what=news">Add News</a> || 
<a href="index.php?page=addnews&what=article">Add Article</a>
</center>
<?php } } ?>

<tr><td width="100%" bgcolor="#66B4F4"><center>News</center></td></tr>

<?php
    
$newsRow 
= @mysql_query("SELECT * FROM news ORDER BY date DESC LIMIT 5") or die(mysql_error());
while (
$nRow mysql_fetch_row($newsRow))
$nPost $nRow[4];

//an earlier way of deleting the data, through a link
if($_GET['strMethod'] == 'conDeletePost')
{ if(
$_GET['post'])
    { 
$deletePost mysql_query("REMOVE * FROM news WHERE newsId = " $_GET['post'] . "");
    } } else {
    if(
$_GET['strMethod'] == 'deletepost')
  {    
$conDeletePost mysql_query("SELECT * FROM news WHERE newsId = " $_GET['post'] . "");
    while(
$conDP mysql_fetch_row($conDeletePost))
 {    echo 
"delete news post " $conDP[3] . "?";
    echo 
'<a href="index.php?page=news&strMethod=conDeletePost&post=' $_GET['post'] . '">Delete</a>  ||';
    echo 
'<a href="index.php?page=news">Back</a>'; } } else { ?>

<tr><td><table width="100%" border="0">
  <tr> 
 <td width="100%"><font size="2">
 
<?php
$nPoster 
= @mysql_query("SELECT * FROM users WHERE userId ='" $nRow[2] . "'") or die(mysql_error());
while (
$nName mysql_fetch_row($nPoster))
 {  
$date1 $nRow[1];
     
$date2 date (substr_replace (substr (substr ($date1,0,2).chunk_split (substr ($date1,2,6),2,"-").chunk_split (substr ($date1,8),2,":"),0,19)," ",10,1));
?>
 
    <tr><td width="100%" bgcolor="#66B4F4">
    <center><font size="1"><?php echo $nRow[3]; ?>
    </font></center></td></tr>
    <tr><td><font size="1">
    //USER <?php echo $nName[1]; ?> HAS LOGGED ON @ <?php echo $date2?><br>
    LISTENING_TO:<?php echo $nRow[4]; ?>/<?php echo $nRow[5]; ?><br>
    BEGIN_TRANSMISSION:<br><br>
            
 <?php
$newsOut 
nl2br($nRow[6]);
 echo 
str_replace($tags$replaceTag$newsOut); ?>
 <br><br></font></td></tr></font>

 <tr><td width="100%" bgcolor="#66B4F4" vlink="yellow"><font size="1">
 <a href="index.php?page=viewprofile&userid=<?php echo "$nName[0]"?>">
 <?php echo "$nName[1]"?></a>
 
  @
  
  <?php 
    
echo $date2;
  if(
$details[0] == $nName[0])
  { 
$postId $nRow[0]; ?>
 || 

<!--here i added the checkboxes...-->
 <a href="index.php?page=news&strMethod=editpost&post=<?php echo $nRow[0]; ?>">Edit Post</a> ||
<input name="chkDelete" type="checkbox" value="delete_<?php echo $postId?>">
<a href="index.php?page=news&strMethod=deletepost&post=<?php echo $nRow[0]; ?>">Delete Post</a>

<?php } } ?>
</font></td></tr></table></td></tr>
<?php } } } ?>
<tr>
    <td>
    Check All || 
<!--here the submit button...-->
      <input name="ckDeleteAll" type="submit" value="Delete Checked">
    </td>
  </tr></form> 


thank you for you quick reply, I will try to figure this one out on this end still, but I jsut don't know what to do next.

Last edited by thecharking : July 21st, 2003 at 12:07 PM.

Reply With Quote
  #4  
Old July 21st, 2003, 12:36 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
To loop through your POST vars by name, try something like the following:

PHP Code:
<?php

$queries
=array();

while(list(
$key,$val)=each($_POST)){
    if(
preg_match("/^delete_/i",$key)){
      
array_push($queries,"DELETE FROM table WHERE id=" intval(preg_replace("/delete_/i","",$key)));
    }    
}

?>


The code looks at each POST variable and checks to see if the field name begins with "delete_." If it does, it pushes a query onto the $queries array for use later. To build the query, we run the preg_replace() function, getting rid of the "delete_" and making sure we're working with an integer. In this example, it doesn't matter what value you give the checkbox. The checkbox will only be passed to the $_POST array if it's checked.

Reply With Quote
  #5  
Old July 21st, 2003, 12:56 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Note that you could probably also just name your checkbox for each entry the same thing, with brackets at the end of the name so it's treated like an array, and then do a foreach on that field. So:

PHP Code:
<?php
$queries
=array();

//Assumes your field name is "deleted[]"
if(is_array($_POST["deleted"])){
    foreach(
$_POST["deleted"] as $d){
        
array_push($queries,"DELETE FROM table WHERE id=" intval($d));
    }
}

?>

Reply With Quote
  #6  
Old July 21st, 2003, 02:21 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Take a look at this sample which will display an array with enough information to create the proper delete sql statement after you check some boxes and press the delete button.

As a side note, dhouston made a comment about just setting a flag as opposed to actually deleting... I usually never delete anything unless there are compelling reasons to do so.
Attached Files
File Type: zip test.zip (681 Bytes, 510 views)
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #7  
Old August 20th, 2003, 03:05 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
i know it's been a while but...

Okay I'm sorry I didn't post this earlier, (after you gave the help), it has been a while. I am still having trouble with this! I can't seem to get it to work. I am really not very familiar with array's... and I don't want to be a bother, but can you explain how I can get each part of the delete array to be deleted? I hope this question isn't too old. I just decided to move on to less frustrating things when I couldn't get it initially; I thought I would jsut come back later and figure it out; but I stillc can'tfigure it out. Hope you can help. thank you very much.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > delete posts checkboxes & PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT