Forums

Can Chronoforms be user to build "mini-apps"?

danieli 27 Oct, 2008
I like the idea of the new module add-on to slap a chronoform on the left/right. One of the internal requests (we use Joomla for our Intranet) was to have people "sign" an "I have read the policies" box, and would like to know if Chronoforms can fit the bill.

The idea would be...
[list]
  • There would be one table to hold the user-id, date, etc...
  • As the chronoform would load it would check the database to see if the user-id is in the database (if it exists, yes is assumed)
  • If the user ID was found, display a message.. "agreement signed dd\mm\yy"
  • If the user-id was not found, display the agreement form with submit button
  • [/list]
    Is Chronoforms suited to these types of "applications"? I have been mostly using it for e-mailed form submission up to this point (holiday requests, internal applications, etc...), and it would be great if its use could be expanded in our environment.
    Max_admin 27 Oct, 2008
    Hi danieli,

    you will need a simple PHP code piece to check for the user id in the table (normal SELECT SQL query) and show the form or the message, this code can be added at the top of the form HTML code itself!

    Cheers
    Max
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    danieli 27 Oct, 2008
    I am extremely green too PHP and Joomla... are there code objects already defined that I would be using, or I need to figure all the code out for myself?
    Max_admin 27 Oct, 2008
    look at the forums for any code with "SELECT", there will be some queries to get data from tables, you will need something similar, ask me about the code you find if you don't know!

    cheers
    Max
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    danieli 29 Oct, 2008
    I am making some headway... basically, this code will return a nice horizontal row of data in the query row...
    <table border='1'>
    
    <tr>
    
    <?php
    $query = "SELECT * FROM #__chronoforms_2";
    $database->setQuery($query);
    $userdata = $database->loadObject();
    if($userdata->subject){
    
    while (list(, $value) = each ($userdata)) {
          echo '<td>' .$value .'</td>'; }}
          else { echo 'No Results'; }
    ?>
    
    </tr>
    
    </table>

    ...only problem is any code I have added to try iterate through the rows has failed... any help interating through the rows? I had thought that the getNumRows method would help me, but it doesn't seem to work.

    The problem I am facing is trying to learn the PHP, while being tripped-up by the "Joomla method spin". Kind of hard to learn a dialect while still trying to understand the core language.
    Max_admin 29 Oct, 2008
    your code rewritten:

    
        <table border='1'>
    
        <tr>
    
        <?php
        $query = "SELECT * FROM #__chronoforms_2";
        $database->setQuery($query);
        $userdata = $database->loadObjectList();
    if(count($userdata)){  
      foreach($userdata as $data){
    echo $data.'<br>';
    }
    }
        ?>
    
        </tr>
    
        </table>
    
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    danieli 29 Oct, 2008
    no luck, this returns nothing... I think I have an issue with "count" functions, as even this code I gleamed from elsewhere on this site returns no value..
    <?php
    echo "count= ";
    global $database;
    $database->setQuery( "SELECT * FROM jos_chronoforms_2" );
    $rows = $database->loadObjectList();
    echo count($rows);
    ?>

    ... I get the "count=", but no value
    Max_admin 29 Oct, 2008
    is this J1.5 or J1.0 ?
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    danieli 03 Nov, 2008
    J1.5.7 & CF3
    GreyHead 03 Nov, 2008
    Hi danieli,

    global $database doens't work with J1.5.7 unless Legacy mode is on. Better to use the new call. And there is a getNumRows() method that will give you the count.
    <?php
    $database = &JFactory::getDBO(); // only needed once
    $query = "SELECT * FROM #__chronoforms_2";
    $database->setQuery( $query );
    $rows = $database->loadObjectList();
    $count = $database->getNumRows();
    echo "count: $count<br />";
    ?>
    Note that you have to execute the query before getNumRows will give you the correct value.

    Bob
    danieli 03 Nov, 2008
    Thanks for the input, but the problem is that I have no experience to know the "new way" from the "old way". I know for sure that I am not using Legacy mode, nor do I want too.

    What I can get to work is this...
    <?php
    $query = "SELECT * FROM jos_chronoforms_2";
    $database->setQuery($query);
    $userdata = $database->loadObjectList();
    $rowcount=count($userdata,0);
    echo "Row count: " .$rowcount ."<br /><br />";
    
    var_dump($userdata);
    ?>

    What I can't seem to get from this is a way to work with "one record" and then iterate through the rest. At least this tells me that I can query the database, and that the results do contain all the records, just need to get at them.

    I was able to hack some code together to get one record to display properly as a table, but my plan initially is not to have only one record displayed.
    <?php
    $query = "SELECT * FROM #__chronoforms_2";
    $database->setQuery($query);
    $userdata = $database->loadObject();
    if($userdata->subject){
    
    while (list(, $value) = each ($userdata)) {
          echo '<td>' .$value .'</td>'; }}
          else { echo 'No Results'; }
    ?>

    Obviously I need a PHP primer, and a concrete guide to "new way" joomla code.
    GreyHead 03 Nov, 2008
    Hi danieli,

    Did the code I posted not work for you?
    You need to combine your code chunks to get a list of entries to display:
    <?php
    $database = &JFactory::getDBO(); // only needed once
    $query = "SELECT * FROM #__chronoforms_2";
    $database->setQuery( $query );
    $rows = $database->loadObjectList();
    //$count = $database->getNumRows();
    //echo "count: $count<br />";
    echo "<table>";
    foreach ($rows as $row) {
        echo "<tr>";
            foreach ($row as $value) { 
                echo '<td>' .$value .'</td>';
            }
        echo "</tr>";
    }
    echo "</table>";
    ?>

    Bob
    danieli 03 Nov, 2008
    Firstly, let me thank you for offering code that works! Your last code gave me an awesome output table of the entire query!

    As mentioned earlier, I seem to have problems with $database->getNumRows(), as it returns NULL. I assume this a J1.5 vs. J1.0 thing.

    Following your code, how much of it is Joomla PHP (needing framework understanding) vs. PHP (core PHP understanding)? wondering where to start my education :wink:
    GreyHead 03 Nov, 2008
    Hi Danieli,

    Hard to tell which is which any more - maybe 50:50 in here. There are now a couple of Joomla books about writing extensions that might be useful. I don't have then so can't comment. There are an increasing number of useful docs on the Joomla dev wiki too. Mostly I use the api docs plus Google to find what I need. However, that's tough until you have learned a bit as much of it looks completely baffling. In that respect Joomla 1.0 was much easier to get your head round than Joomla 1.5 is. But once you've got there many things are much simpler in 1.5.

    I learned a lot about 1.0 just by hanging out here and answering questions - having to dig in the code and find out how it works is one pf my preferred ways of learning. But I did have fair PHP before that so I can look at a chunk of code and have a pretty good idea what it does.

    Max's code is good - but it's not Joomla 1.5 mainstream so to learn that I've found some simple Joomla 1.0 odds and ends and ported them over to 1.5.

    Bob
    danieli 03 Nov, 2008
    Ok, I played with my SQL statement, and outputs, and am pleased with my chronoform displaying the values of a table. I also updated the table to add one of my custom fields as a "primary key", so now multiple submissions from the same person update the already existing record. So I think this "display form" is all set.

    Going back to the "submission form", I have 2 questions?
    [list=1]
  • Is it difficult to get it to preload the existing data from the form? I figure I can do a conditional on a db query to see if they already exist, but if they do, how do i push the data into the fields of the form?
  • How can I add a button/link to the form to fire off an SQL statement to delete a record? (I'll be using a logged in user property as the filter which is also stored in the db.)
  • [/list:o]
    Max_admin 03 Nov, 2008
    Hi danieli,

    to fill the form fields with data you will need to use SQL and PHP to load the fields values, e.g:
    <input type="hidden" name="option" value="<?php echo $option; ?>" />


    I'm planning to release a new ChronoConnectivity which will do this in the core by tomorrow or wednesday, still needs some touches, a new plugin for Chronoforms to do the same but this will be later!

    Cheers
    Max
    Max, ChronoForms developer
    ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
    ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
    danieli 04 Nov, 2008
    Ok Max, loading the record values looks straight forward... now how about making a button or link on the form that execute a "delete" query?
    danieli 04 Nov, 2008
    I thought I'd try adding another "submit" button and add code to the OnSubmit as follows...
    <?php	
    if ($_POST['submit']=="Remove Me")
        {
         $db = &JFactory::getDBO();
         $db->setQuery( "DELETE From #__chronoforms_carpooldrivers WHERE EmployeeName=$_POST['EmployeeName']" );
          if (!$result = $db->query()) {
           echo $database->stderr();
           return false;
           }
         return $database->getNumRows( $result );
        }
    ?>

    ... but all that resulted is an update of the already existing record for the Employee. (have a primary key set for EmployeeName in the table) So I guess my thought isn't quite right.
    GreyHead 04 Nov, 2008
    Hi Danieli,

    Put the second submit button in the Form HTML - in the OnSubmit before check the submit value and execute the delete code if the delete button was used.

    Bob
    danieli 05 Nov, 2008
    Problem 1
    Ok, when I use my OnSubmit code, the PHP fails and the subject doesn't get set.
    <?php   
    $_POST['subject'] = "New Car Pool Driver - ".$_POST['EmployeeName'];
    
    if ($_POST['submit']=="Remove Me")
        {
         $db = &JFactory::getDBO();
         $db->setQuery( "DELETE From #__chronoforms_carpooldrivers WHERE EmployeeName=$_POST['EmployeeName']" );
          if (!$result = $db->query()) {
           echo $database->stderr();
           }
        }
    ?>


    Is if ($_POST['submit']=="Remove Me") correct? I thought I could var_dump($_POST), but that doesn't seem to work. I'm not sure What is breaking the PHP, but obviously a good reason why the delete doesn't work. Even echo $_POST['submit'] returns nothing, so I don't see how the outer IF loop would ever be true and execute.

    In the form HTML I have...
    <input type="submit" value="Submit Form"><input type="reset" value="Reset Form"><br /><input type="submit" value="Remove Me">

    Can I have two 'sumbit' buttons?

    Problem 2
    I also seem to have a problem using the "PRIMARY KEY" trick to have the form update the database; problem being that it kills the inserts of new records. Here is how I implemented it...
    [list=1]
  • created the form
  • tried to use the CF admin to create the table while setting the 'EmployeeName' field to a PK... failed db creation with an "auto_increment" error (odd as I know there was no check mark there)
  • use the CF admin to create the table without a 'manual' PK
  • verified the form could save to the db (insert 'my record')
  • used MySQL Browser to edit the table and set the 'EmployeeName' field to a PK
  • detached/reattached the table to the form in the CF Admin (to sense the db change)
  • verified the form could update 'my record' in the db
  • deleted the sole record using MySQL Browser so I could test my form switching between loading defaults (no record to load) and loading the users record to 'edit'
  • submitted new form and found that my record was not added
  • opened MySQL Browser to remove PK, detached/reattached db in CF Admin, and retest saved the record
  • [/list:o]
    Inserts and Updates should both work at the same time with the PK set on one of my fields right? What might I have done incorrectly?
    danieli 05 Nov, 2008
    I seem to be making some head way on Problem 1... seems not setting the name of each button was a problem... so I have updated it to...
    <input type="submit" name="button" value="Submit Form"><input type="reset" value="Reset Form"><br /><input type="submit" name="button" value="Remove Me">

    ...and updated my OnSubmit to...
    <?php
    $_POST['subject'] = "New Car Pool Driver - ".$_POST['EmployeeName'];
    
    if ($_POST['button']=='Remove Me')
        {
         echo 'hit the delete block';
           }
        }
    ?>

    ...which proved I could now get inside the IF loop. However updating the OnSubmit to
    <?php
    $_POST['subject'] = "New Car Pool Driver - ".$_POST['EmployeeName'];
    
    if ($_POST['button']=='Remove Me')
        {
         $db = &JFactory::getDBO();
         $db->setQuery( "DELETE From #__chronoforms_carpooldrivers WHERE EmployeeName=$_POST['EmployeeName']" );
          if (!$result = $db->query()) {
           echo $database->stderr();
           }
        }
    ?>

    ... broke the PHP again... so something is wrong with my query I guess.

    So, a bit of an update on Problem 1, and Problem 2 is still in full force.
    GreyHead 05 Nov, 2008
    Hi danieli,

    There are some problems with your sql - (a) you need quotes round the value, and (b) you can't put an array value into a string like that (you can with a simple variable name though), and (c) the sql string probably needs to end with a semicolon. So you need
    $sql = "DELETE From #__chronoforms_carpooldrivers WHERE EmployeeName='".$_POST['EmployeeName']."';";

    All the quotes get messy - it's easier and safer to use:
    
    $db = &JFactory::getDBO();
    $temp = JRequest::getVar('EmployeeName', '', 'post');
    if ( $temp ) {
        $temp = $db->Quoted($temp);
        $sql = "DELETE FROM #__chronoforms_carpooldrivers 
            WHERE EmployeeName=$temp;";
        $db->setQuery($sql);
        . . . 
    }


    To see the $_POST array just turn DeBug on in the Geenral tab and it's shown at the top of the page (at least it used to be). If not then put
    echo '$_POST: '.print_r($_POST, true).'<br />';
    near the start of your OnSubmit code.

    Bob
    This topic is locked and no more replies can be posted.