When I try to display the crud, it gives me an error in the foreach. I don't understand the error because I haven't been studying php for long.
I code on Codeigniter4 and here is my code:
<?php
foreach($membres_detail as $row){
?>
<tr id="<?php echo $row->id; ?>">
<td><?php echo $row->id; ?></td>
<td><?php echo $row->pseudo; ?></td>
<td><?php echo $row->email; ?></td>
<td>
<a data-id="<?php echo $row->id; ?>" class="btn btn-primary btnEdit">Modifier</a>
<a data-id="<?php echo $row->id; ?>" class="btn btn-danger btnDelete">Supprimer</a>
</td>
</tr>
<?php
}
?>
My Controller :
public function index()
{
$model = new MemberModel();
$data['membres_detail'] = $model->orderBy('id', 'DESC')->findAll();
return view('list', $data);
}
My Model :
class MemberModel extends Model
{
protected $table = 'membres';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = Entities\MemberEntity::class;
protected $allowedFields = ['email','pseudo','password','id_role','status'];
The Error:
ErrorException
Undefined variable $membres_detail
APPATH\Views\list.php at line 35
<?php
foreach($membres_detail as $row)
?>
Related
When I search a keyword and paginate it, it returns data that matches the like query in the database.
A PHP Error was encountered
Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: admin/view_member.php
Line Number: 105
Backtrace:
File: C:\xampp\htdocs\cms_new\application\views\admin\view_member.php
Line: 105
Function: _error_handler
File: C:\xampp\htdocs\cms_new\application\controllers\admin\Member.php
Line: 63
Function: view
File: C:\xampp\htdocs\cms_new\index.php
Line: 315
Function: require_once
MY Controller
$search = ($this->input->post("member_name"))? $this->input->post("member_name") : "NIL";
$search = ($this->uri->segment(3)) ? $this->uri->segment(3) : $search;
// pagination settings
$config = array();
$config['base_url'] = site_url("admin/member/index/$search");
$config['total_rows'] = $this->Model_member->record_count($search);
$config['per_page'] = "3";
$config["uri_segment"] = 4;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
// integrate bootstrap pagination
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// get books list
$data['users'] = $this->Model_member->fetch_data($config['per_page'], $data['page'], $search);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('admin/view_header',$data);
$this->load->view('admin/view_member',$data);
$this->load->view('admin/view_footer');
My Model
function fetch_data($limit, $start, $st = NULL)
{
if($st == "NIL")
$st = "";
$this->db->select('*');
$this->db->from('tbl_member');
$this->db->like('member_name', $st);
$this->db->or_like('member_email', $st);
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//var_dump($query->result());
foreach ($query->result() as $row) {
$data[] = $row;
}
//print_r($query);
return $data;
}
if ($query->num_rows() == 0) {
$this->session->set_flashdata('recoard','<div class="alert alert-danger text-center">Record Not Found! </div>');
}
return false;
}
function record_count($st = NULL)
{
if($st == "NIL")
$st = "";
$this->db->select('*');
$this->db->from('tbl_member');
$this->db->like('member_name', $st);
$this->db->or_like('member_email', $st);
$query = $this->db->get();
return $query->num_rows();
var_dump($query->num_rows());
}
My View
<?php
$user = count($users);
var_dump($user);
for ($i = 0; $i < $user; ++$i) {
if(! $users ){
// Faild Message
echo $this->session->flashdata('recoard');
}
else {
?>
<tbody>
<tr>
<td class="text-center"><img class="rounded-circle img-fluid avatar-40" src="<?php echo base_url();; ?>public/uploads/<?php echo $users[$i]->member_photo; ?>" alt="profile"></td>
<td><?php echo $users[$i]->member_name; ?></td>
<td><?php echo $users[$i]->member_email; ?><br>
<?php echo $users[$i]->member_mobile; ?></td>
<td><?php echo $users[$i]->member_city; ?><br>
<?php echo $users[$i]->member_state; ?><br>
<?php echo $users[$i]->member_country; ?></td>
<td><?php echo $users[$i]->member_address; ?></td>
<td>
<?php
if($users[$i]->member_access == 1)
{
echo '<span class="badge iq-bg-primary">Active</span></td>';
}
else
{
echo '<span class="badge iq-bg-warning">Inactive</span></td>';
}
?>
</td>
<td>
<div class="flex align-items-center list-user-action">
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Change Status" href="<?php echo base_url(); ?>admin/member/change_status/<?php echo $users[$i]->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="ri-user-add-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="<?php echo base_url(); ?>admin/member/edit/<?php echo $users[$i]->member_id; ?>"><i class="ri-pencil-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" href="<?php echo base_url(); ?>admin/member/delete/<?php $users[$i]->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="iq-bg-danger ri-delete-bin-line"></i></a>
</div>
</td>
</tr>
</tbody>
<?php } } ?>
you seem to not be returning an array to count & loop over, this is why you have the warning.
Also you have the failed message inside your loop that will make more errors later on.
Hope this helps & works for you
<?php
if (!$users || !is_array($users)) {
// Faild Message
echo $this->session->flashdata('recoard');
} else {
foreach ($users as $user) { ?>
<tbody>
<tr>
<td class="text-center"><img class="rounded-circle img-fluid avatar-40" src="<?php echo base_url("public/uploads/"); ?><?php echo $user->member_photo; ?>" alt="profile"></td>
<td><?php echo $user->member_name; ?></td>
<td><?php echo $user->member_email; ?><br>
<?php echo $user->member_mobile; ?></td>
<td><?php echo $user->member_city; ?><br>
<?php echo $user->member_state; ?><br>
<?php echo $user->member_country; ?></td>
<td><?php echo $user->member_address; ?></td>
<td>
<?php
if ($user->member_access == 1) {
echo '<span class="badge iq-bg-primary">Active</span></td>';
} else {
echo '<span class="badge iq-bg-warning">Inactive</span></td>';
}
?>
</td>
<td>
<div class="flex align-items-center list-user-action">
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Change Status" href="<?php echo base_url(); ?>admin/member/change_status/<?php echo $user->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="ri-user-add-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="<?php echo base_url(); ?>admin/member/edit/<?php echo $user->member_id; ?>"><i class="ri-pencil-line"></i></a>
<a data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" href="<?php echo base_url(); ?>admin/member/delete/<?php $user->member_id; ?>" onClick="return confirm('Are you sure?');"><i class="iq-bg-danger ri-delete-bin-line"></i></a>
</div>
</td>
</tr>
</tbody>
<?php }
} ?>
I am getting result from two table in my database. Table one has Project name and table two has sub project names under project name this two table is connected by a id.
Now when I output result using foreach by joining two table it is showing showing project name repeatedly as there are more than one subproject under that repeated project.
I want that the project name will be single and subproject result will loop under the main project name. How to do that?
I amusing codeigniter for development.
my view page:
<?php foreach ($reportlist as $project_item): { ?>
<tr>
<td>
<form method='post' action='update_project'>
<input type='hidden' value='<?php echo ($project_item['pid']); ?>' name='pid'>
<input type='hidden' value='<?php echo $project_item['pp_id']; ?>' name='pp_id'>
<input type="submit" class="linkButton" value="<?php echo $project_item['p_name'] ?>">
</form>
</td>
<td>
<form method='post' action='update_subproject'>
<input type='hidden' value='<?php echo $project_item['pid']; ?>' name='pid'>
<input type='hidden' value='<?php echo $project_item['pp_id']; ?>' name='pp_id'>
<input type="submit" class="linkButton" value="<?php echo $project_item['component_name'] ?>" >
</form>
</td>
<td><?php echo $project_item['coordinating_authority'] ?></td>
<td><?php echo $project_item['start_month'] ?></td>
<td><?php echo $project_item['target_month'] ?></td>
<td><?php echo $project_item['stat_pro'] ?></td>
<td><?php echo $project_item['stat_pro'] ?></td>
<td><?php echo $project_item['remark_pro'] ?></td>
</tr>
<?php } endforeach ?>
</tbody>
My cotroller page:
$this->load->model('projsubproj_add_model');
$data['reportlist'] = $this->projsubproj_add_model->existing_project();
$this->load->view('PAGE-DESIGN/adminheader');
$this->load->view('existing_project_view', $data);
$this->load->view('PAGE-DESIGN/footer');
and my model:
public function existing_project()
{
{
$this->db->select('project_tab.p_name,
pp_project_tab.pp_id AS pp_id,
pp_project_tab.pid AS pid,
pp_project_tab.ppname AS component_name,
pp_project_tab.pp_smonth AS start_month,
pp_project_tab.pp_emonth AS target_month,
pp_project_tab.pp_cordn_reqrd AS coordinating_authority,
pp_project_tab.pps_status AS stat_pro,
pp_project_tab.remarks AS remark_pro');
$this->db->from('project_tab');
$this->db->join('pp_project_tab', 'pp_project_tab.pid =project_tab.pid');
//$this->db->join('pp_status_tab', 'pp_status_tab.pp_id = pp_project_tab.pp_id');
$this->db->order_by('ppname', 'desc');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('project_tab');
return $query->row_array();
}
I got the solution: I just edited my view file for through foreach loop. I made two function to to extract results from two tables and joined them with condition that if the pid of subproject table matches with the pid of subject table, then echo subproject name and its components inside foreach loop of Project loop.
Here is my code:
$projects is for extracting data from project_tab,
$components is for extracting data from pp_project_tab
<?php
foreach ($projects as $project) {
echo $project['p_name'];
foreach ($components as $component) {
if ($project['pid']==$component['pid']) {
echo $component['ppname'] ;
}
}
}
?>
i am getting error message: Invalid argument for foreach() in my View. I wanted to display all entries in my mysql table but i kept on getting error message. I am a newbie in Codeigniter and couldn't really figure out how to solve this. The codes are the following... help me please
user_model.php
<?php
foreach ($daftar as $data) :
?>
<tr>
<td><?php echo $data->id_petugas; ?></td>
<td></td>
<td></td>
<td></td>
<td>
<button class="btn edit"><i class="icon-edit"></i></button>
<button class="btn btn-danger remove" data-toggle="confirmation">
<i class="icon-remove"></i></button>
</td>
</tr>
<?php
endforeach;
?>
user_controller
<?php
class User_controller extends CI_Controller{
function __Construct()
{
parent ::__construct();
}
function user(){
$this->load->model('user_model');
$data['daftar'] = $this->user_model->get_user_all();
$this->load->view('daftar_user',$data);
}
daftar_user.php
<?php
foreach ($daftar as $data) :
?>
<tr>
<td><?php echo $data->id_petugas; ?></td>
<td></td>
<td></td>
<td></td>
<td>
<button class="btn edit"><i class="icon-edit"></i></button>
<button class="btn btn-danger remove" data-toggle="confirmation">
<i class="icon-remove"></i></button>
</td>
</tr>
<?php
endforeach;
?>
i've a bug from this file
This is not a proper coding technique you were using. So please correct your model query code first like:
$query = $this->db->from('petugas')->order_by('id_petugas','ASC')->get()->result();
return $query;
No need to change anything else. You will get your all data in view.
I hope this will help you.
function get_user_all()
{
$query=$this->db->query("select * from petugas order by asc");
return $query->result();
$this->db->select('*');
$this->db->from('petugas');
$this->db->order_by('id_petugas','ASC');
$query=$this->db->get();
if($query->num_rows() > 0){
foreach($query->result() as $data){
$daftar[]= $data;
}
return $daftar;
}
}
ok, it worked .. thanks a lot
my login system is ok. It's working.. I have three user type 1.admin 2.customer service 3. waiter.
I need , if login into admin and cs or waiter. its redirect three different view...
I've tried divert the two different views, how should assign more than two views?
<?php if($level == "1"){ ?>
<?php echo $this->view('admin/template'); ?>
<?php } else { ?>
<?php echo $this->view('cs/template'); ?>
<?php } ?>
I would like to know if it is possible to get a $variable id mixed in with the codeigniter.
Currently just shows http://localhost/codeigniter/codeigniter-cms/0
I just need it to display http://localhost/codeigniter/codeigniter-cms/admin/setting with id hidden and still access this controller.
But If create new website it has different ID so different settings.
foreach ($results as $result) {
$data['websites'][] = array(
'website_id' => $result->website_id,
'name' => $result->name,
'url' => $result->url,
'edit' => site_url('admin/setting') . $result->website_id
);
}
View
<?php if ($websites) { ?>
<?php foreach($websites as $website) { ?>
<tr>
<td class="text-center"><?php if (in_array($website['website_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $website['website_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $website['website_id']; ?>" />
<?php } ?></td>
<td><?php echo $website['name'];?></td>
<td><?php echo $website['url'];?> </td>
<td class="text-right"><i class="fa fa-pencil"></i> Edit Website</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="4">NO RESULTS</td>
</tr>
<?php } ?>
I can view link now changed edit on controller and made on model result_array instead of result. link working fine.
'edit' => site_url('admin/setting', $result['website_id'])
controller updated
$results = $this->model_website->getWebsites();
foreach ($results as $result) {
$data['websites'][] = array(
'website_id' => $result['website_id'],
'name' => $result['name'],
'url' => $result['url'],
'edit' => site_url('admin/setting', $result['website_id'])
);
}
model updated
function getWebsites() {
$this->db->order_by('url', 'asc');
$query = $this->db->get('website');
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
I got a fatal error with my component. Let's say the component name is com_shirts and this is my code
my code in administrator/component/com_shirts/controllers/code.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
class ShirtsControllerCode extends JControllerForm{
protected $view_list = 'codes';
}
my code in administrator/component/com_shirts/controllers/codes.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');
class ShirtsControllerCodes extends JControllerAdmin{
protected $text_prefix = 'COM_SHIRTS';
function getModel($name='Code', $prefix='ShirtsModel', $config=array('ignore_request'=>TRUE)){
$model = parent::getModel($name, $prefix, $config);
return $model;
}
}
my code in administrator/component/com_shirts/models/code.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
class ShirtsModelCode extends JModelAdmin{
function getTable($type='Code', $prefix='ShirtsTable', $config=array()){
return JTable::getInstance($type, $prefix, $config);
}
function getForm($data=array(), $loadData=TRUE){
$form = $this->loadForm();
return $form;
}
}
my code in administrator/component/com_shirts/models/codes.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.modellist');
class ShirtsModelCodes extends JModelList{
function getItems(){
$items = parent::getItems();
foreach($items as &$item){
$item->url = 'index.php?option=com_shirts&task=code.edit&code_id='.$item->code_id;
$item->event_description = substr($item->code_desc, 0);
}
return $items;
}
function getListQuery(){
$query = parent::getListQuery();
$query->select('*');
$query->from('#__shirts_codes');
return $query;
}
}
my code in administrator/component/com_shirts/tables/code.php
<?php
defined('_JEXEC') or die;
class ShirtsTableCode extends JTable{
public function __construct(&$dbo){
parent::__construct('#__shirts_codes', 'code_id', $dbo);
}
}
my code in administrator/component/com_shirts/views/codes/view.html.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class ShirtsViewCodes extends JView{
protected $codes;
function display($tmpl=NULL){
$this->codes = $this->get('Items');
$this->toolbar();
parent::display($tmpl);
}
function toolbar(){
JToolBarHelper::title(JText::_('HEADER'));
JToolBarHelper::addNew('code.add');
JToolBarHelper::editList('code.edit');
JToolBarHelper::divider();
JToolBarHelper::publishList('codes.publish');
JToolBarHelper::unpublishList('codes.unpublish');
JToolBarHelper::divider();
JToolBarHelper::archiveList('codes.archive');
JToolBarHelper::trash('codes.trash');
JToolBarHelper::divider();
}
}
my code in administrator/component/com_shirts/views/codes/tmpl/default.php
<?php defined('_JEXEC') or die; ?>
<form action="index.php?option=com_shirts&view=codes" method="post" name="adminForm" id="adminForm">
<table class="adminlist">
<thead>
<tr>
<th style="width: 1%">
<input type="checkbox" name="checkall-toggle" value="" onclick="checkAll(this)" />
</th>
</tr>
</thead>
<tbody>
<?php foreach($this->codes as $i=>$code):
$url = 'index.php?option=com_shirts&view=code&task=code.edit&event_id='.$code->code_id;
?>
<tr class="row<?php echo $i%2; ?>">
<td class="center">
<?php echo JHtml::_('grid.id', $i, $code->code_id); ?>
</td>
<td class="center">
<a href="<?php echo $code->url; ?>">
<?php echo $this->escape($code->code); ?>
</a>
</td>
<td>
<a href="<?php echo $code->url; ?>">
<?php echo $this->escape($code->code_desc); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHtml::_('form.token'); ?>
</form>
my code in administrator/component/com_shirts/views/code/tmpl/edit.php
<?php defined('_JEXEC') or die; ?>
<form
method="post"
name="adminForm"
class="form-validate"
action="index.php?option=com_shirts&code_id=<?php echo $this->code->code_id; ?>"
>
<div class="width-50 fltlft">
<fieldset class="adminform">
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('required') as $field): ?>
<li>
<?php echo $field->label; ?>
<?php echo $field->input; ?>
</li>
<?php endforeach; ?>
</fieldset>
</div>
<div class="width-40 fltrt">
<fieldset class="adminform">
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('optional') as $field): ?>
<li>
<?php echo $field->label; ?>
<?php echo $field->input; ?>
</li>
<?php endforeach; ?>
</ul>
</fieldset>
</div>
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
my code in administrator/component/com_shirts/views/code/view.html.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class ShirtsViewCode extends JView{
protected $code;
protected $form;
function display($tmpl=NULL){
$this->event = $this->get('Item');
$this->form = $this->get('Form');
$this->toolbar();
parent::display($tmpl);
}
function toolbar(){
if($this->code->code_id){
JToolBarHelper::title(JText::_('COM_SHIRTS_EDIT'));
}else{
JToolBarHelper::title(JText::_('COM_SHIRTS_ADD'));
}
JToolBarHelper::apply('code.apply', 'JTOOLBAR_APPLY');
JToolBarHelper::save('code.save', 'JTOOLBAR_SAVE');
JToolBarHelper::save2new('code.save', 'JTOOLBAR_SAVE_AND_NEW');
JToolBarHelper::divider();
JToolBarHelper::cancel('code.cancel');
}
}
and when I enter the component view, I get this error
"PHP Fatal error: Call to a member function getKeyName() on a non-object in /var/www/joomla/libraries/joomla/application/component/controllerform.php on line 393"
How do I fix this?
OK that was my bad i had mistake in manifest.xml everything is good now
In manifest there was no line
<folder>tables</folder>