Page 1 of 1

Module needed, or just a custom replacement tag?

Posted: Wed 16. Feb 2005, 18:25
by Sheila
I need to add forms that update a database. Can I add content that includes this type of form using a new replacement tag? Or do I need to build a mod? Can it be done at all?

The site is on an intranet, so security is not a concern.

BTW, I've spent hundreds of hours experimenting with different open source CMS systems, including Mambo and LDU. I keeping coming back to phpwcms. I really appreciate its simple power and usability. Keep up the wonderful work, Oliver!

Posted: Mon 21. Feb 2005, 02:13
by RonDog
Hi Sheila,

You won’t need a ‘new replacement tag’, nor a ‘mod’.

I’d like to show you how I solved this with this basic code structure.

Code: Select all

//place this TAG in an article summary field. That' all you need to do.

{PHP:form.update.php}


//this is the pseudo code of the script "form.update.php", 
//which is stored in the root folder in this sample.

<?php

//if you need includes, do them here...
require_once ("include/inc_lib/whatever.inc1.php");
include_once ("include/inc_lib/wahtever.inc2.php");

//this get's the action code from the form
$a = $_REQUEST["a"];

//when entering the page by navigation 
//there's no $a value, so we set it to zero.
$a = (!$a)?0:$a;

//depending on your logic, 
//you need at least to 
//read something from a data base
//and to update a record...

switch($a) {
	case 0:		//read the database

		$data_ok = 0;

		$sql =	"SELECT * FROM ….;

  		if($result = mysql_query($sql)) {
      		if(mysql_num_rows($result)) {
      			if($row = mysql_fetch_array($result)) {
      				$key  		 =	$row["key_field"];
      				$form_field1 =	$row["data_field1"];
  					...
      				$form_fieldn =	$row["data_fieldn"];
      			
      				$data_ok = 1;
      			} else {
				  	//this should not happen, but I think it's a way to secure the access
      				$data_ok = 0;
      			}
      			mysql_free_result($result);
      		} else {
			  	//no record found here...
      			$data_ok = 0;
      		}
       	} else {
		  	//you may use this here: die('Invalid query: ' . mysql_error());
    		$data_ok = 0;
    	}
	break;

	case 1:		//request the form data

		$key	  	 = $_REQUEST["key"];
		$form_field1 = $_REQUEST["form_field1"];
		...
		$form_fieldn = $_REQUEST["form_fieldn"];
		
		//check the data if neccessary...
		
		//set message values to be displayed, in case something wrong or missing...
		
		//update the data record...
		if($data_ok) {
    		$sql = "UPDATE your_table SET".
    		"', db_field1='".$form_field1.
    		"', db_field2='".aporeplace(clean_slweg($db_field2)).
			...
    		"', db_fieldn='".intval($db_fieldn).
    		"' WHERE usr_id=".$key.";";

    		$result = mysql_query($sql);			
            if(!$result) {
                die("error while update to my database: <br /><pre>".$sql."</pre><br />".mysql_error());
                exit();
            }
		}
		break;

	default:
		//do some echo here, in case you mix up the code...
}


switch($a) {
	case 0:
	case 1:
    	if($data_ok) {
    		echo '<form action="/index.php?your_alias_name" name="form_name" method="post">'.chr(10);
        	echo '<input name="a" type="hidden" id="a" value="'.$a.'">'.chr(10); //action code (e.g. update)
        	echo '<input name="key" type="hidden" id="key" value="'.$key.'">'.chr(10); //key value for update
    		...
    		echo 'your form fields here...';
    		...
    		if($data_ok) {
        		echo 'your success messages here...';
    		} else {
        		echo 'your error messages here...';
    		}
    		...
    		echo '<input name="update" type="submit" value="Update" class="button12" ';
    		echo 'onClick="document.form_name.a.value='1';document.form_name.submit();">
    
    		echo '</form>'.chr(10);
    		break;
    	} else {
    		echo 'your error messages here...';
    	}

	default:
}

function your_function_1() {
	//your one time functions here...
}

?>
I hope this will help you to get the clue!

Posted: Mon 21. Feb 2005, 03:53
by Sheila
Oh, nice! I like phpwcms even better now! In any other CMS I've investigated, I would have had to create a mod or some other complex customization.

Thanks very much, RondDog. :)

Posted: Mon 21. Feb 2005, 12:54
by RonDog
Hi Sheila,

it took a while for me as well, to find out how to implement additioal functions and I still get lost in the maze of undocumented code of PHPWCMS, but it's a nice and powerful tool anyway.

Sorry for a bit of nonsense I posted in my pseuso code.

Code: Select all

          if($data_ok) {
              echo 'your success messages here...';
          } else {
              echo 'your error messages here...';
          } 
This part inside the 'switch()' 'case 1' construct is obsolete. It was too late last night and I had problems by keeping my eyes open... :cry:

I hope you found this inconsistency already...

Btw. to build 'Replacement Tags' are as easy as my basic code I posted. This is a powerful way of dropping functionality into your site in almost any place you like.

Success and good luck with PHPWCMS!