phpMyViews - Tutorial

Example 3 : News Edit Form

At first, here are some screenshots from a webpage, which uses PMV. It uses parts of this config file example.

Now let's have a look at a config file for listing and editing news entries


<?php
  
# MySQL Parameters
  
$pmv_db_host="host";
  
$pmv_db_name="db";
  
$pmv_db_user="user";
  
$pmv_db_pass="pass";
  if (
file_exists('pmv_db_config.php')) {
    include(
'pmv_db_config.php');
  }

  
# Actions
  
$pmv['default'][0]['sql']='SELECT newsID, newsDate, newsTopic FROM newsletter ORDER BY newsID DESC LIMIT 10';
  
$pmv['default'][0]['pre']='<table>
        <tr><th>ID</th><th>Topic</th><th>Date</th><th></th></tr>'
;
  
$pmv['default'][0]['main']='<tr><td>$newsID</td><td>$newsTopic</td><td>$newsDate</td><td>
        <form action="'
.$PHP_SELF.'" method="post">
          <input type="hidden" name="msgid" value="$newsID">
          <input type="submit" name="pmv_action" value="edit">
        </form></td></tr>'
;
  
$pmv['default'][0]['post']='</table>';

  
$pmv['edit'][0]['sql']='SELECT * FROM newsletter WHERE newsID="$msgid"';
  
$pmv['edit'][0]['main']='
        <form action="'
.$PHP_SELF.'" method="POST">
          <table> 
            <tr><td>Überschrift:</td><td><input type="text" name="topic" size="60" maxlength="120" value="$newsTopic"></td></tr>
            <tr><td>Text:</td><td><textarea name="text" rows="10" cols="50">$newsText</textarea></td></tr>
          </table>
          <input type="hidden" name="pmv_action" value="update_entry">
          <input type="hidden" name="msgid" value="$newsID"><input type="submit" value="change">
        </form>'
;

  
$pmv['update_entry'][0]['sql']='UPDATE newsletter SET newsTopic="$topic", newsText="$text" WHERE newsID=$msgid';
  
$pmv['update_entry'][0]['post']='Done. <a href="'.$PHP_SELF.'">Back</a>';

  
# execute phpMyViews
  //$path_to_pmv=$_SERVER['DOCUMENT_ROOT']."/phpmyviews.php";
  
$path_to_pmv="phpmyviews.php";
  include 
$path_to_pmv;
?>

Now we have more than one action, as we not simply want to list the news entries, but also edit them.

The different actions are: 'default', 'edit' and 'update_entry'. If PMV is called without parameter, it executes the 'default'-action and ignores the others. If a POST or GET parameter named pmv_action is submitted, it searches for the corresponding configuration variables and executes the action described there.

We use that fact for defining action buttons in addition to our news list:


  $pmv['default'][0]['sql']='SELECT newsID, newsDate, newsTopic FROM newsletter ORDER BY newsID DESC LIMIT 10';
  $pmv['default'][0]['pre']='<table>
        <tr><th>ID</th><th>Topic</th><th>Date</th><th></th></tr>';
  $pmv['default'][0]['main']='<tr><td>$newsID</td><td>$newsTopic</td><td>$newsDate</td><td>
        <form action="'.$PHP_SELF.'" method="post">
          <input type="hidden" name="msgid" value="$newsID">
          <input type="submit" name="pmv_action" value="edit">
        </form></td></tr>';
  $pmv['default'][0]['post']='</table>';

This looks very similar to the first example, except the HTML form in the main line. This provides a button for each entry which, once clicked, requests the current script with the parameters "msgid" and pmv_action. "msgid" is set to a value from the database, pmv_action is always 'edit'.

If PMV detects the pmv_action parameter, which now is set to 'edit' it uses the following configuration:


  $pmv['edit'][0]['sql']='SELECT * FROM newsletter WHERE newsID="$msgid"';
  $pmv['edit'][0]['main']='
        <form action="'.$PHP_SELF.'" method="POST">
          <table> 
            <tr><td>Überschrift:</td><td><input type="text" name="topic" size="60" maxlength="120" value="$newsTopic"></td></tr>
            <tr><td>Text:</td><td><textarea name="text" rows="10" cols="50">$newsText</textarea></td></tr>
          </table>
          <input type="hidden" name="pmv_action" value="update_entry">
          <input type="hidden" name="msgid" value="$newsID"><input type="submit" value="change">
        </form>';

This action fetches all data from the database where the message id matches the "msgid" url parameter, this should normally be only ONE line, so we define a simple main statement, which creates an HTML form, fills the input fields with values from the database and again defines POST parameters pmv_action and "msgid". Notice, that now, we will jump to the 'update_entry' action.

If the 'update_entry' action is recognized, PMV will do the following:


  $pmv['update_entry'][0]['sql']='UPDATE newsletter SET newsTopic="$topic", newsText="$text" WHERE newsID=$msgid';
  $pmv['update_entry'][0]['post']='Done. <a href="'.$PHP_SELF.'">Back</a>';

This updates the database with values submitted throught the HTML form and displays a link, back to the normal view (= 'default' view).


Previous page Next page
SourceForge.net Logo