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 } ?>
Related
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)
?>
I'm trying to delete data by id but it doesn't work.
I have some code like this.
Controller :
public function delete()
{
if (isset($_GET['del'])) {
$ii = $this->input->post('id');
$this->P6_model->delete($ii);
$this->load->view('p6home');
}
}
Model :
public function delete($i)
{
return $this->db->delete('contacts', array('id' => $i));
}
Routes :
$route['deldata'] = 'p6/delete';
View :
foreach ($contacts as $data) {
?>
<tr>
<input type="hidden" name="id" value="<?=$data->id?>"/>
<td data-label="Name"><?=$data->name?></td>
<td data-label="address"><?=$data->address?></td>
<td data-label="phone"><?=$data->phone?></td>
<td>
<button class="positive ui button">Update</button>
<button class="negative ui button" onclick="return confirm('Are you sure?')" name="del" href="deldata">Delete</button>
</td>
</tr>
<?php
}
?>
Thanks before.
you must have declared base_url, right? Use that in your link to redirect a page. Also, give the get parameter in the URL itself. POST will not work without a form. This should work for you. View:
<?php
foreach ($contacts as $data) {
?>
<tr>
<td data-label="Name"><?=$data->name?></td>
<td data-label="address"><?=$data->address?></td>
<td data-label="phone"><?=$data->phone?></td>
<td>
<button class="positive ui button">Update</button>
<button class="negative ui button" onclick="return confirm('Are you sure?')" name="del" href="<?php echo base_url('deldata')."/$data->id"; ?>">Delete</button>
</td>
</tr>
<?php
}
?>
Controller:
public function delete($id)
{
$this->P6_model->delete($id);
$this->load->view('p6home'); //instead of loading the view here, try redirecting to a controller function and load the view there.
}
On my form each row has it's on submit button and you need to check the check box before you can delete it else it should through error.
Question: My checkbox is in_array but if I do not check the box and then press submit it does not through the codeigniter form_validation error. I have used $this->form_validation->set_rules('selected[]', 'Selected', 'required'); But error not showing up.
What is the best solution in making it getting form_validation error to work?
View
<?php echo form_open('admin/design/layout/delete'); ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td style="width: 1px;" class="text-center">
<input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
</td>
<td>Layout Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($layouts) { ?>
<?php foreach ($layouts as $layout) { ?>
<tr>
<td class="text-center"><?php if (in_array($layout['layout_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" />
<?php } ?>
</td>
<td><?php echo $layout['name']; ?></td>
<td class="text-right">
<button type="submit" class="btn btn-danger">Delete</button>
Edit
</td>
</tr>
<?php } ?>
<?php } else { ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php echo form_close(); ?>
Controller Function
public function delete() {
$this->load->library('form_validation');
$this->form_validation->set_rules('selected[]', 'Selected', 'required');
if ($this->form_validation->run() == FALSE) {
echo "Error";
$this->get_list();
} else {
$selected_post = $this->input->post('selected');
if (isset($selected_post)) {
foreach ($selected_post as $layout_id) {
}
echo "Deleted $layout_id";
$this->get_list();
}
}
}
It won't validate per field. selected[] selector in rules means, when you submit your form, it should be at least one selected item. Now you have submit buttons, which are independently submit the form, no matter where are they in the dom, and which checkboxes are selected.
Currently it the same, as you would have one submit button at the end.
I would add some javascript, and set if the checkbox is not selected, you can disable that field:
<script>
$(function() {
$('input:checkbox').on('change', function() {
if($(this).is(':checked')) {
$(this).closest('tr').find('button:submit').prop('disabled', false);
}
else {
$(this).closest('tr').find('button:submit').prop('disabled', true);
}
})
})
</script>
And add disabled to your submit buttons:
<button type="submit" class="btn btn-danger" disabled>Delete</button>
It's not a server side validation, but you can achieve to prevent push the button next unchecked checkboxes.
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 using the joomla newsflash in order to display the 5 most recent items in my home page. the newsflash is attached to my news & events category. now I need that one of the news & events items will not be displayed in my home newsFlash. is it possible?, thanks
For Joomla 1.5.26, a template override might be your best bet. See here for how that's done: http://forum.joomla.org/viewtopic.php?t=145996
In your case, you'd create a new file at
/templates/your_template/html/mod_newsflash/_item.php
You'd put the following code in that file. Be sure to replace "99" with the id of your article. You can get that from the Article Manager table.
<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<?php
$my_article_id = $item->id;
if ($my_article_id !== '99') :
if ($params->get('item_title')) : ?>
<table class="contentpaneopen<?php echo $params->get( 'moduleclass_sfx' ); ?>">
<tr>
<td class="contentheading<?php echo $params->get( 'moduleclass_sfx' ); ?>" width="100%">
<?php if ($params->get('link_titles') && $item->linkOn != '') : ?>
<a href="<?php echo $item->linkOn;?>" class="contentpagetitle<?php echo $params->get( 'moduleclass_sfx' ); ?>">
<?php echo $item->title;?></a>
<?php else : ?>
<?php echo $item->title; ?>
<?php endif; ?>
</td>
</tr>
</table>
<?php endif; ?>
<?php if (!$params->get('intro_only')) :
echo $item->afterDisplayTitle;
endif; ?>
<?php echo $item->beforeDisplayContent; ?>
<table class="contentpaneopen<?php echo $params->get( 'moduleclass_sfx' ); ?>">
<tr>
<td valign="top" ><?php echo $item->text; ?></td>
</tr>
<tr>
<td valign="top" >
<?php if (isset($item->linkOn) && $item->readmore && $params->get('readmore')) :
echo '<a class="readmore" href="'.$item->linkOn.'">'.$item->linkText.'</a>';
endif; ?>
</td>
</tr>
</table>
<?php endif; ?>
The relevant code is in the second and final PHP tags, where I've inserted an IF check which looks for your article ID and skips it if it matches.