Message: Undefined variable: username Filename: views/receiver_view.php Line Number: 58 - php

Brothers your help needed.
i am trying to develop a chat application in which i am displaying friends at the right top but the following error occurs. i have tried again and again but cant solve the problem.
my code is:
function is:
function get_sessionId() {
$session_id = $this->session->userdata('logged_in');
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view',$data);
}
foreach loop is:
<table>
<?php foreach($users as $row): ?>
<tr>
<td>
<a href="javascript:void(0);" onclick="set_user(<?php echo $row->id; ?>)">
<?php echo $row->username;?>
</a>
</td>
</tr>
<?php endforeach; ?>
</table>
and the query that i have write in model is:
function show_user($sessionId) {
$query = $this->db->query("select id, username from users where id <>'$sessionId'");
return $query->result();
}

In your Controller you have assigned the users to:
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view', $data);
So in View Section it should be like this:
<?php foreach($usernames as $row): ?>
not
<?php foreach($users as $row): ?>

Related

CodeIgniter - joining users, posts, comments tables

im joining 3 tables: users, posts and comments in the following code :
public function join_user_post(){
$this->db->select('
posts.post_body,
posts.date_created,
users.username,
users.user_image,
comments.comment_body,
comments.date_created
');
$this->db->from('posts');
$this->db->join('users', 'users.id = posts.post_user_id ');
$this->db->join('comments', 'comments.comment_post_id = posts.post_id ');
$query = $this->db->get();
return $query->result();
}
then passing this array data to the homepage view via controller:
<?php
class Home extends CI_Controller{
public function index(){
$this->load->model('user_model');
$data['posts'] = $this->user_model->join_user_post();
$this->load->view("user/homepage",$data);
}
}
?>
in the homepage view im trying to echo the post with the username and user image, then looping through comments of each post and echo it out
<body>
<?php include 'navbar.php';?>
<?php foreach($posts as $post): ?>
<div>
<img src="<?php echo $post->user_image ?><br>">
<?php echo $post->username ?><br>
<?php echo $post->post_body ?><br>
<?php echo $post->date_created ?><br>
<?php foreach($posts as $post): ?>
<?php echo $post->comment_body ?><br>
<?php echo $post->date_created ?><br>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</body>
but im rather getting all comments on every post instead of getting the comments that are related to specific post id, the foreach is looping through all comments and echo them on every post, if i remove the foreach loop it will echo only one comment at each post
what should i do to solve this issue?
The problem is when you join posts with comments it return many records of comments with the same post. You can modify a little bit first in your controller to store each post with comments belong to it:
function index()
{
$this->load->model('user_model');
$comments = $this->user_model->join_user_post();
$posts = array();
foreach ($comments as $comment) {
if (array_key_exists($comment->post_id, $posts)) {
$posts[$comment->post_id]['comments'][] = $comment;
} else {
$posts[$comment->post_id]['post_body'] = $comment->post_body;
$posts[$comment->post_id]['username'] = $comment->username;
$posts[$comment->post_id]['date_created'] = $comment->date_created;
$posts[$comment->post_id]['user_image'] = $comment->user_image;
$posts[$comment->post_id]['comments'][] = $comment;
}
}
$data['posts'] = $posts;
$this->load->view("user/homepage", $data);
}
And in your view:
<div>
<img src="<?php echo $post['user_image'] ?><br>">
<?php echo $post['username'] ?><br>
<?php echo $post['post_body'] ?><br>
<?php echo $post['date_created'] ?><br>
<?php foreach ($posts['comments'] as $comment) : ?>
<?php echo $comment->comment_body ?><br>
<?php echo $comment->date_created ?><br>
<?php endforeach; ?>
</div>

How do I make a view showing selected id?

I know how to show all data from db using foreach but do not know how to show only 1 record that I select.
My code:
public function go($id)//controller
{
$data["log"] = $this->product_model->getAll();
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>
<?php endforeach; ?>
Please help me to show one record using foreach.
All you have to do is remove the foreach.
Or, change row() to result().
You should remove foreach and do this
return $this->db->get_where($this->_table, ["ID" => $id])->row_Array();
than you can use this
<tr>
<td>
<?php echo $product['id'] ?>
</td>
<td>
<?php echo $product->['tg_mulai'] ?>
</td>
<td>
<?php echo $product->['keterangan'] ?>
</td>
</tr>
you can use foreach() for only multiple rows
foreach operate on arrays and to show single record you should send it to view as array of objects. To do this just replace $data["log"] with $data["log"][] when you get single record from db. Check below code
public function go($id)//controller
{
$data["log"][] = $this->product_model->getById($id);
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>
First view, where you click to see selected data
<?php foreach ($log as $product): ?>
<a href="<?= base_url('controller/function/').$product->id; ?>"
class="btn btn-info">Edit</a>
<?php endforeach; ?>
Then Controller will receive the id like
public function edit_product($id){
$data['datas'] = $this->your_model->model_function($id);
$this->load->view('your_view', $data);
Model
public function model_function($id){
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('table_name');
$query = $this->db->get();
return $query->result();
}
Single Data View
<?php foreach($datas as $data): ?>
<?php echo $data->id; ?>
<?php echo $data->name; ?>
<?php endforeach; ?>
Let me know if it's ok

Set a variable in url using php and mysql

I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:
<?php
$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];
$query = "SELECT id, client_job_name, job_number_id
FROM `job_name`
WHERE `id`='$id'";
$allJobs = $db->query($query);
?>
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.''; ?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
If you want the variables to be avaialble in the URL you need to read them with $_GET.
Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:
if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
$id = $_GET['id'];
$job_number_id = $_GET['job_number_id'];
}
To set it in your foreach statement:
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php
$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];
echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'';
?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.
Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.
Hope this helps!
Try this.
<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){
$query = "SELECT id, client_job_name, job_number_id FROM `job_name` WHERE `id`='$id'";
$allJobs = $db->query($query);
}else{
echo "Id not in session";
}
?>
<table>
<?php if ($allJobs) { foreach ($allJobs as $site_title) : ?>
<tr>
<td>
<?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php }else{ echo 'No results Found' ;} ?>
This may help you.

PHP Error: Trying to get property of non-object

I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem...
I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database.
The problem comes up when I am trying to access the data from the controller, in my view.
Error I am getting for each echo in my loop in 'view_thread_view' is
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/view_thread_view.php
Any solutions would be greatly appreciated.
Here is my code:
Controller thread
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
Model data_model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , 'qid' , $quest));
{
return $q;
}
}
View view_thread_view
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
</div>
EDIT: QUESTION ANSWERED
Thanks to Girish Jangid fixed the problem this is the working code:
Controller
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
//$data = array();
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
}
Model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , array('qid' => $quest)));
{
return $q;
}
}
View
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $data->uname
?>
</div>
<?php endforeach; ?>
</div>
</div>
You should user db function to fetch results, please use CodeIgniter db Query Results functions, like this
if($records->num_rows() > 0){
foreach($records->result() as $data){
if(isset($data->title)) echo $data->title;
}
}
For more detail please read CodeIgniter Query Result function
Query Results
Try this code
<div title="question_view">
<h1>Question View<h1>
<?php if($records->num_rows() > 0) { ?>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
$records is an array, not an object, therefore you cannot do $records->uname. You probably meant $data there too.
Edit: On second look, it appears $data is an array as well! Arrays are accessed via ['key'] not ->key
Also, your code is way over complicated.
<?php
foreach($records as $data){
$string = <<<STR
<div>
<h2>Title</h2>
{$data['title']}
</div>
...etc
STR;
echo $string;
}
http://www.php.net/manual/en/language.types.string.php

displaying data from mysql

Having a little trouble displaying data from a table. Been looking at my code for the past few hours and can't seem to see the problem. What I am trying to do is when a team is clicked on it will go into the players table and display any player that has that team name on the team page. I keep getting a blank page:
index.php
This is the case that launches the team_view.php
case 'view_team':
$team = $_GET['name'];
$teams = get_players_by_team($team);
include('team_view.php');
break;
team_view.php
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Team Roster</h1>
<!-- display product -->
<?php include '../../view/team.php'; ?>
<!-- display buttons -->
</div>
<?php include '../../view/footer.php'; ?>
team.php
<?php
$team = $team['name'];
$first = $player['first'];
$last = $player['last'];
$age = $player['age'];
$position = $player['position'];
$team = $player['team'];
?>
<table>
<?php foreach ($players as $player) :
?>
<tr>
<td id="product_image_column" >
<img src="images/<?php echo $player['player_id']; ?>_s.png"
alt=" ">
</td>
<td>
<p>
<a href="?action=view_player&player_id=<?php echo
$player['player_id']; ?>">
<?php echo $player['first']; ?>
<?php echo $player['last']; ?>
</a>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
product_db.php
<?php
function get_players_by_team($team) {
global $db;
$query = 'SELECT * FROM players
WHERE team = :team';
try {
$statement = $db->prepare($query);
$statement->bindValue(':team', $team);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
You never define $players it looks like what you are expecting to be $players is actually $teams.
$teams = get_players_by_team($team);
Additionally youre using $player at the top of the script instead of inside the loop which makes no sense.

Categories