<?php
final class AcceptReject
{
private $checked = array( );
private $options = array( );
private $accept = null ;
private $reject = null ;
private $sql = array( );
public function __construct( $checked = array( ), $options = array( ), $accept = null, $reject = null )
{
$this->checked = $checked ;
$this->options = $options ;
$this->accept = $accept;
$this->reject = $reject;
if( $this->accept and $this->reject and $this->options and $this->checked )
{
foreach( $this->checked as $id => $check )
{
if( $check )
{
if( $this->options[$id] >= 0 )
{
if( $this->options[$id] )
{
$this->sql[] = sprintf( $this->accept, $id );
}
else $this->sql[] = sprintf( $this->reject, $id );
}
}
}
}
}
public function setChecked( $checked )
{
return ( $this->checked = $checked );
}
public function setOptions( $options )
{
return ( $this->options = $options );
}
public function setAccept( $accept )
{
return ( $this->accept = $accept );
}
public function setReject( $reject )
{
return ( $this->reject = $reject );
}
public function getChecked( )
{
return $this->checked ;
}
public function getOptions( )
{
return $this->options ;
}
public function getAccept( )
{
return $this->accept ;
}
public function getReject( )
{
return $this->reject ;
}
public function hasSql( )
{
if( count( $this->sql ) )
{
return true ;
}
else if( $this->accept and $this->reject and $this->options and $this->checked )
{
foreach( $this->checked as $id => $check )
{
if( $check )
{
if( $this->options[$id] >= 0 )
{
if( $this->options[$id] )
{
$this->sql[] = sprintf( $this->accept, $id );
}
else $this->sql[] = sprintf( $this->reject, $id );
}
}
}
if( count( $this->sql ) )
{
return true;
}
}
else return false;
}
public function getSql( )
{
return sprintf
(
"%s;\n",
implode( ";\n", $this->sql )
);
}
}
if( $_POST )
{
$AcceptReject = new AcceptReject
(
$_POST['checked'],
$_POST['options'],
"UPDATE table SET column = '1' WHERE id = '%d'",
"DELETE FROM table WHERE id = '%d' LIMIT 1"
);
if( $AcceptReject->hasSql( ) )
{
printf("<pre>%s</pre>\n", $AcceptReject->getSql( ) );
}
else printf("<pre>No SQL generated by AcceptReject object</pre>\n");
}
for( $i = 0; $i < 255; $i += 3 )
{
$fakeids[]=$i;
}
?>
<form action="" method="POST">
<?php foreach( $fakeids as $id ): ?>
<p>Comment <?=$id ?><br />
<label><input type="checkbox" name="checked[<?=$id ?>]" value="1"/> Check this box to moderate</label>
<br />
<select name="options[<?=$id ?>]">
<option value="-1"></option>
<option value="1">Accept</option>
<option value="0">Reject</option>
</select></p>
<?php endforeach; ?>
<input type="submit" value="Moderate Comments" />
</form>