function mostViewedTracks() {
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return array();
}
when i run this model it gives error that invalid argument supplied for foreach()
this is my view code:
<?php if( !empty($most_viewed) ) { ?>
<?php foreach($most_viewed as $row): ?>
<a href="<?php echo base_url('index.php/home/track');?>/<?php echo $row->id;?>" class="list-group-item">
<img class="track-list-img" src="<?php echo base_url('assets/img/music');?>/<?php echo $row->id;?>.jpg">
<div class="list-track-infoo">
<h4 class="list-group-item-heading"><?php echo $row->title;?><span class="badge"><?php echo $row->views;?><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></span> </h4>
<p class="list-group-item-text"><?php echo $row->singer;?></p>
</div>
</a>
<?php endforeach; ?>
<?php} ?>
Updated
Controller
function track() {
$id = $this->uri->segment(3);
$data1['track'] = $this->view_models->track($id);
$data1['most_viewed'] = $this->view_models->mostViewedTracks();
$this->load->view('includes/header');
$this->load->view('track', $data1);
$this->load->view('includes/footer');
}
I have tried my best to solve this problem but all in vain
You need to change two mistakes,
You should return the `$result` array in your model like,
$results = $query->result();
return $results;
And in foreach change $row1 to $row which produces the error. see,
<?php foreach($most_viewed as $row): ?>
Instead of return array(); you have to return result set from models file
if($query->num_rows() > 0)
{
return $query->result();// return result set
}else{
return FALSE;//
}
IN controller you have to call your models function as
$data1['most_viewed'] = $this->view_models->getFeaturedTracks();// change mostViewedTracks to getFeaturedTracks
In view use it as
<?php foreach($most_viewed as $row): ?>// remove $row1 to $row
Function should like this....
function getFeaturedTracks() {
$results = array();
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
Related
I have a problem,im trying to convert php standard to CodeIgniter, But I dont know how to convert ths code, please help, and thanks a lot.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("ardefa");
$borneo=mysql_query("select* from borneo");
while($row=mysql_fetch_array($borneo))
{
?>
<a href="#"><li><img src="
<?php
$page = isset($_GET['page']) ? ($_GET['page']):"";
if ($page =='borneo')
{
echo $row["img"];
}
?>">
</li></a>
<?php
}
?>
Hope this will help you :
You don’t need to use db_select if you have single database, if multiple database you only need to use a different database on the same connection. You can switch to a different database when you need to using this $this->db->db_select('ardefa');
You can do like this :
//$this->db->db_select('ardefa');
$this->db->select('*');
$this->db->from('borneo');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*print here to see the result
print_r($result);
*/
}
Use $result like this :
foreach($result as $row)
{
echo $row;
}
Or can also do it like this :
//$this->db->db_select('ardefa');
$query = $this->db->get('borneo');
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*for single array
$row = $query->row_array();
*/
}
For more : https://www.codeigniter.com/user_guide/database/
Try this hope it will help you
MODEL
public function your_function(){
return $this->db->get('borneo')->reslut_array();
}
CONTROLLER
<?php
$this->load->model('model-name');
$data = $this->model-name->model_function();
foreach($data as $row){
if(isset($_GET['page']) && $_GET['page'] == "borneo"){ ?>
<li><img src="<?php echo $row['img']?>" /></li>
<?php } } ?>
I have two tables named 'addexpense' and 'addcategory' .
I have successfully joined each of the tables but on my view page the data are not viewing and an error message is passed on the screen. Please help.
This is my model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result();
}
This is my Controller
public function join(){
$query = $this->base_model->getExpenses();
//$data['result'] = null;
if($query)
{
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
This is my view code
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php }?>
set controller function this way
public function join(){
$data['query'] = $this->base_model->getExpenses();
$this->load->view('listexpense', $data);
}
Please can you check your data is going on view file or not by printing it at start of your view
<?php echo '<pre>';
print_r($query);
?>
It seems like you have not initialized model in controller.
In your controller you need to load model first before using its properties. Please check below for your controller method.
public function join() {
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
$data = array();
if ($query) {
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
Let me know still it not works.
Model change result() to result_array()
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
return $query->result_array();
}
Controller try code below
public function join()
{
$results = $this->base_model->getExpenses();
$data['expenses'] = array();
foreach ($results as $result) {
$data['expenses'][] = array(
'exp_date' => $result['exp_date'],
'exp_amount' => $result['exp_amount'],
'exp_note' => $result['exp_note'],
'exp_created' => $result['exp_created'],
'category_name' => $result['category_name']
);
}
$this->load->view('listexpense', $data);
}
View make sure is the correct view
<table>
<thead>
</thead>
<tbody>
<?php foreach($expenses as $expense){?>
<tr>
<td><?php echo $expense['exp_date'];?></td>
<td><?php echo $expense['exp_amount'];?></td>
<td><?php echo $expense['exp_note'];?></td>
<td><?php echo $expense['exp_created'];?></td>
<td><?php echo $expense['category_name'];?></td>
</tr>
<?php }?>
</tbody>
</table>
Here is the code please check and update
Model
public function getExpenses(){
$this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
$this->db->from('addexpense');
$this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
$query = $this->db->get();
$query_rows = $query->num_rows();
if($query_rows > 0){
return $query->result();
} else{
return 0;
}
}
For Controller
public function join(){
$this->load->model('base_model');
$query = $this->base_model->getExpenses();
//print_r($query);die();
$data['query'] = '';
if($query != 0){
$data['query'] = $query;
}
$this->load->view('listexpense', $data);
}
For View
<tr>
<td><strong>Date</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Note</strong></td>
<td><strong>Created</strong></td>
<td><strong>Category Name</strong></td>
</tr>
<?php
if($query != ''){
foreach($query as $expenses){?>
<tr>
<td><?=$expenses->exp_date;?></td>
<td><?=$expenses->exp_amount;?></td>
<td><?=$expenses->exp_note;?></td>
<td><?=$expenses->exp_created;?></td>
<td><?=$expenses->category_name;?></td>
</tr>
<?php } }?>
remove the comment "//print_r($query);die();" and check that whether the model is sending the data or not and please update..
So, I have been trying to get this delete function to work now for a while.
At the bottom of the foreach I have a delete function. The funtion itself does work, however it always selects the post id of 1.
View
<div><?php foreach($posts as $post) : ?>
<hr>
<h3><?php echo $post['title']; ?></h3>
<div class="row">
<div class="col-md-3">
<img class="post-thumb" src="<?php echo site_url(); ?>posts/image/<?php echo $post['post_image']; ?>">
</div>
<div class="col-md-9">
<small class="post-date">Posted on: <?php echo $post['created_at']; ?> in <strong><?php echo $post['name']; ?></strong></small><br>
<?php echo word_limiter($post['body'], 60); ?>
<br><br>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p>
</div>
</div>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
</form>
Controller
public function posts($offset = 0){
// Pagination Config
$config['base_url'] = base_url() . 'admins/posts/';
$config['total_rows'] = $this->db->count_all('posts');
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['attributes'] = array('class' => 'pagination-link');
// Init Pagination
$this->pagination->initialize($config);
$data['title'] = 'Latest Posts';
$data['posts'] = $this->post_model->get_posts(FALSE, $config['per_page'], $offset);
$this->load->view('templates/header');
$this->load->view('admins/posts', $data);
$this->load->view('templates/footer');
}
Model
public function delete_post($id){
$image_file_name = $this->db->select('post_image')->get_where('posts', array('id' => $id))->row()->post_image;
$cwd = getcwd(); // save the current working directory
$image_file_path = $cwd."\\assets\\images\\posts\\";
chdir($image_file_path);
unlink($image_file_name);
chdir($cwd); // Restore the previous working directory
$this->db->where('id', $id);
$this->db->delete('posts');
return true;
}
EDIT:
get_posts in model
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
The function does run and I get a confirmation message, so the only thing I am really confused about is the Id.
The whole thing is written using the CodeIgniter Framework.
Check your get_post model properly. From what you have, it looks like your query will get it's id from the category table.
Try this instead
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->select('posts.id AS id, posts.slug, posts.body, posts.created_at');
$this->db->from('posts, categories');
$this->db->where('categories.id = posts.category_id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->result_array();
}
Updating your join to LEFT JOIN or simple use WHERE
you are returning only 1 line with row_array() function so you should replace for result_array():
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}else{
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->result_array();
}
}
Thanks to everyone, your answers helped me a lot and I learned a few new things about PHP. I tried implementing your Ideas and they solved the area of the problem I was asking about. In the end I decided to use a simple mysqli query to get the data I needed from the database. mysqli_query($con, "SELECT id,category_id,user_id,title,body,created_at FROM posts Order By id DESC");
May be i am wrong but i think its a foreach syntax issue,
because if you getting same id in all the
rows,
you need to write like,
<?php foreach ($posts as $post) : ?>
instead of,
<?php foreach ($posts as $post) { ?>
//your code goes here
<?php } ?>
All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>
So im running a query and trying to return a result set back into my view however when I do <?php foreach ($form_records as $form_record) : ?><?php echo $form_record['fname']; ?> <?php endforeach; ?> I get Undefined variable: form_records. What am i doing wrong when I run the query (Select fname, lname from form_manager where username = "tester") I get a row returned successfully so the query is working.
Model
function retrieve_form_record($username) {
$this->db->select('fname,lname');
$this->db->from('form_manager');
$this->db->where('username', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$form_records= $query->result_array();
return $form_records;
}
else {
return false;
}
}
}
Controller
function load_saved_forms() {
$username = $this->tank_auth->get_username();
$form_records = $this->Form_model->retrieve_form_record($username);
$this->load->view('welcome',$form_records);
}
The problem is how you pass parameter to view, change this line:
$this->load->view('welcome',$form_records);
to :
$data['form_records'] = $form_records; //<<--pass data to view right way.
$this->load->view('welcome',$data);
and the then you can perform this inside view:
<?php foreach ($form_records as $form_record) : ?>
<?php echo $form_record['fname']; ?>
<?php endforeach; ?>
Note that $form_records in view is an index of array $datayou passed at $this->load->view('welcome',$data);
The $form_records is probably an array and looking at the codeigniter's doc it will turn those into array elements when it is passed to the view.
See this link: http://ellislab.com/codeigniter/user-guide/general/views.html
But it is saying if you did this in your controller:
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$this->load->view('blogview', $data);
You can use that "todo_list" like this in your view:
<?php foreach ($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>