PDA

View Full Version : PHP Checkboxes, sort of thing



JonnyH
June 15th, 2008, 05:15
This one has really stumped me. I'm creating a mutiple subdomain deleter for the cP Creator v2.5 but it doesn't delete ATM. Here's the code:

<?php
case "del":
echo $this->_split();
$this->tinymce("textareas","75&#37;","200px");
?>
<h1>Delete Subdomain</h1>
<%ERRORS%>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<form action="" method="post">
<?php
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
$form = explode(".", $row['domain']);
?>
<tr>
<td width="25%" valign="middle"><?php echo $row['domain']; ?></td>
<td><input name="<?php echo $form[0] ?>" id="<?php echo $form[0] ?>" type="checkbox" value="1" class="form" /></td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="Delete Subdomains" class="button" /></td>
</tr>
</form>
</table>
<?php
if($_POST) {
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
$form = explode(".", $row['domain']);
if($_POST[$form[0]] == "1") {
$query = mysql_query("DELETE FROM `".$this->sql['pre']."subdomains` WHERE `domain` = '". $row['domain'] ."'", $this->sql['con']);
$n++;
}
}
if(!$n) {
$n = "0";
}
$this->errors($n ." subdomains was deleted successfully!");
$this->done();
}
break;?>
The thing is, when you select more than one, it selects the one which is first in the DB and then leaves out the rest.

Thanks in advance,
Jonny

JohnN
June 15th, 2008, 07:51
<?php
case "del":
echo $this->_split();
$this->tinymce("textareas","75&#37;","200px");
?>
<h1>Delete Subdomain</h1>
<%ERRORS%>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<form action="" method="post">
<?php
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
$form = explode(".", $row['domain']);
?>
<tr>
<td width="25%" valign="middle"><?php echo $row['domain']; ?></td>
<td><input name="del<?php echo $form[0] ?>" id="<?php echo $row['domain']?>" type="checkbox" value="1" class="form" /></td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="Delete Subdomains" class="button" /></td>
</tr>
</form>
</table>
<?php
if($_POST) {
$query = '1=2';
$n=0;
foreach($_POST as $key=>$content){
if(strpos('del',$key) !== false){
$query .= " OR `domain` = '".$_POST[$key]."'";
++$n;
}
}

mysql_query("DELETE FROM `".$this->sql['pre']."subdomains` WHERE $query", $this->sql['con']);

$this->errors($n ." subdomains was deleted successfully!");
$this->done();
}
break;?>

themoose
June 15th, 2008, 13:37
I'd take JohnN's one step further.


<?php
case "del":
echo $this->_split();
$this->tinymce("textareas","75&#37;","200px");
?>
<h1>Delete Subdomain</h1>
<%ERRORS%>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<form action="" method="post">
<?php
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
$form = explode(".", $row['domain']);
?>
<tr>
<td width="25%" valign="middle"><?php echo $row['domain']; ?></td>
<td><input name="del[<?php echo $form[0] ?>" id="<?php echo $row['domain']?>]" type="checkbox" value="1" class="form" /></td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="Delete Subdomains" class="button" /></td>
</tr>
</form>
</table>
<?php
if($_POST) {
$query = '1=2';
$n=0;
foreach($_POST as $key=>$content){
if(strpos('del',$key) !== false){
$query .= " OR `domain` = '".$_POST[$key]."'";
++$n;
}
}

mysql_query("DELETE FROM `".$this->sql['pre']."subdomains` WHERE $query", $this->sql['con']);

$this->errors($n ." subdomains was deleted successfully!");
$this->done();
}
break;?>

$_POST['del'] is now an array, $_POST['del'][whateverFormOwas] would equal the value.

JonnyH
June 16th, 2008, 05:21
Both methods which you posted didn't work but I tweaked them to make 'em work. Thanks for the help guys. Working Code:

<?php
case "del":
echo $this->_split();
$this->tinymce("textareas","75%","200px");
?>
<h1>Delete Subdomain</h1>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<form action="" method="post">
<?php
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
?>
<tr>
<td width="25%" valign="middle"><?php echo $row['domain']; ?></td>
<td><input name="<?php echo $row['domain']?>" id="<?php echo $row['domain']?>" type="checkbox" value="1" class="form" /></td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="Delete Subdomains" class="button" /></td>
</tr>
</form>
</table>
<?php
if($_POST) {
$n=0;
foreach($_POST as $key=>$content){
$data = explode("_", $key);
if($content == "1"){
$sql = "DELETE FROM `".$this->sql['pre']."subdomains` WHERE `domain` = '{$data[0]}.{$data[1]}'";
mysql_query($sql, $this->sql['con']);
++$n;
}
}

$this->errors($n ." subdomains was deleted successfully!");
$this->done();
}
break;
?>

JohnN
June 16th, 2008, 07:38
just glad we could help with such a worthwhile project:)

iBrightDev
June 16th, 2008, 09:01
Both methods which you posted didn't work but I tweaked them to make 'em work. Thanks for the help guys. Working Code:

<?php
case "del":
echo $this->_split();
$this->tinymce("textareas","75%","200px");
?>
<h1>Delete Subdomain</h1>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<form action="" method="post">
<?php
$query = mysql_query("SELECT * FROM ".$this->sql['pre']."subdomains",$this->sql['con']);
while($row = mysql_fetch_array($query)) {
?>
<tr>
<td width="25%" valign="middle"><?php echo $row['domain']; ?></td>
<td><input name="<?php echo $row['domain']?>" id="<?php echo $row['domain']?>" type="checkbox" value="1" class="form" /></td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="Delete Subdomains" class="button" /></td>
</tr>
</form>
</table>
<?php
if($_POST) {
$n=0;
foreach($_POST as $key=>$content){
$data = explode("_", $key);
if($content == "1"){
$sql = "DELETE FROM `".$this->sql['pre']."subdomains` WHERE `domain` = '{$data[0]}.{$data[1]}'";
mysql_query($sql, $this->sql['con']);
++$n;
}
}

$this->errors($n ." subdomains was deleted successfully!");
$this->done();
}
break;
?>

that is what i was going to post. ;) lol, j/k, i didnt get to help since i just now saw this. :( hope the new project is coming together well Jonny. good luck with it.

JonnyH
June 16th, 2008, 10:51
New project?
Was for v2.5 which has just been released.
Thanks though MCT =]