• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP Checkboxes and drop down menu

TechDudeDan

New Member
Hi,

I have a PHP script which allows you to accept/reject user submitted guestbook comments. Now you select the comment by clicking on the checkbox next to it and you accept/reject it by selecting the action by using a drop down box, now how would I get it so once you press the submit button it would take the correct action and then run a sql query for all the comments you selected?
 
PHP:
<?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"/>&nbsp;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>

that's as general as I can be, I can't guess the format of your database, so it doesn't execute anything, it just generates one SQL statement for you to execute with $db->query or mysql_query or however your database is accessed ...
 
Last edited:
OOP = Object Orientated Programming. It's not easy to learn compared to the rest of PHP. If you're a beginner, you can use this site: It's videos - It's slow, and drags out a simple function like include into 3 parts or something. But I watched their OOP videos, it helped a little but you can still use sites like Tizag.

http://www.killerphp.com
 
Back
Top