Codeigniter : How to change result array [duplicate] - php

This question already has an answer here:
looping in php codeigniter views
(1 answer)
Closed 3 years ago.
How to change result array like this
to like this
this is my Controller
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
echo "<pre>";
print_r($arrData);
echo "</pre>";
$this->load->view('welcome_message');
}

controller
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
$data = array();
$data['arrData'] = $arrData;
$this->load->view('welcome_message',$data);
}
view code
First you need to set header then this
<?php foreach($arrData as $data){ ?>
<tr>
<td><?php echo $data['user_firstname'] ?></td>
</tr>
<?php } ?>

controller: First of all pass data to your view from your controller:
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
// Create an Data Array to store all the data
// that you need to pass to your view
$data['user'] = $arrData;
$this->load->view('welcome_message', $data);
}
view: Then in your view file create a table and make it dynamic to use the data that need to be shown:
<?php foreach($user as $key => $value) : ?>
<tr>
<td><?= $value['user_firstname'] ?></td>
<td><?= $value['user_lastname'] ?></td>
...
..
.
</tr>
<?php endforeach; ?>

Do like this in the HTML table
$data['arrData'] = json_decode($responseData,true);
Pass the data in the view $this->load->view('welcome_message', $data);
<?php foreach($arrData as $data): ?>
<tr>
<td><?= $data['user_firstname'] ?></td>
</tr>
<?php endforeach; ?>

Change the controller to pass the $arrData data :
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$data['arrData'] = json_decode($responseData,true);
$this->load->view('welcome_message', $data);
}
and try like this within the welcome_message.php view file :
<table class="table table-hover">
<thead>
<tr>
<th>The Title</th>
</tr>
</thead>
<tbody>
<?php foreach($arrData as $data): ?>
<tr>
<td><?= $data['user_firstname'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Related

How to display data in view of the CodeIgniter?

Unable to display the data in a CodeIgniter view. Need to print the builders_name in a CodeIgniter view. So far I have the following code:
Model :
public function get_builders()
{
$query1 = $this->db->select('name')->from('builders')->where("name LIKE 'a%'")->limit(3)->get();
$query2 = $this->db->select('name')->from('builders')->where("name LIKE 'b%'")->limit(3)->get();
$result1 = $query1->result();
$result2 = $query2->result();
return array($result1, $result2);
}
Controller:
public function index(){
$this->load->database();
$data['h']= $this->Builders_Model->get_builders();
$this->load->view('home', $data);
}
View:
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Don't like this try it
public function get_builders()
{
$query1 = $this->db->select('name')
->from('builders')
->like('name','a')
->or_like('name','b')
->limit(3)->get();
$result = $query1->result();
return $result;
}
views code
<?php foreach ($h as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Hope fully works . Thank You
What you pass to the view in Codeigniter is accessible by its name. Just <php echo $h; ?> and you should be good to go
you can use :
<?php
foreach($h as $row){?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
}?>
You have to use $h variable in foreach for notation :
<?php
<tr>
<td><?php echo $rowDetail->name; ?></td>
</tr>
}
?>
First this is you haven't loaded the Builder_modal in your controller try to do some thing like
$this->load->model('Builder_model');
after $this->load->database();
Next thing in your view you have to echo like
echo $h; or var_dump($h) if you have an array
It's really working
model
public function get_builders()
{
$this->db->select('name');
$this->db->from('builders');
$this->db->like('name','a');
$this->db->or_like('name','b');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
controller
public function index(){
$this->load->model('Builder_model');
$data= $this->Builders_Model->get_builders();
$this->load->view('home', ['data'=>$data]);
}
view
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>

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,

Unable to call fetch data from controller to view in codeigniter

I was unable to show data on view from the database using controller and model in CodeIgniter.
Controller Code:
class Leads extends CI_Controller {
public function show($id) {
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['name'] = $leads['name'];
$data['email'] = $leads['email'];
$data['contact'] = $leads['contact'];
$data['referral'] = $leads['referral'];
$data['project_detail'] = $leads['project_detail'];
$data['note'] = $leads['note'];
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
$this->load->view('layouts/footer', $data);
}
Model code:
class Leads_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
public function get_leads($id) {
if ($id != FALSE) {
$query = $this -> db -> get('leads', array('lead_id' => $id));
return $query -> row_array();
}
else {
return FALSE;
}
}
view code:
<td>
<?php echo $data['name']; ?>
</td>
<td>
<?php echo $data['email']; ?>
</td>
<td>
<?php echo $data['contact']; ?>
</td>
<td>
<?php echo $data['referral']; ?>
</td>
<td>
<?php echo $data['project_detail']; ?>
</td>
<td>
<?php echo $data['note']; ?>
</td>
$leads is an array so there should be foreach loop in the view
In controller :
public function show($id)
{
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['leads'] = $leads;
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
}
Your Model :
public function get_leads($id) {
if ($id != FALSE) {
$this->db->where(array('lead_id' => $id));
$query = $this->db->get('leads');
return $query->row_array();
}
else {
return FALSE;
}
}
Your view should be like this:
<?php if ($leads) {
foreach($leads as $lead) {?>
<?php echo $lead['name']; ?>
<?php echo $lead['email']; ?>
<?php echo $lead['contact']; ?>
<?php echo $lead['referral']; ?>
<?php echo $lead['project_details']; ?>
<?php echo $lead['note']; ?>
}
}?>
if single row data: Use variable without $data in your view
if your model method return $query->row();
Your view :
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contact; ?></td>
<td><?php echo $referral; ?></td>
<td><?php echo $project_detail; ?></td>
<td><?php echo $note; ?></td>
for more : https://www.codeigniter.com/user_guide/database/query_builder.html

CodeIgniter : display two query result with one return

I am trying to display 'Category' database and this part is quite easy. The hard part is I need to count the subcategory from the 'Category' database and displaying them with one table.
Category | Sub Category
------------------------
Cat A | 5
Cat B | 7
here is my Model:
function category() {
$query = $this->db->get('category');
$result = $query->result_array();
foreach($query->row() as $q) {
$this->db->where('subcat_id', $q['cat_id']);
$query2 = $this->db->get('subcat');
if($query2) {
return true;
} else {
return false;
}
here is my Controller:
function dispaly_category() {
$data['category'] = $this->mymodel->category();
$this->load->view('view', $data);
}
here is my View:
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo (count subcat for the above category); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i just post an answer with the assumption you've one Table only (you don't need a separate table for subcategories, but in case you really need your second table you should be able to derive this based on my answer)
Your Model
function category()
{
$query = $this->db
->select("c.*, count(cc.id) AS countSubCategories")
->from("category c")
->join("category cc", "cc.parent_id = c.cat_id","left")
->group_by("c.id")
->get();
return $query->result();
}
Your Controller
function display_category()
{
$arrViewData = array(
'category' => $this->mymodel->category()
);
$this->load->view('view', $arrViewData);
}
your view
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo $c->countSubCategories; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

How Can I Pass Result of Foreach from Controller to View CodeIgniter

just like my title, how can I pass foreach result from controller to view?
So far I did this:
--Controller.php
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data['kodeSubKategori']=$kodeSubKategori;
$data['namaSubKategori']=$namaSubKategori;
$data['definisiSubKategori']=$definisiSubKategori;
$data['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data['aliasSubKategori']=$aliasSubKategori;
}
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
Then, in my view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<tr>
<?php
foreach((array)$kodeSubKategori as $itemKodeSubKategori)
echo '<td>'.$itemKodeSubKategori.'</td>';
?>
<?php
foreach((array)$namaSubKategori as $itemNamaSubKategori)
echo '<td>'.$itemNamaSubKategori.'</td>';
?>
<?php
foreach((array)$definisiSubKategori as $itemDefinisiSubKategori)
echo '<td>'.$itemDefinisiSubKategori.'</td>';
?>
<?php
foreach((array)$unsurAbstrakSubKategori as $itemUnsurAbstrakSubKategori)
echo '<td>'.$itemUnsurAbstrakSubKategori.'</td>';
?>
<?php
foreach((array)$aliasSubKategori as $itemAliasSubKategori)
echo '<td>'.$itemAliasSubKategori.'</td>';
?>
</tr>
</table>
It successfully show ONLY THE LAST ONE data. I want to get all data from the result from foreach. How can I do that?
I suggest you to pass all $get_json_subkategori['SubKategoris'] array in data array to your view:
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemSubKategori'] = $get_json_subkategori['SubKategoris'];
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
And in your view:
<h3>Sub Kategori</h3>
<table class="table table-responsive table-inherit">
<tr>
<th>Kode Sub Kategori</th>
<th>Nama Kategori</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
</tr>
<?php foreach($itemSubKategori as $rowSubKategori): ?>
<tr>
<td><?php echo $rowSubKategori['kodeSubKategori'] ?></td>
<td><?php echo $rowSubKategori['namaSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['definisiSubKategori'] ?></td>
<td><?php echo $rowSubKategori['unsurAbstrakSubKategori'] ?></td>
<td><?php echo $rowSubKategori['aliasSubKategori'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Because you data array override each time that's y you get only last value every time.
You need to create your array with auto incremented value
$i=0;
foreach($get_json_subkategori['SubKategoris'] as $itemSubKategori){
$kodeSubKategori = $itemSubKategori['KodeSubKategori'];
$namaSubKategori = $itemSubKategori['Nama'];
$definisiSubKategori = $itemSubKategori['Definisi'];
$unsurAbstrakSubKategori = $itemSubKategori['UnsurAbstrak'];
$aliasSubKategori = $itemSubKategori['Alias'];
$data[$i]['kodeSubKategori']=$kodeSubKategori;
$data[$i]['namaSubKategori']=$namaSubKategori;
$data[$i]['definisiSubKategori']=$definisiSubKategori;
$data[$i]['unsurAbstrakSubKategori']=($unsurAbstrakSubKategori) ? 'true' : 'false';
$data[$i]['aliasSubKategori']=$aliasSubKategori;
$i++;
}
Pass data in one array. and you can catch all in view at one foreach llop
Use this
In Controller
$get_url_subkategori = $this->ws_url->GetUrl('SubKategoriRetrieve?KodeKategori='.$param);
$get_json_subkategori = json_decode(file_get_contents($get_url_subkategori), true);
$data['itemKodeSubKategori'] = $get_json_subkategori;
$this->load->view('include/header');
$this->load->view('kategori_detail', $data);
$this->load->view('include/footer');
In View
<tr>
<?php
foreach($kodeSubKategori as $itemKodeSubKategori)
{
echo '<td>'.$itemKodeSubKategori['KodeSubKategori'].'</td>';
echo '<td>'.$itemKodeSubKategori['Nama'].'</td>';
echo '<td>'.$itemKodeSubKategori['Definisi'].'</td>';
echo '<td>'.$itemKodeSubKategori['UnsurAbstrak'].'</td>';
echo '<td>'.$itemKodeSubKategori['Alias'].'</td>';
}
?>
</tr>

Categories