Undefined variable (Codeigniter) - php

I have this error in my view and cannot find out where is the problem:
MY Controller name 'Question':
public function show_all_question_set() {
$data = array();
$data['question_set'] = $this->Question_Model->select_all_question_set();
$this->load->view('question/make_set', $data);
}
My Model name 'Question_Model':
public function select_all_question_set() {
$this->db->select('*');
$this->db->from('tbl_question_set');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
My view name 'make_set':
<?php
foreach ($question_set as $all_set) {
?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td> <?php echo $all_set->exam_name; ?> </td>
<td> <?php echo $all_set->subject_name; ?> </td>
<td> <?php echo $all_set->set_name; ?> </td>
<td align="center">
<em class="fa fa-book"> দেখুন </em>
</td>
</tr>
<?php } ?>
Table : tbl_question_set(set_id, exam_name, subject_name, set_name)

$this->load->model('Question_Model');
add this line to load the model if model is not autoloaded.

i think you are getting error because you write model name in capital letter.. please try this. paste this code in your model hopefully it will work..
public function show_all_question_set() {
$data = array();
$data['question_set'] = $this->question_model->select_all_question_set();
$this->load->view('question/make_set', $data);
}

Related

Codeigniter : Compare two dates and show it on a table with listing

I want to compare two dates and show them front of my article
difference date example.
ArtOne ( +2Hours ) | 10/10/2019 | something
SQl
CREATE TABLE `mytable` (
`id` int(11) NOT NULL,
`PERSONNE_A_LIVRER` text NOT NULL,
`ETAT` text NOT NULL,
`DATE_TRANSPORT` text NOT NULL,
`DATE_LIVRER` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DATE_TRANSPORT = 01/10/2019 11:38
DATE_LIVRER = 02/10/2019 11:38
...
All i want to know diff in hoours in in a column.
here is my model,
public function select_all() {
$sql = "SELECT * FROM mytable";
$data = $this->db->query($sql);
return $data->result();
}
code of my Controller
public function listart() {
$data['dataPegawai'] = $this->M_pegawai->select_all();
$this->load->view('pegawai/list_data', $data);
}
function differenceInHours($startdate,$enddate) {
$starttimestamp = strtotime($startdate);
$endtimestamp = strtotime($enddate);
$difference = abs($endtimestamp - $starttimestamp)/3600;
return $difference;
}
code of my view html and php
<?php
foreach ($dataPegawai as $albat) {
?>
<tr>
<td style="min-width:90px;">
<?php echo $albat->PERSONNE_A_LIVRER ; ?>
</td>
<td>
<?php echo $albat->N_LIVRAISON;?>
</td>
<td>
<?php echo $hours_difference = differenceInHours($albat->DATE_EXP,$albat->DATE_TRANSPORT);?>
</td>
<td>
<?php echo $albat->DESTINATAIRE;?>
</td>
<td>
<?php echo $albat->DIRECTION_A_LIVRER ;?>
</td>
<td>
<?php echo $albat->DESCRIPCION_ETAT;?>
</td>
<td class="text-center" style="min-width:90px;">
<button class="btn default konfirmasiHapus-articulo"> List it </button>
</td>
</tr>
<?php
}
?>
and thanks in advance
For example you want to compare both DATE_EXP and DATE_TRANSPORT value, you could call the differenceInHours() function on the controller instead, not on the view, because it's a good practice to keep the view for presentation only.
Modify the controller like this :
public function listart() {
$data['dataPegawai'] = $this->M_pegawai->select_all();
foreach ($data['dataPegawai'] as $key => $value) {
$startdate = $value->DATE_EXP;
$enddate = $value->DATE_TRANSPORT;
$data['dataPegawai']->$key->hourDifference = $this->differenceInHours($startdate, $enddate);
}
$this->load->view('pegawai/list_data', $data);
}
function differenceInHours($startdate,$enddate) {
$starttimestamp = strtotime($startdate);
$endtimestamp = strtotime($enddate);
$difference = abs($endtimestamp - $starttimestamp)/3600;
return $difference;
}
And the view like this :
<?php
foreach ($dataPegawai as $albat) {
?>
<tr>
<td style="min-width:90px;">
<?php echo $albat->PERSONNE_A_LIVRER ; ?>
</td>
<td>
<?php echo $albat->N_LIVRAISON;?>
</td>
<td>
<?php echo $albat->hourDifference;?>
</td>
<td>
<?php echo $albat->DESTINATAIRE;?>
</td>
<td>
<?php echo $albat->DIRECTION_A_LIVRER ;?>
</td>
<td>
<?php echo $albat->DESCRIPCION_ETAT;?>
</td>
<td class="text-center" style="min-width:90px;">
<button class="btn default konfirmasiHapus-articulo"> List it </button>
</td>
</tr>
<?php
}
?>
now i've tried this and it doesn't work, it shows me error message as follows :
A PHP Error was encountered Severity: Notice
Message: Undefined variable: hourDifference
My controller i edited
public function tampil() {
$data['dataPegawai'] = $this->M_pegawai->select_all();
foreach ($$data['dataPegawai'] as $item => $value) {
$D1 = $value->OBSERVATION;
$D2 = $value->DATE_TRANSPORT;
$data['dataPegawai']->$key->$hourDifference = $this->differenceInHours($startdate, $enddate);
}
$this->load->view('pegawai/list_data', $data);
}
and then i called those to my view as follows :
<?php
foreach ($dataPegawai as $albatr) {
?>
<tr>
<td><?php echo $albatr->$hourDifference;?> </td><!-- -->
<td class="text-center" style="min-width:230px;">
<i class="glyphicon glyphicon-check-sign"></i> Check </button>
</td>
</tr>
<?php
}
?>
Any Help !

Codeigniter anchor tag hyperlinked to a form in controller but the data shown outside the table

I am using Codeigniter 3 and trying CRUD operation. I have created the basic crud operation and am showing the data in a table however I have linked a paragraph tag in the controller below the table to the form controller, If I want to enter another data
The issue is when I click on the link to enter another data it redirect me the original form in controller but when I enter the data and submit it, The data is shown below the table in the paragraph tag.
I am not able understand why this is happening as the controller is the same
I had faced a similar issue before when redirecting in controller.I had redirected the page after submission to show_form() controller which was basically redirecting the page to $this->load->view('learn/view_form');
in which I have kept a condition that if No data is present click to enter. Now when it redirects to show_form() controller it goes into else condition even if the data is present
CONTROLLER
<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Learning extends CI_Controller{
public function __construct(){
parent::__construct();
$this ->load->helper("url");
$this->load->model("tatti_test");
$this->load->database();
$this->load->helper();
}
//functions should be passed here
//creating a function
function start_learn() {
//this varible
$this->load->view('learn/start_learn');
}
function start_crud(){
$this->load->view('learn/form');
}
function show_form(){
$this->load->view("learn/view_form");
}
function insert_form(){
$name = $this->input->post("u_name");
$email = $this->input->post("u_email");
$mobile = $this->input->post("u_mobile");
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("u_file")){
$file='noimage.png';
}
else {
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->insert_tatti($data);
redirect("learning/view_form");
}
function view_form(){
$data['returned_data']=$this->tatti_test->show_form();
$this->load->view("learn/view_form",$data);
}
function delete_entry(){
$id=$this->uri->segment(3);
$data=$this->tatti_test->for_unlink($id);
$filepath="./assets/images/".$data['file_name'];
unlink($filepath);
$this->tatti_test->delete_entry($id);
redirect('learning/view_form');
}
function time_to_update(){
$id=$this->uri->segment(3);
$data['fetched_update_entry']=$this->tatti_test->update_entry($id);
$this->load->view("learn/update.php",$data); //bus associative array hi leta hai
}
function up_db(){
$name =$this->input->post('up_name');
$email = $this->input->post('up_email');
$mobile = $this->input->post('up_mobile');
$file = $this->input->post('up_file');
$id = $this->input->post('up_id');
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("up_file")){
$data= $this->tatti_test->remove_prev($id);
$file=$data['file_name'];
}
else {
$data= $this->tatti_test->remove_prev($id);
$path="./assets/images/".$data['file_name'];
unlink($path);
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->up_nw($data,$id);
redirect('learning/view_form');
}
} /*this accesses command from main ci controller */
?>
VIEW
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } ?>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
MODEL
<?php
class Tatti_test extends CI_Model{
function insert_tatti($insert_data){
$this->db->insert("f_form",$insert_data);
}
function show_form(){
$query = $this->db->get("f_form");
$response=[];
if ($query->num_rows() > 0){
$response = $query->result_array();
}
else {
$response = 0;
}
return $response;
}
function for_unlink($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response=[];
foreach ($query->result_array() as $rows){
return $response = $rows;
}
}
function delete_entry($id){
$this->db->where("id",$id);
$this->db->delete("f_form");
}
function update_entry($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response = [];
if($query->num_rows() > 0 ){
foreach($query->result_array() as $rows);
$response = $rows;
}
return $response;
}
function up_nw($introduced_data,$id){
$this->db->set($introduced_data);
$this->db->where('id',$id);
$this->db->update('f_form');
}
function remove_prev($id){
$this->db->where('id',$id);
$query = $this->db->get('f_form');
$response = [];
foreach($query->result_array() as $rows){
$response=$rows;
}
return $response;
}
}
?>
This is how the data is showing when clicked on the link below table
enter image description here
You're html formatting is messed up. You should have the closing </table> outside your foreach loop or premature <table> closure.
Also moved the Add another entry link outside the foreach loop. So it only appears once, and your document format not messed up.
You can use this fixed view instead:
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
I realize that you are trying to do CURD,
first of all, try this to improve your code and fill the missing library of codeigniter: https://github.com/avenirer/CodeIgniter-MY_Model
For your code:
No data passed to the view in show_form(),
You should check the form submission and the conditions to load the view,
simple thing to do is follow the best practice using ready scripts,
Hope this will be useful,

Codeigniter-how to foreach and echo project name by id in my view page

$data['posts'] = $this->whiteboard_model->u_getTSrowsindex(array('limit'=>$this->perPage));
$data['postsexp'] = $this->whiteboard_model->u_getTSrowsexp(array('limit'=>$this->perPage));
//print_r($data['postsexp']); exit;
foreach($data['posts'] as $post)
{
$project_id =$post['project_name'];
$data['projectName'] = $this->whiteboard_model->get_project_name($project_id);
print_r($data['projectName']);
}
//This is my controller
public function get_project_name($project_id) {
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
//echo $this->db->last_query();
//return fetched data
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//my model
<?php if(!empty($posts)): foreach($posts as $post):
//print_r($post);?>
<tr>
<td> <?php echo $post['advisor']; ?></td>
<td> <?php echo $post['spentdate']; ?></td>
<td><?php echo $post['project_name']; ?>
<?php if(!empty($projectName)){
foreach($projectName as $prj_name){ //print_r($projectName);
echo $prj_name['project_name'];
}
} ?>
</td>
<td> <?php echo $post['activity']; ?></td>
<td> <?php echo $post['billtype']; ?></td>
<td> <?php echo $post['timespent']; ?></td>
<td> <?php if($post['status'] == 1){?>
<b style = "color:blue;">Pending</b>
<?php }else if($post['status'] == 2){?>
<b style = "color:green;">Approved</b>
<?php }else{?> <b style = "color:red;">Rejected</b>
<?php } ?> </td>
</tr>
<?php endforeach; else: ?>`enter code here`
//my view file
Finally iam not getting proper values from projectName array ..its giving only one name
It seems like you are fetching data using the project_id
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
Here you may be getting only the details of one project where the project_id = $project_id
Comment out that line and see if you get all the project names. Additionally use a print_r($query->result()); to verify your data.
So that it would look like:
$this->db->select('project_name');
$this->db->from('project_list');
//$this->db->where('project_id',$project_id);
$query = $this->db->get();
print_r($query->result());
You can refer more here.

foreach connection foreach and select SUM

How can I join a foreach with another foreach in the query WHERE.
I have stored in the second Foreach the contents with the ID of the first ID and wants to display this in the respective column.
So if in the second column db_buy.tb_buy_shop has the ID 2, this should show in the HTML column where the db_shop.tb_shop_id 2.
view:
<?php
foreach ($shops_where_1 as $shop_where_1):
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
$gesamtsumme = $buy_sum['gesamtsumme'];
}
endforeach;
?>
<tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<td></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" disabled></div></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($gesamtsumme,2,",",".");?>" disabled></div></td>
<td></td>
<td></td>
</tr>
<?php
// endforeach;
endforeach;
?>
Model:
public function shops_where_1()
{
$this->db->select('*');
$this->db->from('db_shop');
$this->db->where('db_shop.tb_shop_buy = 1');
$this->db->order_by('tb_shop_name');
$query = $this->db->get();
return $query->result_array();
}
public function buy_sums()
{
$this->db->select('(SELECT SUM(db_buy.tb_buy_gesamt) FROM db_buy) AS gesamtsumme');
$this->db->select('(SELECT SUM(db_buy.tb_buy_abbezahlt) FROM db_buy) AS abbezahlt', FALSE);
$this->db->select('tb_buy_shop');
$this->db->from('db_buy');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function buy_shop($slug)
{
// if (!$this->session->userdata('logged_in'))
// {
// redirect('users/login');
// }
$data['get_shops'] = $this->Admin_model->get_shops($slug);
$data['shops_where_1'] = $this->Admin_model->shops_where_1();
$this->load->view('templates/header_acp');
$this->load->view('admin/buy_shop', $data);
$this->load->view('templates/footer');
}
If I am correct in what you are asking it seems that you are just wanting to know how to format the code so that it will build the table. This would be how to do so.
<?php
foreach ($shops_where_1 as $shop_where_1):
?> <tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<?php
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
?>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" readonly></div></td>
<?php
}
endforeach; // endforeach 2
</tr>
endforeach; // endforeach 1;
?>

View can't call Controller Function Zend Framework 2

I can't call my controller in view page. even if i use print_r in controller but it didn't show. I have body_product.phtml view code:
<table class="table table-hover table-condensed">
<tbody>
<?php
$i = 1;
//print_r("list produk:".$this->productList);
foreach ($this->productList as $data) {
$desc=explode(",",$data['descriptions']);
?>
<tr>
<th colspan="3">
<input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
<input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
<h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
</th>
</tr>
<tr id="dashe">
<td>
<ul class="myul">
<?php foreach($desc as $descriptions) { ?>
<li class="myli"> <?php echo $descriptions; ?></li>
<?php } ?>
</ul>
</td>
<td>
<h4 class="prize">
<?php setlocale(LC_MONETARY, 'id_ID');
echo money_format('%.2n', $data['package_price']); ?>
/ month
</h4>
</td>
<td>
<p id="btn-hrm" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
and in the indexController:
public function loadProductAction() {
$viewModel = new ViewModel();
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if ($data) {
foreach ($data as $val) {
array_push($arr, $val);
}
}
print_r('teaaat'.$arr);
$viewModel->setVariables(array('productList' => $arr))
->setTerminal(true);
return $viewModel;
}
If I open print_r in the view,it show error Warning: Invalid argument supplied for foreach() in.... I think it cause of view can't call the controller.
Help me please,thanks.
Firstly when you try to use print_r it is expecting an array. So it should be something like print_r($arr). Also give this a try and see if it helps.
public function loadProductAction() {
$storage = Product\Storage::factory($this->getDb());
$productList = new Product($storage);
$data = $productList->loadProduct();
$arr = array();
if (is_array($data) && !empty($data)) {
foreach ($data as $val) {
array_push($arr, $val);
}
} else {
echo '$data is not an array or it is empty';
}
print_r($arr);
return new ViewModel(array(
'productList' => $arr
));
}

Categories