Skip to main content
Topic: PHPRAD Classic edition and Mobile App (Read 2756 times) previous topic - next topic

PHPRAD Classic edition and Mobile App

Hello, i download the trial 30 day of PHPRad classic edition
I create a database sample with a table "attendance" with fields id,RFIDTAG,Data,Ora,Stato
I will to use api request to add a record from a mobile app call add post method.
My http request is http://localhost/alumniattendance/attendance/add?RFIDTAG=11111&Data=22/12/2020&Ora=11:00:00&Stato=Ingresso

When i call it i was redirect to add page and the fields ar correct populate with the value.

How can perform submission in table automatically ? Actually i need to press to button submit in order to write value to table, but i will add record automatically aftere call the post method . Is possible ? Can provide me sample code ? Thanks in advance.



Re: PHPRAD Classic edition and Mobile App

Reply #3
@Amilu1967‍ you can create your API in the ApiContrller.php in the app/controllers/ folder. Here you can find more info on creating the API function Adding cusotm button php (phprad.com).
can provide me sample code based on my api request ? Thanks in davance
i create this in apicontroller.php but not work:

function insertattendance()
   {
   
   table_data = array(
    $modeldata['RFIDTAG'],
    $modeldata['Data'],
    $modeldata['Ora'],
    $modeldata['Stato']);
   
$db->insert("attendance", $table_data);
}
can help me? thanks

Re: PHPRAD Classic edition and Mobile App

Reply #4
@Amilu1967‍ it should look like this.
Code: [Select]
function insertattendance()
{
    if(!empty($_GET)){ // we check if the get variable is not empty.
        table_data = array(
            $_GET['RFIDTAG'],
            $_GET['Data'],
            $_GET['Ora'],
            $_GET['Stato']
        );

        $db = $this->GetModel();
        $db->insert("attendance", $table_data);
    }
}

Re: PHPRAD Classic edition and Mobile App

Reply #5
@Amilu1967‍ it should look like this.
Code: [Select]
function insertattendance()
{
    if(!empty($_GET)){ // we check if the get variable is not empty.
        table_data = array(
            $_GET['RFIDTAG'],
            $_GET['Data'],
            $_GET['Ora'],
            $_GET['Stato']
        );

        $db = $this->GetModel();
        $db->insert("attendance", $table_data);
    }
}

make changes, call http://localhost/alumniattendance/api/insertattendance/?RFIDTAG=9999999&Data=22/12/2020&Ora=11:00:00&Stato=Ingresso return error 500

Re: PHPRAD Classic edition and Mobile App

Reply #6
@Amilu1967‍ please make sure you did not set publish to production as true so that you can get a proper error message and know what you are doing wrong.

Re: PHPRAD Classic edition and Mobile App

Reply #7
This is the my ApiController.php file:

Code: [Select]
<?php

/**
 * Info Contoller Class
 * @category  Controller
 */

class ApiController extends BaseController
{

/**
* call model action to retrieve data
* @return json data
*/

function json($action, $arg1 = null, $arg2 = null)
{
$model = new SharedController;
$args = array($arg1, $arg2);
$data = call_user_func_array(array($model, $action), $args);
render_json($data);
}
function insertattendance()
{
    if(!empty($_GET)){ // we check if the get variable is not empty.
        $table_data = array(
            $_GET['RFIDTAG'],
            $_GET['Data'],
            $_GET['Ora'],
            $_GET['Stato']
        );

        $db = $this->GetModel();
        $db->insert("attendance", $table_data);
    }

}

This is the error produced:


Error Message
syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)

File
C:\xampp\htdocs\alumniattendance\app\controllers\ApiController.php On Line 38

Stack Trace
1 unknown file(unknown line): autoloadController('ApiController')
2 unknown file(unknown line): spl_autoload_call('ApiController')
3 C:\xampp\htdocs\alumniattendance\system\Router.php(148): class_exists('ApiController', true)
4 C:\xampp\htdocs\alumniattendance\system\Router.php(109): Router->run('api/insertattendance/')
5 C:\xampp\htdocs\alumniattendance\index.php(106): Router->init()

Where i wrong ? Thanks in advance

 

Re: PHPRAD Classic edition and Mobile App

Reply #8
This is the my ApiController.php file:

Code: [Select]
<?php

/**
 * Info Contoller Class
 * @category  Controller
 */

class ApiController extends BaseController
{

/**
* call model action to retrieve data
* @return json data
*/

function json($action, $arg1 = null, $arg2 = null)
{
$model = new SharedController;
$args = array($arg1, $arg2);
$data = call_user_func_array(array($model, $action), $args);
render_json($data);
}
function insertattendance()
{
    if(!empty($_GET)){ // we check if the get variable is not empty.
        $table_data = array(
            $_GET['RFIDTAG'],
            $_GET['Data'],
            $_GET['Ora'],
            $_GET['Stato']
        );

        $db = $this->GetModel();
        $db->insert("attendance", $table_data);
    }

}

This is the error produced:


Error Message
syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)

File
C:\xampp\htdocs\alumniattendance\app\controllers\ApiController.php On Line 38

Stack Trace
1 unknown file(unknown line): autoloadController('ApiController')
2 unknown file(unknown line): spl_autoload_call('ApiController')
3 C:\xampp\htdocs\alumniattendance\system\Router.php(148): class_exists('ApiController', true)
4 C:\xampp\htdocs\alumniattendance\system\Router.php(109): Router->run('api/insertattendance/')
5 C:\xampp\htdocs\alumniattendance\index.php(106): Router->init()

Where i wrong ? Thanks in advance
After insert the lack  "}" now the erro is :


Error Message
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'

File
C:\xampp\htdocs\alumniattendance\app\models\PDODb.php On Line 459

Stack Trace
1 C:\xampp\htdocs\alumniattendance\app\models\PDODb.php(459): PDOStatement->execute()
2 C:\xampp\htdocs\alumniattendance\app\models\PDODb.php(1151): PDODb->buildInsert('attendance', Array, 'INSERT')
3 C:\xampp\htdocs\alumniattendance\app\controllers\ApiController.php(34): PDODb->insert('attendance', Array)
4 C:\xampp\htdocs\alumniattendance\system\Router.php(196): ApiController->insertattendance()
5 C:\xampp\htdocs\alumniattendance\system\Router.php(109): Router->run('api/insertattendance/')
6 C:\xampp\htdocs\alumniattendance\index.php(106): Router->init()

my table "attendance" fields are : id , RFIDTAG , Data , Ora , Stato


Re: PHPRAD Classic edition and Mobile App

Reply #9
Modified as :
Code: [Select]
function insertattendance()
    {
    if(!empty($_GET)){ // we check if the get variable is not empty.
        $table_data = array(
            "RFIDTAG" => $_GET['RFIDTAG'],
            "Data" => $_GET['Data'],
            "Ora" => $_GET['Ora'],
            "Stato" => $_GET['Stato']
        );
       
        $db = $this->GetModel();
        $db->insert("attendance", $table_data);
    }
    }

Now Work.

Thanks in advance.

Now How i can know If the operation is success ? is there a way ?

Re: PHPRAD Classic edition and Mobile App

Reply #10
@Amilu1967‍ you can use if statement to check the response of your insert.
Code: [Select]
$res = $db->insert("attendance", $table_data);
if($res){
   //Do something if successful.
} else {
   //Else do this if not successful.
}

Re: PHPRAD Classic edition and Mobile App

Reply #11
Now  i need the retrieve record from the app of a table "alunni" in json format .
How to wirite a function to make this in ApiController ?
eg (http mysite/api/getAlunni )
Can provide me sample code ?
Thanks in advance
(i purchased PHPRad Classic Edition)

Re: PHPRAD Classic edition and Mobile App

Reply #12
@Amilu1967‍ please note that codes used in page event and any PHP code, syntax, and function can be used here, you need a basic knowledge of PHP to attempt this section.
Here is a sample code:
Code: [Select]
function getAlunni() {
    $db = $this->GetModel(); // here you get an instance of the model class
    $alunni = $db->get("alunni"); // alunni is the able you want to get records from.

   if($alunni){
       //Do something if successful.
        render_json($alunni); //here you display as json using phprads render_json function
    } else {
       //Else do this if not successful.
       render_json("error" => "Error getting records."); //here you display as json using phprads render_json function
    }
}

Re: PHPRAD Classic edition and Mobile App

Reply #13
Very thanks, i not practice with PHP, i follow manuals an tutorial for help me.

Re: PHPRAD Classic edition and Mobile App

Reply #14
Hello,regard functions getalunni :
can provider me a samples of db.where fieldname=value from $_GET variabile in order to retrieve a specific record ?
I try but without results.
Thanks in advance