SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Collecting form information quickly

    Hi All

    I have a very long form with many entries.

    In the past to process a form I have manually added each field of the form to a variable.

    I am sitting here thinking there has to me a much easier way of doing this but I do not know how. I am thinking maybe an array but I'm not too sure where to start.

    Any ideas?

    Thanks for any help you can give

  2. #2
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this:

    Name every field in your form as the equivalent database fields.

    Then you can easily construct the update or insert query with a foreach loop.

    A good friend (and former boss) once told me: "If you want something to be automated, just give it as a daily task to the laziest guy in the office... He'l find a way to do it with the less effort possible and as automated as it can be"

    One of the most annoying things I've found in my business (web-dev) are the forms. And I guess I'm the lazziest person in the office (I'm the only one here), so I've developed few scriptlets (?) to minimize the effect.

    1.- I have a query builder...

    PHP Code:
    // Funcion para preparar queries, requiere de la funcion previa
     
    function mysql_creaquery($tabla,$campos,$tipo="INSERT",$id=0) {
      
    $campos_array = array();
      
    $campos_pri explode (",",$campos);
      foreach (
    $campos_pri as $cpri) {
       
    $campos_array[]=explode("|",$cpri);
      }
      
      switch (
    $tipo) {
       case 
    "INSERT":
       
    //Construimos lista de campos (aqui le agrego el "id" que asumo como default)
       
    $campos="id";
       for (
    $i=0;$i<count($campos_array);$i++) {
        
    $campos.=",".$campos_array[$i][0];
       }
       
    $valores="NULL";
       for (
    $i=0;$i<count($campos_array);$i++) {
        
    $valores.=",".mysql_prepare($_POST[$campos_array[$i][0]],$campos_array[$i][1]);
       }
       
    $qry ="INSERT into ".$tabla." (".$campos.") values (".$valores.")";
       return 
    $qry;
       break;
       
       case 
    "UPDATE":
       
    $pares=array();
       for (
    $i=0;$i<count($campos_array);$i++) {
        
    $pares[]=$campos_array[$i][0]."=".mysql_prepare($_POST[$campos_array[$i][0]],$campos_array[$i][1]);
       }
       
    $asignacion implode (",",$pares);
       
    $qry ="UPDATE ".$tabla." SET ".$asignacion." where id=".$id;
       return 
    $qry;
       break;
      }
     } 
    2.- And I have a preparing function (which is used in the previous one). This one finds NULL, and empty form fields and converts to the appropiate data.

    PHP Code:
    // Funcion para preparar cadenas para insertar en MySQL
     
    function mysql_prepare($texto="",$tipo="s") {
      
    $tipo = ($tipo=="") ? "s" $tipo;
      if (
    $texto=="" && $tipo=="s") {
       return 
    "NULL";
      } elseif (
    $texto==&& $tipo=="n") {
       return 
    0;
      } elseif (
    $tipo=="x") {
       return 
    $texto;
      } else {
       
    $pre=($tipo=="s") ? "'" "";
       
    $parains=$pre.$texto.$pre;
       return 
    $parains;
      }
     } 
    So, to construct a query from a form, I just need to do this...

    $query=mysql_creaquery("tablename","field|n,other|s,etc|s","UPDATE");

    First argument is the table name, second one is a list of all the fields I'll use, marked with a |n or |s, either they are numeric or string fields in the database.

    I also have a validatos, but it is very dirty, as I have been adding customized verifications on the fly...

    Hope that helps.

    Comments about my code are wellcome.

  3. #3
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't read all of manoloweb's response so I may reiterate a few things...

    If you post the form to self, the variables will automatically be assigned according to the name of the field name.
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  4. #4
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the $_POST[] of course...

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    manoloweb comments are great and looks like what I am looking for but I can't get it to work.

    If manoloweb can make any further comments or explain the code a bit more it would be great. Thanks for your help.

    With the POST to self what else should I use to get the info to the database?

    Thanks for all your help.

  6. #6
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you POST to self the variables will automatically be created based on the field names. To get it into the database, just use your MySQL statements. As long as you understand a little about OOP, Manolooweb's function and class should do the trick for automating it.
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •