Codeigniter View returns only last row query - codeigniter

Im new to CI/PHP/development. Im stuck on this problem. It seems to be really basic but i just cant get it right!
The problem is that my view will only display the last row of the query results
Model:
$this->db->select('Caption,Description');
$query = $this->db->get_where('Events', array ('User_iduser' => $user_id));
$results = $query->result();
Controller:
$this->load->model('get_events_model');
$data['results'] = $this->get_events_model->get_events();
$this->load->view('main/members_area_form',$data);
View:
<?php foreach($results as $row); {
echo $row->Caption;
echo $row->Description;
}
?>
To troubleshoot I have put:
var_dump($results);
...in the view and the result is:
array(3) { [0]=> object(stdClass)#18 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(4) "Blue" } [1]=> object(stdClass)#19 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(3) "Red" } [2]=> object(stdClass)#20 (2) { ["Caption"]=> string(5) "Color" ["Description"]=> string(5) "Black" } }
So there is nothing wrong with the db query but still the foreach loop is only displaying the last row from the query. What am I doing wrong?

remove ";" from
<?php foreach($results as $row); {
the line will be
<?php foreach($results as $row) {
in your case foreach was only looping but your echos was not in foreach.
after executing foreach
it was coming to your echo part and echoing last row as foreach already conpleted its loop.

look at your view code:
you should remove your unnecessary semicolon ";"
after the foreach
<?php
foreach($results as $row) {
echo $row->Caption;
echo $row->Description;
}
?>

Related

Remove JSON object value Laravel from MySql database

I want to remove JSON object value from mysql database via eloquent. I have tried with this code it works but I have to pass array key "$.language[1]".
Here is JSON object {"name":"The Lord of the Rings:The Fellowship of the Ring","language":["Hindi","English","Spanish"]} stored in database.
Here I want to remove English language from all the records.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use DB;
class BookController extends Controller
{
public function store(){
$book = new Book;
$book->attributes = $jsonAttr;
$book->save();
echo "Book saved";
die;
}
public function updateLanguage()
{
$result = Book::where('attributes->language','LIKE','%English%')->update(['attributes' => DB::raw('JSON_REMOVE(attributes, "$.language[1]")')]);
//$result = Book::where('attributes->language','LIKE','%H%')->get();
echo "<pre>";
print_r($result);
die;
}
}
Any help would be appreciated.
Where condition fetch all the match record from Database. You need to loop this query to remove particular book language. Try this code...
public function updateLanguage()
{
//Get all matched records from database
$result = Book::where('attributes->language','LIKE','%')->get();
//Loop items to get unique id
foreach($result as $key => $val){
$id = $val['id'];
$attr = json_decode($val->attributes, true);
$data = $attr['language'];
foreach($data as $itemkey => $lang){
if($lang == "English"){
//Pass unique id to remove language from book record
$result = Book::where('id',$id)->update(['attributes' => DB::raw('JSON_REMOVE(attributes, "$.language['.$itemkey.']")')]);
}
}
}
$result = Book::where('attributes->language','LIKE','%')->get();
foreach ($result as $key => $value) {
print_r($value['attributes']);
echo "<br>";
}
die;
}
You need json_decode for transform json to array : link
You remove "English" in array with unset : link
After that you can json_encode for transform array to json.

Foreach row array CodeIgniter

I have created a model which joins 2 tables
function list_get($id){
$this->load->database();
$query = $this->db-> select('*')
->from('lists')
->join('list_items', 'list_items.items_list_id = lists.list_id')
->where('items_list_id', $id);
return $query->get()->row_array();
}
In my controller I created the view_list function
public function view_list($id = !FALSE){
$this->load->model('model_lists');
$data["query"] = $this->model_lists->list_get($id);
$this->load->model('model_lists');
if($data["query"]){
$this->load->view('lists/view_list',$data);
} else {
redirect('lists');
}
}
Now my view returns only the first result from the database
<?php echo $query['item_url'];?> How can I create a foreach to show all the results?
change this line return $query->get()->row_array();
to return $query->get()->result_array();
in view
foreach($query as $querys){
echo $querys['item_url'];
echo '</br>';
}
Change this return $query->get()->row_array(); to this
return $query->result_array();
and in view
foreach($query as $row)
{
echo $row['column_name'];
}

get value from json php codeigniter

I have a json object like like this
[
[
{
"class":"com.ehealth.data.Sample_Data",
"collectedBy":"2013-07-21",
"collectedDate":"Kamal",
"orderID":2,
"sampleID":2.897033553E9
},
{
"class":"com.ehealth.data.Order_Data",
"doctorUsername":"Kamal",
"dueDate":"2014-01-02",
"orderDate":"2013-12-12",
"orderID":2,
"patientID":"P0001",
"prority":1,
"status":"complete",
"testType":"Fasting Blood Sugar"
}
],
[
{
"class":"com.ehealth.data.Sample_Data",
"collectedBy":"2013-07-22",
"collectedDate":"Kamal",
"orderID":3,
"sampleID":5.978956192E9
},
{
"class":"com.ehealth.data.Order_Data",
"doctorUsername":"Kamal",
"dueDate":"2014-01-02",
"orderDate":"2013-12-12",
"orderID":3,
"patientID":"P0001",
"prority":2,
"status":"complete",
"testType":"Fasting Blood Sugar"
}
]
]
and i decode it to php array
$data['query'] = json_decode($curl_response);
i try to access valuse like this
foreach ($query as $row) {
echo $row->sampleID;
}
but when i am going to access the the values inside it i can not access i get a error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
can anybody help me. i dont know what to do
You can get it:
foreach ($query as $row) {
echo $row[0]->sampleID;
}
<?php
$str = '[[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-21","collectedDate":"Kamal","orderID":2,"sampleID":2.897033553E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":2,"patientID":"P0001","prority":1,"status":"complete","testType":"Fasting Blood Sugar"}],[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-22","collectedDate":"Kamal","orderID":3,"sampleID":5.978956192E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":3,"patientID":"P0001","prority":2,"status":"complete","testType":"Fasting Blood Sugar"}]]';
$json = json_decode($str,true);
foreach ($json as $datas){
foreach ($datas as $data){
echo (isset($data['sampleID']) ? $data['sampleID'] :'')."<br/>";
}
}
echo '<pre>';
print_r ($json);
echo '</pre>';
// exit;
?>

I have a question about using form_dropdown()

I have a question about using form_dropdown().
table:category
cat_id
cat_name
Controller:
function index()
{
$this->load->model('category_model');
$data['category'] = $this->category_model->cat_getallcat();
$this->load->view('category_input',$data);
}
Model:category_model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category')->result();
}
View:
<?php
$this->load->helper('form');
echo form_open('send');
$list[''] = 'Please select a Category';
foreach($query as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
echo form_dropdown('category', $list);
echo form_close();
?>
error obtained:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/category_input.php
Line Number: 28
the ->result() will return the first row only, so this function is what you want to loop over.
Model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category');
}
View
foreach($query->result() as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
EDIT:
Also, you are sending the result as $data['category'] then trying to access it as $query. So really the foreach would be foreach($category->result() as $row) in this example
You are asking foreach to loop over $query, but I you haven't set $query as a variable anywhere that I can see.
Since you set $data['category'] to hold your query result() in your controller, you need to loop over $category in the view:
foreach($category as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}

Array not resetting

I have a loop in my php which takes the database values of two tables and displays them in a CodeIgniter dropdown - however the array doesn't reset itself after the news_types loop, it uses the category data instead...can anyone help me?
Thanks
//inside a loop
if(isset($news_types)) {
foreach($news_types as $type) {
$options[$type['id']] = ucwords($type['type']);
}
}
if(isset($categories)) {
foreach($categories as $category) {
$options[$category['id']] = ucwords($category['category']);
}
}
echo '<p>';
echo format_label($field);
echo form_dropdown($field, $options, check_value($field, $submitted, $record->$field));
echo '</p>';
If your news type id's match up with category ids they will be overwritten... Try this to verify
if(isset($news_types)) {
foreach($news_types as $type) {
$options["news_".$type['id']] = ucwords($type['type']);
}
}
if(isset($categories)) {
foreach($categories as $category) {
$options["cat_".$category['id']] = ucwords($category['category']);
}
}
prefix your ids to make sure they do not clash.

Resources