not displaying result in codeigniter - php

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.

Related

Show table data based on ID in codeigniter

I have a table that store the data of ID, ID Login, Name, etc. But i just want to show the data based on ID Login
Here My Controller :
function index(){
$data['hasil'] = $this->M_user_lapor->index_model();
$this->load->view('v_user_lapor/index', $data);
}
My Model :
function index_model(){
$baca = $this->db->query('select * from user_lapor');
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}
View :
<tbody>
<?php
if ($hasil){
$no = 1;
$array = json_decode($hasil, true);
foreach($array as $data) {
?>
<tr>
<td class="text-center"><?php echo $no++;?></td>
<td><?php echo $data['nm_unit'];?></td>
<td><?php echo $data['pic_1'];?></td>
<td><?php echo $data['pic_2'];?></td>
<td><?php echo $data['ip_wan'];?></td>
<td><?php echo $data['ip_lan'];?></td>
<td><?php echo $data['prov'];?></td>
<td><?php echo $data['icn_sid'];?></td>
<td><?php echo $data['tlkm_sid'];?></td>
</tr>
<?php
}
}
?>
</tbody>
As you can see, there is id_login inside my model, and i want to show the table data based on it, hopefully somebody can help me because i'm just using the codeigniter, thnaks
I solved this by myself, lol. I just pass the id_login value from session, so i add this to my controller :
function index(){
$id_login = $this->session->id_login;
$data['hasil'] = $this->M_user_lapor->index_model($id_login);
$this->load->view('v_user_lapor/index', $data);
}
And call it to my model :
function index_model($id_login){
$baca = $this->db->query('select * from user_lapor where id_login='.$id_login);
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}

CodeIgniter using if to show some table

So i am trying to make a website for collecting data from user and classify the data as 'suspicious' or 'not suspicious'. After i collect and classify the data, i want to show and send to server ONLY 'suspicious' data.
My table has one column called 'note', where the value of the 'note' is 'suspicious' or 'not suspicious'
is it possible to show the part of table that only have 'not suspicious' value in 'note' ? if possible, can you guys give me any idea how ?
Here is my code for showing the data:
Controller:
public function success() {
$dataz['resultss'] = $this->welcome_model->dataz();
$this->load->view('success',$dataz);
}
Model
public function dataz() {
$query = $this->db->get('info');
$results = $query->result();
return $resultss;
}
View
<?php
foreach($resultss as $row) {
echo '<tr>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->note.'</td>';
?>
<td>
</td>
<?php
echo '</tr>';
}
?>
Any help would be appreciated!
You can achieve your objective in many ways.
For 'not suspicious' records, use this,
$query = $this->db->get_where('info', array('note' => 'not suspicious'));
For 'suspicious' records, use this,
$query = $this->db->get_where('info', array('note' => 'suspicious'));
The method #Naga suggested is better but if you want to stick to your way and do it in view;
<?php
foreach($resultss as $row) {
if($row->note == 'not suspicious') {
echo '<tr>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->note.'</td>';
echo '<td></td>'; // why this extra td?
echo '</tr>';
}
}
?>

The table displays data in one column into the data row in codeigniter?

someone please help me, I use the CodeIgniter with mvp procedure. I have a database like this:
is it possible if I want to display data in a table view like this?
I can only show you on a date, when retrieving data at another date id_status everything is not working.
controller when one date only.
public function status()
{
$tanggal1 = '2017-03-28';
$data['status'] = $this->M_Reports->status($tanggal1);
$this->load->view('homepage/template');
$this->load->view('report/status', $data);
}
model :
public function status($tanggal1) {
$sql = "SELECT operasional.id_ops, operasional.id_status, operasional.tanggal, peralatan.nm_peralatan FROM operasional INNER JOIN peralatan ON operasional.id_peralatan = peralatan.id_peralatan WHERE tanggal='$tanggal1' ORDER BY id_ops";
return $this->db->query($sql)->result();
}
And view :
<?php
$start = 0;
foreach($status as $data) {
?>
<tr>
<td><?php echo ++$start ?> </td>
<td><?php echo $data->nm_peralatan ?></td>
<td><?php echo $data->id_status ?></td>
<td><?php echo $data->tanggal ?></td>
</tr>
<?php
}
?>
You model function should be like this
public function status($top_of_date)
{
// This will get all dates
$dates=$this->db->distinct()->select('tanggal')->from('operasional')->get()->result_array();
// This will get all patients / peralatan
$patients=$this->db->distinct()->select('id_peralatan')->from('operasional')->get()->result_array();
// Loop through peralatan
for($i=0;$i<count($patients);$i++)
{
$st=$this->select('id_peralatan')->from('operasional')->WHERE('id_peralatan',$patients[$i])->get()->result_array();
// Start Creating an array for this index
$data[$i]=array(
'id_peralatan'=>$st[0]
);
// loop through dates
for($j=0;$j<count($dates);$j++)
{
//if date is greater than top of date
if($dates[$i]>$top_of_date)
{
$st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array();
// Create index and save the status
$data[$i]['top_of_date']=$st[0]['id_status'];
}
elseif($dates[$i]<$top_of_date)
{
$st=$this->db->select('id_status')->from('operasional')->WHERE('tanggal',$dates[$i])->get()->result_array();
// Create index and save the status
$data[$i][$dates[$i]]=$st[0]['id_status'];
}
}
}
return $data;
}
Now from your controller you only have to send top of date. like;
public function status()
{
$tanggal_top = '2017-03-28';
$data['status'] = $this->M_Reports->status($tanggal_top);
$this->load->view('homepage/template');
$this->load->view('report/status', $data);
}

View content with parameters in CodeIgniter

Hello all I have a problem that my information is not being send to my view file.
What mistake do I have?
My Model:
public function viewbds($duanid, $bdsid)
{
$q = $this->db->query("SELECT bds.tenduan, bds.id, bds.tieude FROM bds WHERE bds.tenduan=$duanid AND bds.id=$bdsid);
if($q->num_rows() > 0)
return $q->result();
return false;
}
My Controller:
public function chothue($duanid, $bdsid)
{
$this->load->model('admin_model');
$date[chothue] = $this->admin_model->viewbds('$duanid', '$bdsid');
$this->load->view('admin/view', $data);
}
My View: admin/chothue/1/1
<?php foreach($chothue as $d) { ?>
<tr>
<td><?php echo $d->id; ?></td>
<td><?php echo $d->tenduan; ?></td>
<td><?php echo $d->tieude; ?></td>
<?php } ?>
$this->db->query("SELECT bds.tenduan, bds.id, bds.tieude FROM bds WHERE bds.tenduan=$duanid AND bds.id=$bdsid")->get();
Don't forget to use ->get()
AND use active record
$this->db->select("tenduan, id, tieude")->from("bds")->where("tenduan", $duanid)->where("id", $bdsid)->get();

Yii multi model with HAS_MANY Tabular form

I have been trying to find a solution to something that I believe is rather simple? What I would like to do is create a tabular form to collect some records for a survey.
Table: survey_record
id
meter_id
status_id
create_time
create_user
Table: survey_inspection
id
survey_record_id (FK)
bay_id
bay_usage_id
So table survey_inspection HAS_MANY inspections from survey_record.
Assuming an inspector is doing a survey for meter_id = 1001, the user identifies the status_id of the meter then, they have to fill in 4 inspection entries, as 1001 has 4 bays.
To generate the 4 bays I simply created a for loop. Which works fine.
View: _form.php
...
$meter=Meter::model()->findByPk($model->meter_id);
$bays = count($meter->bays); //retrieves the number of inspections required
<?php echo $form->dropDownListRow($model,'status_id',$model->getStatusId(),array('prompt'=>'Select Status ...')); ?>
...
<?php for ($i = 1; $i <= $bays; $i++): ?>
<?php echo $form->textFieldRow($inspection, "[$i]bay_id", array('readonly'=>true, 'value'=>"$i", 'class'=>'span1')); ?>
<?php echo $form->dropDownListRow($inspection, "[$i]bay_usage_id", $inspection->getUsageId(), array('prompt'=>'Bay Status ...') ); ?>
<?php endfor; ?>
...
However when I submit the form I am only receiving two (not four) results and I can't seem to display the validated fields correctly. So here's the famous controller file:SurveyRecordController.php
public function actionCreate($survey_id, $meter_id)
{
$model=new SurveyRecord;
$inspection = new SurveyInspection;
$model->survey_id = (int)$survey_id;
$model->meter_id = (int)$meter_id;
if(isset($_POST['SurveyRecord'], $_POST['SurveyInspection']))
{
$model->attributes=$_POST['SurveyRecord'];
$valid= $model->validate();
$i=1;
foreach($_POST['SurveyInspection'][$i] as $inspection)
{
$inspection = new SurveyInspection;
$inspection->bay_id =$_POST['SurveyInspection'][$i]['bay_id'];
$inspection->bay_usage_id =$_POST['SurveyInspection'][$i]['bay_usage_id'];
$valid= $inspection->validate() && $valid;
echo '<br/><br/><br/><pre>';
print_r($_POST['SurveyInspection'][$i]);
echo '</pre>';
if($valid)
{
if($model->save(false))
{
$inspection->survey_record_id = $model->id;
$inspection->save(false);
$this->redirect(array('/meter'));
}
}
$i++;
//$inspection->attributes=$_POST['SurveyInspection'][$i];
}
}
$this->render('create',array(
'model'=>$model,
'inspection'=>$inspection,
));
}
I have a funny feeling that I may not be doing the foreach loop correctly as when I view the array $_POST['SurveyInspection'][$i] I am only returned with two entries when there should be four.
Some information that may be helpful:
PHP version: 5.4.14
Yii version: 1.1.13
PostgreSQL version: 9.1.9
Thank you kindly :)
To my knowledge i think for the validation u have bypassed the default validation by passing
false to save method (ex : $model->save(false)) and u are also using yii's validate function . I think u try removing the false parameter and just use save() for the validation issue
The foreach loop is wrong. You can make it proper and a bit cleaner:
foreach($_POST['SurveyInspection'] as $i => $value)
{
$inspection = new SurveyInspection;
$inspection->bay_id = $value['bay_id'];
$inspection->bay_usage_id = $value['bay_usage_id'];
...
$i++ is not needed, as foreach will loop every element.
I've taken a completely different approach, valuable suggestions are always welcome :)
Hope this will be helpful for some of you.
Controller:
public function actionCreate($survey_id, $meter_id)
{
$bays = $this->getBayCount($meter_id);
for ($i=1;$i<=$bays;$i++)
{
$model=new SurveyRecord;
$model->survey_id = (int)$survey_id;
$model->meter_id = (int)$meter_id;
${'inspection_'.$i} = new SurveyInspection($i);
if(isset($_POST['SurveyRecord'], $_POST["SurveyInspection"][$i]))
{
$model->attributes = $_POST['SurveyRecord'];
${'inspection_'.$i}->attributes = $_POST["SurveyInspection"][$i];
echo '<br/><br/><br/><pre>';
print_r(${'inspection_'.$i}->attributes);
echo '</pre>';
}
}
for ($i=1;$i<=$bays;$i++)
{
${'inspection_'.$i} = new SurveyInspection($i);
$inspection['inspection_'.$i] = ${'inspection_'.$i};
}
$this->render('create',array(
'inspection'=>$inspection,
'model'=>$model
));
}
View:
...
<div class="control">
<?php echo $form->dropDownListRow($model,'status_id',$model->getStatusId(),array('prompt'=>'Select Status ...')); ?>
<?php
$meter=Meter::model()->findByPk($model->meter_id);
$bays = count($meter->bays);
?>
<?php for ($i=1; $i<=$bays; $i++): ?>
<table>
<tr>
<td><?php echo $form->textFieldRow($inspection["inspection_$i"], "[$i]bay_id",
array('readonly'=>true, 'value'=>"$i", 'class'=>'span1')); ?></td>
<td><?php echo $form->dropDownListRow($inspection["inspection_$i"], "[$i]bay_usage_id", $inspection["inspection_$i"]->getUsageId(),
array('prompt'=>'Bay Status ...') ); ?></td>
</tr>
</table>
<?php endfor; ?>
</div>
...
Credit: http://yiiblog.info/blog/index.php/post/108/Rename+ACtiveRecord+$_POST+array's+Name+to+update+multimodels
Note: There's still some work to be done on the view/controller (doesn't seem to be able to select on $_POST for my dropdowns on second model), will post once I have fixed it.
Thanks for your help #PeterM and #Ninad for your input.

Categories