Jump to content

Handeling different DB actions on one PHP page


smc

Recommended Posts

Hello,

 

I've hit a wall on my Moderation Que script. Basically what I want to do is give the administrator the chance to approve, reject, or delete stories that are all called from the database into a table. My trouble is how do I use the PHP to handle all these different scenarios with different DB information.

 

Heres an example: ([ ] = Radio Buttons)

 

Noodles [ ]Approve [X]Reject [ ] Delete

Pasta    [X]Approve [ ]Reject [ ] Delete

 

How do I make it so for Noodles it does the appropriate DB action, and for Pasta it does the appropriate DB action?

 

Here is the code I've developed thus far:

 

if ( !isset($_POST['submit']) ){
$int = 1; //All the int code was part of an experiment to get it working but it wouldn't work because it couldn't dynamically call the appropriate INT with the appropriate article ID
function echoresults() {
dbconnect();
$fido = mysql_query("SELECT * FROM stories WHERE status = 'pending' ORDER BY date ASC")
			or die( mysql_error() );
while ( $row = mysql_fetch_array($fido) ) {
	$author = $row['author'];
	$id = $row['id'];
$fidouser = mysql_query("SELECT * FROM users WHERE username = '$author'");
while ( $call = mysql_fetch_array($fidouser) ) {
	$firstname = $call['first_name'];
	$lastname = $call['last_name'];
}
$author = $firstname . " " . $lastname;
echo
	("
	<tr valign=\"top\" width=\"100%\">
        <td width=\"38%\"><a href=\"/staff/staff_viewstory.php?story=" . $id . "\" target=\"_blank\"><strong><font size=\"2\">" . $row['title'] . "</font></strong></a></td>
        <td width=\"26%\"><i><font size=\"2\">" . $author . "</font></i></td>
        <font size=\"1\"><td width=\"50%\">
        <input name=\"" . $int . "\" type=\"radio\" value=\"0\">
        <font size=\"2\">Approve</font>  
        <input name=\"" . $int . "\" type=\"radio\" value=\"1\">
        <font size=\"2\">Reject</font>  
        <input name=\"" . $int . "\" type=\"radio\" value=\"2\">
        <font size=\"2\">Delete</font></td></font>
        </tr>
	");
$int = $int+1;
}
}
session_register(intfinal);
$intfinal = $int;
require ( $cms_root_path . 'templates/admin/admin_que.php' );
}

if ( isset($_POST['submit']) ) {
$int = 1;
if ( $intfinal >= $int ){
	die;
}
$process = $_POST[$int];
if ( $process == "1" ) {
	dbconnect();
	$fido = mysql_query("UPDATE stories SET status='approved' WHERE //Stopped here because I realized it wasen't going to do what I wanted

 

Thanks for any help anyone can offer!!

Ok, First off, I want to critique just a little! Clean up your code some and you may get a little quicker response. I used up 1 hour of my life trying to understand your code and decide what to do with it.

 

Try this on for size.

 

$define("Approve", 0);
$define("Reject", 1);
$define("Delete", 2);

// Use the unique story ID number for the APPROVE/REJECT/DELETE inputs.
// Make sure your FORM action="this.php" this.php being the php file we are altering.
$table_form = '
<tr valign="top" width="100%">
        <td width="38%"><a href="/staff/staff_viewstory.php?story=[iD]" target="_blank"><strong><font size="2">[TITLE]</font></strong></a></td>
        <td width="26%"><i><font size="2">[AUTHOR]</font></i></td>
        <font size="1"><td width="50%">
        <input name="[iD]" type="radio" value="0">
        <font size="2">Approve</font>  
        <input name="[iD]" type="radio" value="1">
        <font size="2">Reject</font>  
        <input name="[iD]" type="radio" value="2">
        <font size="2">Delete</font></td></font>
        </tr>';

// If the there is no submit variable, show available stories.
// USE $_REQUEST INSTEAD OF $_POST. $_REQUEST IS = $_POST & $_GET or .php?variable=value ITEMS
if(!isset($_REQUEST["submit"])) 
{
echo echo_results();
// show form
}
else // Form submitted, interpret our data
{
// Build a small list of current pending stories to get their ID numbner,
// because we stored them in the form as ID numbers.

dbconnect();
$fido = mysql_query("SELECT * FROM stories WHERE status = 'pending' ORDER BY date ASC")
			or die( mysql_error() );

while($row = mysql_fetch_arrow($fido))
{
	$stories[] = $row['id'];
}


// now that we have an array of all pending stories, compare them to the $_REQUEST global to see if any of these
// stories are being processed
foreach($stories as $story_id)
{
	if(in_array($story_id, $_REQUEST))
	{
		// Story found. The VALUE of that story will be what the user has chosen to do with it. 0=Accept 1=Reject 2=Delete
		$action = $_REQUEST[$story_id];

		// I made defines at the top of this page to use for clarity here
		if($action == Approve)
			// Change its status from pending, or move to accepted table
		if($action == Reject)
			// Change its status from pending, or move to rejected table
		if($action == Delete)
			// DESTROY! KILL! MURDER!
	}
}

dbdisconnect();

// Now give the user results... you can either save what was done to each story and thank the user, or just return to 
// the original, page, or whatever, but now is the time to do it!
}

function echo_results()
{
global $table_form;

dbconnect();
$fido = mysql_query("SELECT * FROM stories WHERE status = 'pending' ORDER BY date ASC")
			or die( mysql_error() );

while($row = mysql_fetch_array($fido))
{
	$id = $row['id'];
	$author = $row['author'];
	$title = $row['title'];

	$fidouser = mysql_query("SELECT * FROM users WHERE username = '$author'");
	$user = mysql_fetch_array($fidouser);

	$firstname = $call['first_name'];
	$lastname = $call['last_name'];

	$author = $firstname . " " . $lastname;

	$temp = $table_form;
	$temp = str_replace("[iD]", $id, $temp);
	$temp = str_replace("[AUTHOR]", $author, $temp);
	$temp = str_replace("[TITLE]", $title, $temp);

	$results .= $temp; // .= appends the value. Meaning $results = $results.$temp;
}

dbdisconnect();

return $results;
}

 

I hope this helps some. If you are new to PHP, let me give you just a couple small advices.

 

1. Don't do functions or function spaces like so:

    while(somecalc) {

    la de da

    }

 

    Do

    while(somecalc)

    {

   

        la de da;

    }

 

2. $int = $int + 1; is DEAD. Use $int++ instead, or even $int-- to take off a number. Subsequently, to append values, you use the .= operator. This will append the give value onto the first supplied value. $roses .= $violets. $roses now equals rosesviolets.

 

3. Try not to nest functions within IF statements or any other function. Functions should always be separated entities. You will notice in my example that I made the HTML a global variable, and then in echo_results() I used a global call to access that data in a function. This makes it much easier to track what is happening in a process.

 

4. Line Spacing and Character Spacing Costs Nothing, but Means Everything! Put some lines in between lines! I.e.

  $author=$row['author'];

  $title=$row['title'];

  $id=$row['id'];

 

  Try this instead:

  $author = $row['author'];

  $title = $row['title'];

  $id = $row['id'];

 

  echo $author."<br>";

 

  echo $title."<br>";

 

5. Enjoy your day and I hope my code helps a little!

Your code didn't work, or my adapting proccess messed it up in some way. Now I get my results page but with no text on it. This tells me none of the if functions executed correctly. Here is the adapated code

 

//This function will allow the results to be echoed from within the template file
function echo_results()
{
global $table_form;
//This is our standard table format for result display
$table_form = '
<tr valign="top" width="100%">
        <td width="38%"><a href="/staff/staff_viewstory.php?story=[iD]" target="_blank"><strong><font size="2">[TITLE]</font></strong></a></td>
        <td width="26%"><i><font size="2">[AUTHOR]</font></i></td>
        <font size="1"><td width="50%">
        <input name="[iD]" type="radio" value="0">
        <font size="2">Approve</font>  
        <input name="[iD]" type="radio" value="1">
        <font size="2">Reject</font>  
        <input name="[iD]" type="radio" value="2">
        <font size="2">Delete</font></td></font>
        </tr>';

dbconnect();
$fido = mysql_query("SELECT * FROM stories WHERE status = 'pending' ORDER BY date ASC")
			or die( mysql_error() );

while($row = mysql_fetch_array($fido))
{
	$id = $row['id'];
	$author = $row['author'];
	$title = $row['title'];

	$fidouser = mysql_query("SELECT * FROM users WHERE username = '$author'");

	while ( $call = mysql_fetch_array($fidouser) ){
	$firstname = $call['first_name'];
	$lastname = $call['last_name'];
	}

	$author = $firstname . " " . $lastname;

	$temp = $table_form;
	$temp = str_replace("[iD]", $id, $temp);
	$temp = str_replace("[AUTHOR]", $author, $temp);
	$temp = str_replace("[TITLE]", $title, $temp);

	$results .= $temp; // .= appends the value. Meaning $results = $results.$temp;
}

return $results;
}

if ( !isset($_REQUEST["submit"]) ){
$cms_root_path = '../';
require ( $cms_root_path . 'templates/admin/admin_que.php' );
}

if ( isset($_REQUEST["submit"]) ) {
//This will define the variables to be translated later on. Just provides more clarity
define("Approve", 0);
define("Reject", 1);
define("Delete", 2);

// Build a small list of current pending stories to get their ID numbner,
// because we stored them in the form as ID numbers.

dbconnect();
$fido = mysql_query("SELECT * FROM stories WHERE status = 'pending' ORDER BY date ASC")
			or die( mysql_error() );

while($row = mysql_fetch_array($fido))
{
	$stories[] = $row['id'];
}
$statustxt = "";

// now that we have an array of all pending stories, compare them to the $_REQUEST global to see if any of these
// stories are being processed
foreach($stories as $story_id)
{
	if(in_array($story_id, $_REQUEST))
	{
		// Story found. The VALUE of that story will be what the user has chosen to do with it. 0=Accept 1=Reject 2=Delete
		$action = $_REQUEST[$story_id];

		// The defines will help clarify things up top
		if($action == Approve) {
			dbconnect();
			$fido = mysql_query("UPDATE stories SET status = 'approved' WHERE id = '$story_id'");
			$statustxt .= "Story <b>" . $story_id . "</b> has been approved.<br />";
		}
		if($action == Reject) {
			dbconnect();
			$fido = mysql_query("UPDATE stories SET status = 'rejected' WHERE id = '$story_id'");
			$statustxt .= "Story <b>" . $story_id . "</b> has been rejected.<br />";
		}
		if($action == Delete) {
			dbconnect();
			$fido = mysql_query("UPDATE stories SET status = 'delete' WHERE id = '$story_id'");
			$statustxt .= "Story <b>" . $story_id . "</b> has been marked for deletion.<br />";
		}
		if($action == "") {
			$statustxt .= "Story <b>" . $story_id . "</b> has been ignored.<br />";
		}
	}
}

$cms_root_path = '../';
require ( $cms_root_path . 'templates/admin/admin_que_results.php' );
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.