Codeigniter validaton_error() to array - validation

I want to convert the output of the validation_errors() to an array(json object eventually). What I've done so far with the help of other tutorials is this -> I've extended the CI_Form_Validation Library to return the protected array _error_message
static function errorToArray()
{
$CI =& get_instance();
return $CI->form_validation->_error_messages;
}
But to no luck have I been able to view what the content inside the array is to even consider passing it to the view. Any help would be appreciated.

This is very simple
if($this->form_validation->run() === TRUE){
}else{
$array = validation_errors();
echo json_encode($array);
}

Related

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

Is this Codeigniter 3.0 compatible?

Can someone tell me if this code is codeigniter 3.0 compatible ?
If not, how should it be formatted?
if ( ! function_exists('get_site_url'))
{
function get_site_url($data){
$CI =& get_instance();
//$data = '';
//echo base_url(); exit;
$data =str_replace('{SITE_URL}',base_url(),$data);
return $data;
}
}
SITE_URL is constant that you define using define('SITE_URL','value').So no need quotes ''.Try like this..
if ( ! function_exists('get_site_url'))
{
function get_site_url($data){
$CI =& get_instance();
//$data = '';
//echo base_url(); exit;
$data =str_replace(SITE_URL,base_url(),$data);
return $data;
}
}
In order to use base_url() don't forget to load url helper in
application/config/autoload.php
As you pass the $data['common_row'] into the view section
$this->load->view('home_view',$data) the data has been converted into a array is not object so when you are trying to get the data in the view you can try something like this <?php echo get_site_url($common_row['services']);?> or <?php echo get_site_url($common_row[0]['services']);?> depending upon the result.
You can debug the code and see the actual value by
print_r($common_row); die(); on the view page.

Codeigniter variable not found message in sub views

I have stacked with the situation , please anyone help me !
Controller ->
public function index() {
$data = array();
$data ['tittle'] = 'Membership Payment';
$data ['page'] = $this->load->view('membership_payment', $data, true);
$data ['breadcrumb'] = $this->load->view('breadcrumb', $data, true);
$data['result'] = $this->payment_model->membership_payment();
$data['db']='---------------';
$this->load->view('master', $data);
View->master page
<?php echo $db; ?>
Master pages working fine , but problem is i have subpages membership_payment , in this page the data array variable are not working ,
Display the variable not found , there is any trick to pass data array to master subpage
Thanks in Advance !
ROB
I'm assuming that you've used $result which is the returned data of membership_payment(); method in the membership_payment view file.
If so, just change the order of the following line and put that over the $data['page'], as follows:
// ...
$data['result'] = $this->payment_model->membership_payment();
$data ['page'] = $this->load->view('membership_payment', $data, true);
// ...

How to pass multiple variable data from model to view

Could you please tell me how my model,controller and view should look like if I want to pass the following variable data($amount1, $amount2, $amount3) to my view file via controller from my model.
case 1: $amount1=100;
case 2: $amount2=500;
case 3: $amount3=1000;
I want to have the variables in a way that I don't have to echo them in any { } example:
foreach ($records as $row){ $i++; ?>
// I don't want to echo those inside in this.
// I want to echo it like this way- <? echo $amount1;?>
}
Thanks in Advance :)
If you pass an array of data from your controller to your view you can access each element as a variable in the view. Here is an example of what I mean:
model:
class Model extends CI_Model
{
public function get_data()
{
$data = array(
'amount1' => 100,
'amount2' => 500,
'amount3' => 1000,
);
return $data;
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
// get data from model
$data = $this->model->get_data();
// load view
$this->load->view('view', $data);
}
}
view:
<h1><?php echo $amount1; ?></h2>
<p><?php echo $amount2; ?></p>
<!-- etc... -->
I have found a solution myself. I just wanted to share so that it can help others. So here it is..
Your model should look like following :
function net_income(){
$data['amount1']=50;
$data['amount2']=100;
return json_encode($data);
}
Your controller:
function add(){
$this->load->model('mod_net_income');
$json = $this->mod_net_income->net_income();
$obj = json_decode($json);
$data['amount1']= $obj->{'amount1'};
$this->load->view('your_viewfile_name',$data);
}
And then in your view file: just
<? echo "$amount" ; ?>
Thanks :)

Codeigniter num_row returns "array" instead of number

Alright, Im trying to count all the rows where "Membership_Status" = Active. The result I get right now is "Array" instead of a number.
Here is my model
class Report_model extends Model
{
function count_members()
{
$query = $this->db->get_where('Membership', array('Membership_Status' => 'Active'));
return $query->num_rows();
}
}
Here is my controller
class Report extends Controller {
function YTD_report()
{
$data['main_content'] = 'report_membership_view';
$this->load->view('includes/template', $data);
}
}
Here is my view
report_model->count_members();
echo $total;
?>
My result is Array, where according to the db info, it should be 4.
What can I do/change to get it to display the proper number?
thanks
the $data array your passing to the view will create one variable for each key to be used by view...
So your controler once the model is loaded, you should do:
$data['total'] = $this->Report_model->count_members();
Then in the view you can use the $total variable like this:
<?php echo $total; ?>

Resources