Data not shown in view Codeigniter - php

I am new in Codeigniter and i have problem when trying to display data in view page. I have follow Codeigniter documentation, tutorials and questions in stackoverflow but still no answer can help me althought i run the tutorial and it work perfectly. but when i implement in my code, it give me an error. Hope you guys can help me. I am not sure what the problem. Thank you in advanced.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: b
Filename: home/adminviewbranch.php
Line Number: 81
Form.php(controller)
public function view_branch(){
$this->load->model('branch_model');
$data = array();
$data['b'] = $this->branch_model->branch_view();
$this->load->view('home/adminviewbranch', $data);
}
branch_model.php(model)
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch');
return $query;
}
adminviewbranch.php(view)
<div id="page" class="container">
<table id="table_id" class="display">
<thead>
<tr>
<th>Branch Name</th>
<th>Branch Address</th>
<th>Branch Contact</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(count($b)>0) {
foreach ($b as $row) {
?>
<tr>
<td><?=$row->branch_name;?></td>
<td><?=$row->branch_add;?></td>
<td><?=$row->branch_Hp;?></td>
<td><?=$row->branch_Hp;?></td>
<?php
}
}
else {
echo "No Record found!";
}
?>
</tr>
</tbody>
</table>
</div>

in foreach do foreach ($b->result() as $row)

I think there is missing function in your foreach.
Try this:
<?php
if(count($b)>0)
{
foreach ($b->result() as $row){
?>

Modify your model code like below
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch')->result_array();
return $query;
}

Change your model code to following
public function branch_view(){
//data is retrive from this query
$query = $this->db->get('branch');
return $query->result_array(); // this allows you to fetch results in the form of multidimentional array
}
Then you can access it in view as
<?php
if(count($b)>0)
{
foreach ($b as $row)
{
?>
<tr>
<td><?=$row['branch_name'];?></td>
<td><?=$row['branch_add'];?></td>
<td><?=$row['branch_Hp'];?></td>
<td>Action</td>
</tr>
<?php
}
}
else
{
echo "<tr colspan='3'><td>No Record found!</td></tr>";
}
?>

in model change this line
$query = $this->db->get('branch');
to
$query = $this->db->get('branch')->result();
(OR)
in view change this lines
<?php
if(count($b)>0) {
foreach ($b as $row) {
?>
to
<?php
if($b->num_rows()>0) {
foreach ($b->result() as $row) {
?>

Your model is not result anything.you can check with print_r() function.you must use be returning with object or array.return $query->result();
and your view must be foreach($tes as $t){$t->your_view}
or return $query->row();
with single data $row->your_view

Related

laravel cannot display jsondata

I'm try to use jsondata to display the results.
This is my output.
Now i just try to display the result
public function resultsurvey($survey_id)
{
//$results=[];
$allResults= Results::where('survey_id', $survey_id)->get()->toArray();
$justResults=Results::select('results')->where('survey_id', $survey_id)->get();
$json = json_decode($justResults, true);
$aQuestions = array();
$aAnswers = array();
foreach($json as $sKey => $oItem) {
array_push($aQuestions, $sKey);
array_push($aAnswers, $oItem);
}
dd($aQuestions , $aAnswers);
}
in pure php i just use a but in the laravel it's not work.
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<?php foreach($aQuestions as $aQuestionsn){?>
<th scope="col"><?php echo $aQuestionsn; ?></th>
<?php }?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($aAnswers as $aAnswersn) {?>
<td><?php echo $aAnswersn;?></td>
<?php }?>
</tr>
</tbody>
</table>
</div>
How i can display the jsonstring ?
All i need look like this
well i think in your case there is another array inside your array you need to go inside that array
foreach($aAnswers as $aAnswersn)
{
foreach ($aAnswersn as $value)
{
echo $value;
}
}
At least your json_encode is possibly in the wrong location, try this:
public function resultsurvey($survey_id)
{
//$results=[];
$allResults= Results::where('survey_id', $survey_id)->get()->toArray();
$justResults=Results::select('results')->where('survey_id', $survey_id)->get();
$aQuestions = array();
$aAnswers = array();
// $justResults is a collection so loop through it
foreach($justResults as $sKey => $oItem) {
// $oItem is possibly JSON encoded at this stage
$oItem = json_decode($oItem, true);
array_push($aQuestions, $sKey);
array_push($aAnswers, $oItem);
}
dd($aQuestions , $aAnswers);
}
Please note all changes

Undefined property PHP error on Codeigniter

I am super stuck on a problem. I went through many solution give here related to this but I did not found any solution to my error.
this is the error message I am facing and I used print_r to view what is the output but is also a dead end.
My controller - Search.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search extends CI_Controller {
function index(){
$this->load->model("ModSearch");
$data["get_ztable"] = $this->ModSearch->get_zscore();
$data["get_course"] = $this->ModSearch->getCourse();
$data["get_stream"] = $this->ModSearch->getStream();
$data["get_district"] = $this->ModSearch->getDistrict();
$data["get_uni"] = $this->ModSearch->getUniversity();
$this->load->view('addZscore',$data);
}
Model - ModSearch.php
public function get_zscore(){
$query = $this->db->get('tbl_zscore');
return $query;
}
View - addZscore.php
<table style="width:50%">
<tr>
<th>Z ID</th>
<th>Z Score</th>
</tr>
<?php echo print_r($get_ztable);?>
<?php
if($get_ztable->num_rows() > 0)
{
foreach ($get_ztable as $row)
{
?>
<tr>
<td><?php echo $row->z_id; ?></td>
<td><?php echo $row->z_score; ?></td>
</tr>
<?php
}
}else{
echo 'Database Error(' . $this->db->_error_number() . ') - ' . $this->db->_error_message();
}
?>
</table>
You have to make changes in your model function that must return a "result" that you can easily use in your view. So just replace
return $query
with
return $query->result()
Or make a change in your foreach loop in views from
foreach($get_ztable as $row)
to
foreach($get_ztable->result() as $row)

not displaying result in codeigniter

i tried to use print_r($list) but the result array().There is no element in the array.I want to show data form my table.there is user table, job table and industry table.Can anybody suggest me where is mistake.
This is my model function get_data_by_query used in controller
public function get_data_by_query($qry)
{
$query = $this->db->query($qry);
return $query->result();
}
This is my controller where i am verifying email, phone number.
public function Dashboard()
{
if ($this->Common_model->Is_User_Logged())
{
$condition='';
$data['email_verified']=self::IsEmailVerified();
$data['phone_verified']=self::IsPhoneVerified();
$userdata=$this->session->userdata('user_logged_in');
$data['user_email']=$userdata['email'];
//print_r($data);
$res=$this->Common_model->get_data_by_query("SELECT * FROM user WHERE user_id={$userdata['id']} limit 1");
//print_r($res);
$data['user_skills'] = $res[0]->user_skills;
//print_r($data);
$arr = array($res[0]->user_skills);
//print_r($arr[0]);
//exit;
$condition.=" and FIND_IN_SET(job_title,'$arr[0]')";
print_r($condition);
$sql_get="SELECT j.*,j.job_industry,i.industry_title
FROM job j
LEFT JOIN industry i on i.industry_id=j.job_industry
WHERE job_status=1 ".$condition." ";
//print_r($sql_get);
$data['list']=$this->Common_model->get_data_by_query($sql_get);
//print_r($data['list']);
$data['user_phone']=$res[0]->user_mobile;
$this->load->view('user/header');
$this->load->view('user/home', $data);
$this->load->view('user/footer');
}
else
{
redirect('user');
}
}
This is my view
<?php
if(!empty($list)>0)
{
foreach ($list as $row)
{
?>
<tr>
<td><?php echo $row->job_role;?></td>
<td><?php echo $row->industry_title;?></td>
<td><?php echo $row->job_vacancy;?></td>
<td><?php echo $row->job_keyword;?></td>
<!--<td><?php echo $row->job_type;?></td>-->
</tr>
<?php
} }
?>
what is the reason that $list is empty array although i have data in my table. I will be thankful for you.
After a lot of effort, i found that there is no problem in above code. The problem is in tje SQL query. I am trying to resolve that query.

Undefined variable,index error fetching data codeigniter

Im fetching data from a table to another table that will be shown in my dashboard page for a user after loggin in.
But there is a problem with the indexes, i got this error:
Here is the line error:
Here is my code:
My view file ("usuario"):
<thead>
<th>id</th>
<th>User</th>
<th>Subject</th>
<th>Grade</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false) {
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['User']."</td>
<td>".$record['name']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My controller file ("login"):
public function home(){
$data['record']=$this->m_login->getDetails();
$this->load->view('usuario',$data);
}
My model file ("m_login"):
public function getDetails()
{
$st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata['id'])
->get()->result_array();
return $st[0];
}
You have the variable $records on view but not on controller
Change
$data['record'] = $this->m_login->getDetails();
To
// add this array() just in case no results found
$data['records'] = array();
$data['records'] = $this->m_login->getDetails();
$this->load->view('usuario', $data);
Another way is on controller
$results = $this->m_login->getDetails();
$data['records'] = array();
if ($results) {
foreach ($results as $result) {
$data['records'][] = array(
'id' => $result['id'],
'User' => $result['User'],
'name' => $result['name'],
'grade' => $result['grade'],
'date' => $result['date']
);
}
}
$this->load->view('usuario',$data);
View
<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>No Results Found</td>
</tr>
<?php } ?>
Wrong array index in your controller.
Change this
$data['record']=$this->m_login->getDetails();
to
$data['records']=$this->m_login->getDetails();
result_array() return returns result with array so you need to it like this -
if (count($record) > 0 && $record != false) {
foreach($record as $rec){
echo "<tr>
<td>".$rec['id']."</td>
<td>".$rec['User']."</td>
<td>".$rec['name']."</td>
<td>".$rec['grade']."</td>
<td>".$rec['date']."</td>
</tr>";
}
}
Please check this from above code and comment if you have any problem

Hide column if column is empty i.e. array element is empty

I have an array such:
$prices = array();
Along with a MySQL query and fetch:
$query = "SELECT $columns FROM Prices WHERE `key` LIKE '$rows' LIKE '$AirportPU' AND rate LIKE '$rate'";
if($results = $db->query($query))
{
if($results->num_rows)
{
while($row = $results->fetch_object())
{
$prices[] = $row;
}
$results->free();
}
I've printed out the table using the following code: (I have removed some table columns)
<?php
if(!count($prices)) {
echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
} else {
?>
<table>
<thead>
<tr>
<th>Location</th>
<th>City</th>
</tr>
</thead>
<tbody>
<?php
foreach ($prices as $p) {
?>
<tr>
<td> <?php echo $p->Location; ?> </td>
<td> £<?php echo $p->City; ?> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } ?>
I can return data from the MySQL query, and print this to the table. However, sometimes not all the columns need be printed as they have no values. I would like to hide these columns.
I have tried checking the array using:
print (isset($prices["City"])) ? "Exists</br>" : "Doesn't Exist</br>" ;
But that always returns "Doesn't Exist"
if (array_key_exists("City",$prices))
{
echo "Element exists!";
}
else
{
echo "Element does not exist!";
}
That also returns false.
You need to check it inside the foreach loop which you are executing.
print (isset($p->City)) ? "Exists</br>" : "Doesn't Exist</br>" ;
Check with
if(empty($price['City'] || $price['City'] == "")){
//Hide column
}

Categories