I am setting up a blog from scratch and everything works great. Now I'm working on a "private message" feature. On my messages function in my controller profile.php, it shows all the messages that the user received. This seems to be working perfectly fine.
My view_message function shows the conversation between the user that is currently logged in and the user that he's talking to. I put the user's (who the logged in user is talking to) user ID in the uri segment.
So, $user_from = $this->uri->segment(3)
Here's my code first:
my profile.php controller (view_message function)
public function view_message() {
$data['page_title'] = 'Read Message';
$data['messages'] = $this->model_message->message();
$this->load->view('includes/header', $data);
if ( $data['messages']) {
$this->load->view('profile/view_message', $data);
} else {
$this->load->view('404/main');
}
}
My model_message.php Model:
public function message(){
$uid_to = $this->session->userdata('uid');
$uid_from = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('messages');
$this->db->where('uid_to', $uid_to);
$this->db->where('uid_from', $uid_from);
$this->db->where('receive_delete', 0);
$this->db->join('users', 'users.uid = messages.uid_from');
$this->db->order_by("mid", "desc");
$query = $this->db->get();
$result = $query->result();
return $result;
}
My view_message.php view (I have removed most of the code and only left the basic code to show what I have basically):
<?php foreach ($messages as $message) :
$uid = $this->session->userdata('uid');
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<a href="javascript:;" class="timeline-body-title font-red-madison">
<?php if ($uid != $message->uid_to) {
echo 'You';
} else {
echo $message->first_name;
} ?>
</a>
<span class="timeline-body-time font-grey-cascade">
<?php echo date("g:i A - F j, Y ", strtotime($message->sent_date)) ?>
</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-red-cascade">
<?php echo $message->body ?>
</span>
</div>
I'm only able to see the "recieved" messages, not sent. I am not able to figure that out yet. Any help is appreciated. Thank you very much.
Related
In database table there is no rows means getting error Undefined variable: results using codeigniter,in case there is no row or empty table is there means i want to display view page
controller
$data['breview'] = $this->Profile_model->review();
$this->load->view('supplierreview', $data);
Model
public function review() {
$this->db->select('*');
$this->db->from('reviews');
$this->db->join('supplier_otherdetails', 'supplier_otherdetails.supplierid_fk = reviews.supplier_id');
$this->db->join('customer_registration', 'reviews.customer_id=customer_registration.id');
$this->db->join('sub3_category', 'reviews.product_id=sub3_category.id');
$this->db->where('supplierid_fk', $this->session->id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$results = $query->result();
}
return $results;
}
view page
<?php
foreach ($breview as $row) {
?>
<div class="reviewsection">
<img src="<?php echo 'data:image;base64,' .$row->product_image; ?>" class="img-circle img-user" alt="" width="50px;" height="50px;"/>
<span class="starfont"><botton class="btn btn-danger"> <?php echo $row->ratings; ?> <span class="fa fa-star starfont"></span></botton> </span>
<div class="content-left">
<p><b>Product Name:<?php echo $row->product_name; ?></b></p>
<p><?php echo $row->review_msg; ?></p>
<?php $buyer_review = strtotime($row->review_date);?>
<?php $date=date('d-F-Y',$buyer_review); ?>
<p>Buyer Name : <?php echo $row->first_name; ?> <?php echo $date ; ?></p>
</div>
</div>
<?php } ?>
$results is not defined.
Please add $results = [];
Here you go:
$results = [];
if ($query->num_rows() > 0) {
$results = $query->result();
}
return $results;
In my model, in most cases, i do the following
public function my_function() {
//$qry = YOUR_QUERY
if ($qry->num_rows() > 0) //or ==1 or whatever, depends on your structure
return $qry->result_array(); // or row_array, depends on your structure
return FALSE;
}
Then in your controller you can check if the result is FALSE or EMPTY like:
$result_from_model = $this->my_model->my_function();
if($result_from_model && !empty($result_from_model)) {
//your code here
}
You can use the following code :
return (is_array($results)?$results:array());
Hope it works.
All the answers given so far are good. Here is yet another way to do it. It's essentially the same as the accepted answer only using a ternary instead of if.
$query = $this->db->get();
return $query->num_rows() > 0 ? $query->result() : array();
}
i am working with this ajax Pagination in Codeigniter HMVC. i get the output i wanted.. BUT only on the first link. then after that, the next pages dont open. or when i click them nothing happens. i checked the element and it has errors.
Failed to load resource: the server responded with a status of http://http://localhost/TLC_HR/Job_Titles/ajaxPaginationData/10 500 (Internal Server Error)
jQuery-2.1.4.min.js:4 POST http://localhost/TLC_HR/Job_Titles/ajaxPaginationData/10 500 (Internal Server Error)k.cors.a.crossDomain.send # jQuery-2.1.4.min.js:4n.extend.ajax # jQuery-2.1.4.min.js:4n.(anonymous function) # jQuery-2.1.4.min.js:4onclick # Job_Titles:524
here are my codes.
Job_Titles.php - controller
<?php
class Job_Titles extends MY_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Job_Titles_Model');
$this->load->library('Ajax_pagination');
$this->perPage = 10;
}
// VIEW REDIRECTING /////////////////////////////////////////////////////////
public function index(){
/// view ajax config/////
$data = array();
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get data // must pass this to $this->load->view('content_view') and make it $this->load->view('content_view',$data,false)
$data['job_titles'] = $this->Job_Titles_Model->getRows(array('limit'=>$this->perPage,$this->uri->segment(3)));
$data['content_view'] = 'Job_Titles/jobtitles_read';
$this->templates->admin_template($data);
}
public function add_view(){
$data['content_view'] = 'Job_Titles/add_view';
$this->templates->admin_template($data);
}
//// pagination
public function ajaxPaginationData(){
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//pagination config
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get post data
$data['job_titles'] = $this->Job_Titles_Model->getRows(array('start'=>$offset,'limit'=>$this->perPage));
//load the view
$data['content_view'] = "'Job_Titles/jobtitles_read', $data, false";
$this->templates->admin_template($data);
}
}
?>
Job_Titles_Model.php -- model
<?php
class Job_Titles_Model extends CI_Model{
function __construct() {
$this->table = 'job_titles';
}
///// VIEW PAGINATION /////////////////////////////////////////////////////////
public function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
if(array_key_exists("start", $params) && array_key_exists("limit", $params)){
$this->db->limit($params['limit'],$params['start']);
}elseif (!array_key_exists("start", $params) && array_key_exists("limit", $params)) {
$this->db->limit($params['limit']);
}
$query = $this->db->get();
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
}
?>
admin_template.php --- template
<li>
<i class="fa fa-circle-o"></i>Job Titles
</li>
<div class="box-body">
<?php $this->load->view($content_view); ?>
</div>
and add_view.php --- where pagination will show.
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/bootstrap/css/inputfield.css">
<div>
<center>
<fieldset>
<?php
echo form_open('Job_Titles/create');
?>
<p>
<label class="field" for="JOB_CODE"><span>*</span>Job Code:</label>
<input type = "text" name="JOB_CODE" class ="textbox-300">
<label class = "error"><?php echo form_error("JOB_CODE"); ?></label>
</p>
<p>
<label class="field" for="JOB_NAME"><span>*</span>Job Name:</label>
<input type = "text" name="JOB_NAME" class ="textbox-300">
<?php echo form_error("JOB_NAME"); ?>
</p>
<?php
echo form_submit('submit','Save');
echo form_close();
?>
</fieldset>
</center>
</div>
i've to give an answer base on my comment (i will extend my answer based on your reply because we have to start somewhere to clean this mess ;))
first of all in your Job Titles Controller replace the part with your content view as a string with the following line of code
$data['content_view'] = array(
"url" => "Job_Titles/jobtitles_read",
"data" => $data,
"blnReturnAsAString" => false
);
and in your view you should write this
$this->load->view($content_view["url"], $content_view["data"], $content_view["blnReturnAsAString"]);
I am facing server 500 error on following website, fabcoders.com/ghostwriter
Login in as username: chelsea Password:Chelsea123
once logged in you'll be directed to http://fabcoders.com/ghostwriter/dashboard page.
There you'll find a listing as topic,submission,view click the view link.
You'll be directed to http://fabcoders.com/ghostwriter/project/view_submission/230 page click the view. and that that is were i get the 500 error. It show blank but once you inspect element and go to console there you can see the error.
The code is in codeigniter
Here is the controller section
public function view_submission_detail ($submission_id)
{
$user_id = $this->session->userdata("id");
$this->load->model("submission_m");
$this->load->model("ratings_m");
$star = $this->input->post("rating");
$review=$this->input->post("reviews");
$tip=$this->input->post("tip");
$record = $this->submission_m->get($submission_id);
$rat_to=$record->fk_user_id;
//dump($record);
$select = $this->ratings_m->get($rat_to);
//$tip=$select->tip;
$this->load->model("addproject_m");
$temp = $this->user_m->get($rat_to);
$emailid = $temp->email;
$user_id = $temp->id;
$prjct = $this->addproject_m->get($record->fk_project_id);
//dump($record->fk_project_id);
//dump($prjct);
$pname = $prjct->p_title;
$buyername = $prjct->p_buyname;
$this->data['tip'] = $tip;
$this->data['numele'] = count($this->submission_m->viewusersub($user_id));
$this->data['numacc'] = count($this->submission_m->viewuseracc($user_id));
$this->data['numrej'] = count($this->submission_m->viewuserreject($user_id));
$this->data['numrew'] = count($this->submission_m->viewuserrewrite($user_id));
//$this->data['usersubs'] = $this->submission_m->viewusersub($user_id);
$get_rating_count=$this->submission_m->get_rating_count($rat_to);//rating_m
$this->data['get_rating_count']=$get_rating_count;
$get_rating_details=$this->submission_m->get_rating_details($rat_to);//rating_m
$this->data['get_rating_details']=$get_rating_details;
$sum=0;
foreach($get_rating_details as $rows)
{
$sum=$sum+$rows->rating;
}
$average_rating=0;
if($get_rating_count != 0)
$average_rating=(int)($sum/$get_rating_count);
if($this->input->post("accept") && $this->input->post("pk_submission_id")){
redirect('dashboard');
}else if($this->input->post("reject") && $this->input->post("pk_submission_id")){
$this->submission_m->reject_submission($submission_id);
$this->submission_m->rejectMail($emailid,$pname,$buyername);
redirect('dashboard');
}else if($this->input->post("rewrite") && $this->input->post("pk_submission_id")){
$this->submission_m->rewrite_submission($submission_id);
$this->submission_m->rewriteMail($emailid,$pname,$buyername);
redirect('dashboard');
}
$this->data['average_rating']=$average_rating;
$this->data['submission'] = $this->submission_m->get($submission_id);
$this->data['username'] = $this->user_m->get($this->data['submission']->fk_user_id);
$this->data['project'] = $this->addproject_m->get($this->data['submission']->fk_project_id);
$this->load->view('project/view_submission_details', $this->data);
}
Here is the view part
p_title ?>
img/close.png">
Writer's Name:
firstname ?>
Submitted Date:
created))); ?>
Rating:
</li><?php if($tip!= ""){?>
<li class="label-list">Tip for Writer:</li>
<li><?php echo $tip; } ?></li>
<br>
<?php if($numele>=1)
echo "Previous submissions";?>
<br>
<?php if($numele>=1)
echo "Number of submissions ".$numele;?>
<br>
<?php if($numacc>=1)
echo "Number of Projects Accepted ".$numacc;?>
<br>
<?php if($numrej>=1)
echo "Number of Projects Rejected ".$numrej;?>
<br>
<?php if($numrew>=1)
echo "Number of Projects Rewritten ".$numrew;?>
<br>
<? } ?>
<?php //echo "Tip".$get_rating_details->tip ?>
</ul>
<div class="content1 div-center2 colorwhite">
<p class="text-content"><?php echo nl2br( htmlentities($submission->content)) ?></p>
</div>
<div class="buttons">
<ul class="button-row">
<?php if(!($project->p_complete==1) && ($submission->status=='NOT REVIEWED')) { ?>
<?php
echo form_open('','id="contentForm"');
echo form_hidden("pk_submission_id",$submission->pk_submission_id);
echo " ".form_submit("accept","Accept",'class="btn-green .btn-small" id="acceptBtn"');
echo " ".form_submit("reject","Reject",'class="btn-red .btn-small"');
echo " ".form_submit("rewrite","Rewrite",'class="btn-blue .btn-small"');
echo form_close();
?>
<?php } ?>
</ul>
</body>
</html>
<script>
$(function() {
$("#acceptBtn").click(function(e){
var sdata = $('#contentForm').serialize();
$.ajax({
url: "<?php echo site_url('project/accept_submission'); ?>",
type: "POST",
data:sdata +'&accept=1',
success: function(data){
$('.buttons').remove();
$('#baseModal-xs .modal-content').html(data);
$('#baseModal-xs').modal("show");
}
});
return false;
});
});
</script>
It works locally but it does not run when live the project is still in development stage.
I don't know much about servers cause i only do coding, but in my company i was told to fix the issue and i have no clue my week end is going to be ruined :(
I had previously posted regarding the same issue but it was for different section even that was not fixed.
I don't know if the model part also can cause issue but i cant put the code cause i have passed to limit of the question size
In the view part i had added a extra closing brace which had no opening brace i removed that brace and Wa-lah it worked :).
I have another error anymore, when I try to display a result in my view, the result is NULL and I can't see the result of my query at models.
Here's my code list :
on the controller (home.php) :
$data['hasil5'] = $this->home_model->popular_list();
on the model (home_model.php) function popular_list() :
function popular_list($limit=2)
{
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
return $query->result();
} //thanks to kumar_v
and on my view (home.php) as a part of "Popular news" :
<h2>Most Popular News :</h2>
<?php
foreach ($hasil5 as $data5):
?>
<div class="welcome clear"><img class="imgl" src="<?php echo base_url(); ?>assets/news/original/<?php echo $data5->image; ?>" alt="" height="119" width="125" />
<div class="fl_right">
<h2><?php echo anchor($data5->kategori.'/detail/'.$data5->id,$data5->title) ?></h2>
<p><?php echo $data5->sinopsis; ?></p>
</div>
</div>
<?php
endforeach;
?>
The results is NULL, can you correct it again? thanks..
try this :
function popular_list($limit = 2) {
$query = $this->db->select('*')
->where('id',$this->uri->segment(3))
->where('publish',1)
->where('viewed >= ',5)
->order_by('id','DESC')
->limit($limit)
->get('news');
print_r($query->result());
return $query->result();
}
You can put the whole thing in $query variable. And also, you can improve your $this->db->where() by putting the params in an array.
You should fix your code to look like so:
<?php
foreach ($data['hasil5'] as $data5):
?>
You were using the wrong variable in your foreach loop.
Checking if the query will return any row is a good idea.
function popular_list($limit=2)
{
$result = null;
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
if($query->num_rows() > 0)
{
$result = $query->result();
}
return $result;
}
Hello I work on WordPress and I have a premium theme which is not supported from the creator anymore so I am trying to make a change; I am not sure that this is called a bug. On comments when a visitor is place a comment; his avatar is not the default 'mystery' in this case (no_avatar.gif) but it automatically takes the avatar of the current post author. I like to shows the default for unregistered users. I would like to notice that the theme is use it's own avatars. I target the code on functions but I am not able to change it, any help would be appreciated, thanks in advance here is the code:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}
Update: The function is calling tgt_get_avatar_link but it seems that is:
function tgt_ad_comment($comment, $args, $depth){
$GLOBALS['comment'] = $comment;
global $helper;
?>
<li>
<div class="comment" id="comment-<?php comment_ID()?>">
<?php //echo get_avatar($comment) ?>
<?php echo $helper->image(tgt_get_avatar_link($comment->user_id), $comment->comment_author, array('title' => $comment->comment_author, 'width' => '58px', 'height' => '58px')) ?>
<div class="comment_content">
<strong><?php echo get_comment_author_link() ?></strong> <?php _e('Say ','ad')?>(<?php comment_time('F j, Y \a\t g:i a') ?>)
<br/>
<?php comment_text() ?>
</div>
</div>
The function is calling $user_id but it seems that is:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}
If the comment author has no defined avatar, you'd like to show a default image. This seems to be the else in your conditional:
else { $avatar = get_the_author_meta('tgt_image'); }
Without a $user_id to pass to get_the_author_meta, it assumes the user ID of the current post author. This is likely causing the behavior you describe.
Try to remove the else statement, skipping straight from if (!empty($user_id)){$avatar = get_the_author_meta('tgt_image', $user_id);} to if (!$avatar){.... Now, when the function is passed a null user_id, it should return TEMPLATE_URL . '/images/no_avatar.gif';