SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PEAR HTML_QUICKFORM fetch a table and display

    Here is something a little dynamic I'm having a little trouble with using PEAR's HTML_Quickform which is very handy by the way.

    I have a small look up table called task_table. I want to be able to allow my admin users to be able to edit this entire table. For this example i am only displaying one column. When i figure out one column i will fill in the rest. For now lets just try to pull each username and display each of them and then edit and then update.

    I can easily do it using standard procedural style. But using HTML QUICKFORM is a bit tricky. Anybody know how to solve this one.

    PHP Code:
    <?php

    //load PEAR DB
    require 'DB.php';

    //load QuickForm
    require_once 'HTML/QuickForm.php';

    //include my functions that has database handler param
    include('C:\wamp\www\reports\php_lib\functions.php'); 

    //get $dbh
    $dbh=dsn_connect();


    /
    Initialize QuickFORM
    $form 
    = new HTML_QuickForm('Task Table');

    //Get all the records in the task_table and 
    $sql_dept="SELECT username from task_table";

    $result $dbh->query($sql_dept);



    //initialize counter var
    $i 0;

    WHILE(  
    $row  $result->fetchRow( )) {
     
    //actual text box gets shown
     
    $form->addElement('text''MyTextBox''Users Name:');
       

    // Set default values from sql query to show in each text box
    $form->setDefaults(array( 'MyTextBox'   => $row[$i]));

    //add to counter
    ++$i;

    }



    //button element goes here and  validation function after not shown here



    // render and display the form
    $form->display();

    ?>
    ************** Database table and the data i want to display and edit

    Code MySQL:
    CREATE TABLE `task_table` (
      `user_id` int(11) NOT NULL default '0',
      `last_digit` int(5) default NULL,
      `username` varchar(50) default NULL,
      KEY `user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
     
    #
    # Dumping data for table task_table
    #
     
    INSERT INTO `task_table` VALUES (6943,0,'Rebecca');
    INSERT INTO `task_table` VALUES (6943,1,'Rebecca');
    INSERT INTO `task_table` VALUES (9023,2,'Karissa ');
    INSERT INTO `task_table` VALUES (9023,3,'Karissa ');
    INSERT INTO `task_table` VALUES (7913,4,'Amy ');
    INSERT INTO `task_table` VALUES (7913,5,'Amy ');
    INSERT INTO `task_table` VALUES (8036,6,'Yolanda ');
    INSERT INTO `task_table` VALUES (8036,8,'Yolanda ');
    INSERT INTO `task_table` VALUES (8239,7,'Tavey ');
    INSERT INTO `task_table` VALUES (8239,9,'Tavey ');
    INSERT INTO `task_table` VALUES (7019,6547,'Deb ');
    INSERT INTO `task_table` VALUES (7019,6826,'Deb ');
    INSERT INTO `task_table` VALUES (7019,6980,'Deb ');
    INSERT INTO `task_table` VALUES (7019,9939,'Deb ');

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you adding elements inside of a loop, you need to make sure that names of form elements are different.

    Better to fetch user_id, username from task_table
    then fetch result as an array where keys are values of user_id, values are username
    then
    foreach($result as $userid => $username){
    $form->addElement('text', 'user'.$userid, 'Users Name:');
    }
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet. Let me give that a try. I'll post what i find back. Thanks again.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't work.

  5. #5
    SitePoint Member
    Join Date
    Nov 2009
    Location
    Lawrence, KS
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mbriggs View Post
    Doesn't work.
    The point lampcms was trying to make was that the name="MyTextBox" needs to be unique for every one of your form fields, otherwise your $_POST will only have 1 value in it, instead of 1 value for every form field. Using your current example code, every text input will have the same name="MyTextBox".

    The other problem is $form->setDefaults() should only be called once, right before the form is displayed.

    And yet another problem is you have no way to uniquely identify the rows in your `task_table` to then update the values after editing them with the form. Each table should have an id column, so let's add one to your table:

    Code:
    ALTER TABLE `task_table` ADD COLUMN `id` INT(10) UNSIGNED AUTO_INCREMENT;
    Then for your PHP code, let's try this instead:

    PHP Code:
    //load PEAR DB 
    require 'DB.php'

    //load QuickForm 
    require_once 'HTML/QuickForm.php'

    //include my functions that has database handler param 
    include('C:\wamp\www\reports\php_lib\functions.php'); 

    //get $dbh 
    $dbh=dsn_connect();

    //Get all the records in the task_table
    $sql_dept="SELECT id, username from task_table"

    $result $dbh->query($sql_dept); 

    $form = new HTML_QuickForm('task_table');

    foreach(
    $results as $row)
    {
        
    //Add text box
        
    $form->addElement('text''username['.$row['id'].']''Users Name:');

        
    //Store default value for this user's name
        
    $defaults['username['.$row['id'].']'] = $row['username'];
    }

    //Add a submit button
    $form->addElement('submit''submit''Submit');

    //Set form field default values
    $form->setDefaults($defaults);

    //Display the form
    $form->display(); 
    Then to add this data back to the database...

    PHP Code:
    $new_username $_POST['username'];
    foreach(
    $new_username as $id=>$username)
    {
        
    //Run your SQL UPDATE code here...
        //$id is the task_table.id field for your database
        //$username is the text input value
        //UPDATE `task_table` SET `username` =  mysql_real_escape_string($username) WHERE `id` = mysql_real_escape_string($id)

    Hope that points you in the right direction.


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
  •