Hello, I am stumped on a problem im having, and I get the feeling that the solution is quite simple yet requires knowledge of certain functions. Basically I am creating a guestbook which allows users to post comments, however I am trying to create an admin approval system so that comments must be approved before they can be seen.

So far I have an admin login system working and the entire guestbook system working, Users can post to the guestbook and I have created a table for these posts to be stored in, this table has an approved field. In my output pages I have an sql query which only displays rows which have approved set to 1.

I have also created a table for admin which allows them to log in to view the approve post page, this page currently shows all the posts which have approved set to 0. Currently I am making a table which dynamically changes in size as it adds a row for each post. There are two columns, one which contains the post and one which contains a checkbox. This table is part of a form and the idea is that the admin can select approve or deny which will then go back to more code to allow a sql function to find the post id and approve or deny as appropriate. My problem is that I don’t quite know how to link each checkbox to the form. Each of the comments in the sql table has a unique id, perhaps I can somehow use this?

Here is the code, I have commented out the login system but please let me know if there are better safer ways of doing things:

<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Admin Post Approval Area</title>
</head>
<body>
<?php 
//if(!$userid && !$username){
//echo '<META HTTP-EQUIV="Refresh" Content="0; URL=./login.php">';    
    //exit;    
}

?>

<?php 
	//////////////////////////connect to db area 
mysql_connect("localhost", "x", "y");
		mysql_select_db("z");// select database
?>
	
<?php 
	//////////////////////////if form pressed area 
	if($_POST['approvebtn']){


	// this area will have a sql query which approves the posts as appropriate, I am simply struggling to know how to point the form elements from the table below into this. 
	}

?>
	
	
		

<form action='./approve4.php' method='post'>
	<table width="500" border="1">
	
	
		<?php
		///////////////////// display form area 
		
	
	$query = mysql_query("SELECT * FROM guestbook WHERE Approve = '0' ORDER BY id DESC");
$numrows = mysql_num_rows($query); 
	
		while($row = mysql_fetch_assoc($query))//fetch assoc gets all info outta row
	{
		$id = $row['id']; 
		$name = $row['name']; 
		$email = $row['email']; 
		$message = $row['message'];
		$time = $row['time']; 
		$date = $row['date']; 
		$ip = $row['ip']; 
		$starsystem = $row['starsystem']; // will value pass thru? 
		
		$message = nl2br($message);
		
		echo "
		<tr>
			<td><div>
					By <b>$name</b> at <b>$time</b> on <b>$date</b><br />
					Rating - <b>$starsystem</b> <br />
					$message
					
					</div>
			</td>
			<td>
				<form id='form1' name='approve' form action='./guestbookinput.php' method='post'>
  			<p>
    			<label>
     		 	<input type='radio' name='approve' value='1' />
      			approve</label>
    			<br />
    			
				<label>
      			<input type='radio' name='approve' value='2' />
      			deny</label>
    			<br />
  				</p>
				</form>

			</td>
	</tr>";
	}
				?>
				
		

	<tr>
		<td></td>
		<td><input type='submit' name='approvebtn' value='Approve' /></td>
	</tr>
		
	</table>
	</form>
		


	

</body>
</html>

Please write shorthand with direct question!

If you want to create a series of variables (checkboxes) in a form and then be able to use those variables in the processing of the form data, you can:

1. Create the variables in a loop using
<input type=checkbox name="check".$i value= ...

giving you a series of variables from check1 to checkn

2. In the program that receives the form data, you can also construct variables with these names but in a slightly different way:

$wk = ${"check".$i};

This creates the $check1 variable (if $i = 1) thus both $check1 and $wk would have
the same value.

commented: Thanks for the help so far +0

If it is approve or deny only don't use checkboxes use radio buttons for you input values.

Hi Chrishea,

Thanks for the help so far, however I am slightly confused by how it works. What is that technique called so I can read up further?

I am slightly worried that the iteration wont match up with post id if a user enters a new value while another user is using the form, would the same system work if I looped through my database and used the ID field which will always have a unique number?

If you could point me towards an example of this in practise I would be extremely grateful

ddymacek thanks for the advice, I will certaintly use radio buttons, however my confusion lies with the fact that im not sure how to point the form element to the data.

I am extremely confused about how to post the "check".$i value to the new php, would I do something like:

$approvesystem = $_POST;

? This doesnt seem to post the information and I am pretty confused. I have done this in a while loop which would go thru all the table so I think im just confused on the synax.

In the module that has the form you are just creating Post variable names by concatenating a couple of values. That's no big deal. You create the variables by using the concatenated string for the name of the Post variable exactly as shown in my previous post. The value would be something that you can use as a key for the DB. Since the program will be working in a dynamic environment where a user can post a new comment at any time, what your program gets (and will display in the form) is a "point-in-time" snapshot. A second later, or while the list is being reviewed by the administrator, someone could add another one. Your program won't show that additional one until you go through the process again. This kind of processing isn't critical, so there isn't a need to be adding to the list in real-time. That would require some fancy code using Ajax and it just isn't necessary. Additions happening while the list is being processed shouldn't create a problem because your Post values should be a key that can be used to reference the database that will still be valid even if additions have happened since the list was produced. If the database was being dynamically re-organized and keys were being changed in real time then that would be an issue but for what you are doing new items should get added to the end (of the DB table) and existing records shouldn't be changing.

In the module that receives the form data, the example that I gave you is creating variable names on the fly so you can reference them. They are called "variable variables" and you can find some more info here. You could probably also reference the Post variables by concatenating an index to the basic variable name for the Post variable (as you showed in your last post). You can use print_r to dump the Post variables in the receiving program so that will show you what you created that the receiving module needs to process.

Thanks for all the help Chrishea, thanks to you I have a understanding of what I am meant to do, but practically I am still struggling.

Essentially I have concatenated the variables in the form, and in the reciving php I have got a way to point to the id's. My issue comes from the fact that I dont know now how to loop through and actually go through each id.

This all feels very abstract and im sure im missing a simple solution, but trying the typical loops I would use hasnt helped.

<?php
if($_POST['approvebtn']){

		//I cant quite figure out how to loop through the following, any help would be greatly appreciated, I did try and do: 

	$query = mysql_query("SELECT * FROM guestbook WHERE Approve = '0' ORDER BY id DESC");
$numrows = mysql_num_rows($query); 

while($numrows){ // however this just echoed out my $approvesystem value for the top row, maybe I am doing something wrong in how I access the data? 


			$id = $_POST['uniqueid']; // this gets value of id from my form 
			$wk ="approve$id"; //this appends id number to the end of approve to allow me to identify different id's
			
			
	$approvesystem = $_POST[$wk]; //so the value of approve$i is stored each loop iteration.   
	
	echo $approvesystem; // just printing the value of my checkbox. 

	}// ends while 
}

?>

Here is a little sample program to demonstrate this:

<?PHP

// This demos how to access (Post) variables that have been created in a series 
// with a base name and an index number.
// For a real app, the $x variable will probably be $_POST

     $count     = 3;            // create this in the form (hidden variable) 
     $x[field1] = "aaaa";       // set up some test data
     $x[field2] = "bbbb";
     $x[field3] = "cccc";
     
     $i = 1;

     while  ($i <= $count) {
          $name = "field".$i;
          $val = $x[$name];
          echo "<br>Do whatever processing needs to be done for ".$val;
          $i++;
     }
?>
commented: very helpful +0

Hi Chrishea, thanks for all the help. I appreciate you taking the time out to read through my problem and provide support!

I managed to come to a solution before seeing your post and I see that I was thinking along the right lines. My problem with the loop was that I was using mysql_fetch_assoc to get information from each row one row at a time. So I simply wrapped a while loop around that which looped for the number of numrows found from doing a different sql query.

Thanks for all the help, you certainly pointed me in the right direction! Im suprised I didnt find a tutorial explaining this stuff so im very grateful you helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.