CodeIgniter Forums
[Solved] Active Records DB Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forumdisplay.php?fid=18)
+--- Thread: [Solved] Active Records DB Model (/showthread.php?tid=1506)



[Solved] Active Records DB Model - wolfgang1983 - 03-16-2015

When I search in my input put field with the model below. If there is the word that I type does not match the throws foreach error how do stop that?

A PHP Error was encountered

Severity: Notice
Message: Undefined variable: user_groups
Filename: user/users_group_list.tpl
Line Number: 85
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: user/users_group_list.tpl
Line Number: 85

Throw errors only show up if type something that does not match what the best way so if I type something that does not match will not throw errors

PHP Code:
public function getUserGroups($limit null$offset NULL) {
$this->db->select("user_group_id, name");
$this->db->from($this->db->dbprefix 'user_group');
$this->db->limit($limit$offset);
$this->db->order_by('name''asc');
$data = array(
'name' => $this->input->post('name'),
'user_group_id' => $this->input->post('user_group_id')
);
$this->db->like($data);
$this->db->or_like('name''match');
$this->db->or_like('user_group_id''match');
$query $this->db->get();
return 
$query->result_array();




RE: Active Records DB Model - CroNiX - 03-16-2015

The problem would be addressed in the view, where you are using the data, not in the model.

it would be something like:
PHP Code:
<?php if(count($data)): //check to see if there ARE users returned ?>
<?foreach($data as $d): //yes, list them ?>
<div><?=$d['name']; ?></div>
<?php endforeach; ?>
<?php 
else: //no, display message ?>
<p>Sorry, no users matching criteria.</p>
<?php endif; ?>



RE: Active Records DB Model - wolfgang1983 - 03-16-2015

(03-16-2015, 07:46 AM)CroNiX Wrote: The problem would be addressed in the view, where you are using the data, not in the model.

it would be something like:



PHP Code:
<?php if(count($data)): //check to see if there ARE users returned ?>
<?foreach($data as $d): //yes, list them ?>
<div><?=$d['name']; ?></div>
<?php endforeach; ?>
<?php 
else: //no, display message ?>
<p>Sorry, no users matching criteria.</p>
<?php endif; ?>

I have tried your way and my way no luck still showing error


Code:
<?php echo Modules::run('admin/common/header/index');?>

<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">


<?php echo form_open('admin/users_group');?>
<div class="well">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="name" class="control-label">Name</label>
<?php
$data = array(
'id' => 'name',
'name' => 'name',
'class' => 'form-control',
'placeholder' => 'Search By Name',
'value' => set_value('name')
)
;?>
<?php echo form_input($data);?>
<?php echo form_error('name', '<div class="text-danger" style="margin-top: 20px;">', '</div>'); ?>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="user_group_id" class="control-label">User Group ID</label>
<?php
$data = array(
'id' => 'user_group_id',
'name' => 'user_group_id',
'class' => 'form-control',
'placeholder' => 'Search By User Group ID',
'value' => set_value('user_group_id')
)
;?>
<?php echo form_input($data);?>
<?php echo form_error('user_group_id', '<div class="text-danger" style="margin-top: 20px;">', '</div>'); ?>
</div>
<div class="form-group text-right">
<?php
$data_filter = array(
'id' => 'filter',
'name' => 'filter',
'class' => 'btn btn-primary',
'content' => 'Filter',
'type' => 'submit'
);?>
<?php echo form_button($data_filter);?>
</div>
</div>
</div>
</div>
<?php echo form_close();?>

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px;"><?php echo $title;?></div>
<div class="pull-right">
<button type="submit" class="btn btn-danger" form="users_group"><i class="fa fa-trash"></i> Delete</button>
<a href="<?php echo base_url('admin/users_group/add');?>" class="btn btn-success"> <i class="fa fa-plus"></i> Add User Group</a></div>
</div>
<div class="panel-body">
<?php if ($success) { ?>
    <div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?>
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
<?php } ?>
<?php echo form_open('admin/users_group/delete');?>

<div class="table-responsive">
<table  class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="text-left">User Group ID</th>
<th class="text-left">Name</th>
<th class="text-right">Action</th>
</tr>
</thead>
<?php if ($users_group) {?>
<?php foreach ($users_group as $user_group) { ?>
    <tr>
    <td class="text-left user_group_id"><?php echo $user_group['user_group_id']; ?></td>
    <td class="text-left name-user-group"><?php echo $user_group['name']; ?></td>
    <td class="text-right"><a href="<?php echo $user_group['edit']; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i> Edit</a></td>
    </tr>
<?php } ?>
<?php } else { ?>

<p>No Results</p>

<?php } ?>
</table>
</div>

<?php echo form_close();?>
</div>
<div class="panel-footer clearfix">
<div class="pull-left pag-user-group"><?php echo $pagination; ?></div>
<div class="pull-right" style="padding-top: 7.5px;"><?php echo $results; ?></div>
</div>
</div>

</div>
</div>

</div><!-- # Page Inner End -->
</div><!-- # Page End -->

</div><!-- # Wrapper End -->

<?php echo Modules::run('admin/common/footer/index');?>
Controller:


PHP Code:
    public function index($offset 0) {

        
$this->load->model('admin/user/model_user_group');

        
$this->load->library('pagination');

        
$data['title'] = "Users Group";

        
$success $this->session->userdata('success');

        if (isset(
$success)) {
            
$data['success'] = $this->session->userdata('success');
            
$this->session->unset_userdata('success');
        } else {
            
$data['success'] = '';
        }

        
$page_number $this->uri->segment(3);

        if (isset(
$page_number)) {
            
$page $page_number;
        } else {
            
$page 0;
        }

        
$data['users_group'] = array();

        
$admin_limit 20;
    
        
$user_group_total $this->model_user_group->getTotalUserGroups();

        
$results $this->model_user_group->getUserGroups($admin_limit$this->uri->segment(3));

        
$config['base_url'] = base_url() . 'admin/users_group';
        
$config['total_rows'] = $user_group_total;
        
$config['per_page'] = $admin_limit;
        
$config['uri_segment'] = 3;

        
$config['full_tag_open'] = "<ul class='pagination'>";
        
$config['full_tag_close'] ="</ul>";
        
$config['num_tag_open'] = '<li>';
        
$config['num_tag_close'] = '</li>';
        
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        
$config['next_tag_open'] = "<li>";
        
$config['next_tagl_close'] = "</li>";
        
$config['prev_tag_open'] = "<li>";
        
$config['prev_tagl_close'] = "</li>";
        
$config['first_tag_open'] = "<li>";
        
$config['first_tagl_close'] = "</li>";
        
$config['last_tag_open'] = "<li>";
        
$config['last_tagl_close'] = "</li>";

        
$this->pagination->initialize($config); 

        
$data['pagination'] = $this->pagination->create_links();

        foreach (
$results as $result) {
            
$data['users_group'][] = array(
                
'user_group_id' => $result['user_group_id'],
                
'name' => $result['name'],
                
'edit' => site_url('admin/users_group/edit' .'/'$result['user_group_id'])
            );
        }

        
$paginations_lang "Showing %d to %d of %d (%d Pages)";

        
$data['results'] = sprintf($paginations_lang, ($user_group_total) ? (($page 0) * $admin_limit) + 0, ((($page 0) * $admin_limit) > ($user_group_total $admin_limit)) ? $user_group_total : ((($page 0) * $admin_limit) + $admin_limit), $user_group_totalceil($user_group_total $admin_limit));

        
$this->load->view('template/user/users_group_list.tpl'$data);
    } 



RE: [Solved] Active Records DB Model - wolfgang1983 - 03-16-2015

(03-16-2015, 07:46 AM)CroNiX Wrote: The problem would be addressed in the view, where you are using the data, not in the model.

it would be something like:

PHP Code:
<?php if(count($data)): //check to see if there ARE users returned ?>
<?foreach($data as $d): //yes, list them ?>
<div><?=$d['name']; ?></div>
<?php endforeach; ?>
<?php 
else: //no, display message ?>
<p>Sorry, no users matching criteria.</p>
<?php endif; ?>

All solved now I had to tinker with my model and view and controller all good now.