Codeigniter 3 - fetch data from another table where current ID - php

I'm working on helpdesk system.
I created two tables contacts and contacts_comments.
Now I fetch contacts details by contact ID in view page - this working correct.
and now from view page I insert comments to second table contacts_comments.
To contacts_comments I insert message and current contact(ticket) ID.
And now I try display all this comments for current ticket ID.
<?php foreach ($comments as $comment): ?>
<div class="d-flex mb-4">
<div class="flex-shrink-0">
<img src="<?php echo base_url(); ?>assets/c_admin/assets/images/users/avatar-8.jpg" alt="" class="avatar-xs rounded-circle" />
</div>
<div class="flex-grow-1 ms-3">
<h5 class="fs-13"><?php echo $comment->ticket_client; ?> <small class="text-muted"><?php echo $comment->created_at; ?></small></h5>
<p class="text-muted"><?php echo $comment->comments_message; ?></p>
<i class="mdi mdi-reply"></i> Reply
</div>
</div>
<?php endforeach; ?>
But this function display for me all comments from table contacts_comments.
I try build function:
//get comments by id
public function get_comments_by_id($id)
{
$id = clean_number($id);
$this->db->where('ticket_id', $id);
$query = $this->db->get('contacts_comments');
return $query->row();
}
Can anyone help me how to add to this query in CI3 with result somelike:
DB WHERE contacts.id == contacts_comments.ticket_id

Related

How to fix `Get all images from database`

PHP code is:
class Gallery{
public function get_images(){
global $pdo;
$query = $pdo->prepare('SELECT * FROM gallerys order by id desc');
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
public function fetch_data($pid){
global $pdo;
$query = $pdo->prepare('SELECT * FROM gallerys where id = ? order by id desc');
$query->BindValue(1,$pid);
$query->execute();
return $query->fetch();
}
}
HTML code is:
$post = new Gallery;
$check = new Gallery;
$galery = $post->get_images();
<?php foreach($galery as $post){?>
<div class="fw-carousel fl-wrap full-height lightgallery">
<div class="slick-slide-item">
<div class="box-item">
<img src="../asset/<?php echo $post['image'];?>" alt="">
<i class="fa fa-search" ></i>
</div>
</div>
</div>
<?php }?>
Database structure:
---id---name---user---post_date---image---
When user post something and put 3 images into table gallerys on column user```` will be places$_SESSION['user_id'];and with that i will retrieve images byuser_id``` now i get only 1 image not all example 3 or 5 or 10...
When user upload example 3 or 5 images into database and want to view his upload, with this code they will get only 1 image, not all image what he upload. I don't get where is the problem in this script.
Any help?
You're generating a carousel/gallery/slider for each of your three images, overlapping each other. What you want is to create one carousel/gallery/slider with your three images inside. Replace
<?php foreach($galery as $post){?>
<div class="fw-carousel fl-wrap full-height lightgallery">
<div class="slick-slide-item">
<div class="box-item">
<img src="../asset/<?php echo $post['image'];?>" alt="">
<i class="fa fa-search" ></i>
</div>
</div>
</div>
<?php }?>
with
<div class="fw-carousel fl-wrap full-height lightgallery">
<?php foreach($galery as $post){?>
<div class="slick-slide-item">
<div class="box-item">
<img src="../asset/<?php echo $post['image'];?>" alt="">
<i class="fa fa-search" ></i>
</div>
</div>
<?php }?>
</div>

Query the database in foreach results

I'm trying to build a webpage with simple status posts. I've created foreach loop to query the database and load all the posts:
<?php
$user_id = Check::isLoggedIn();
$dbstories = DB::query('SELECT * FROM stories WHERE user_id=:user_id ORDER BY story_time DESC', array(':user_id'=>$user_id));
$post_story = "";
foreach($dbstories as $story) {
?>
<div>
<article>
<div>
<a href="profile.php">
<?php
// to pull user first name
$user_first = DB::query('SELECT user_first FROM users WHERE user_id=:user_id', array(':user_id'=>$user_id))[0]['user_first'];
echo $user_first;
?>
</a>
<div>
<time>
<?php
// to pull post datetime
$post_time = $story['story_time'];
echo '<time class="timeago" datetime="' . $post_time . '"></time>';
?>
</time>
</div>
</div>
</div>
<?php
// to pull post contents
$post_story = $story['story_body'];
if (strlen($post_story) <= 25) {
echo '<h3>' . $post_story . '</h3>';
} else {
echo '<p>' . $post_story . '</p>';
}
?>
<div>
<div>
<a href="#">
<svg class="thunder-icon"><use xlink:href="icons/icons.svg#thunder-icon"></use></svg>
<span>
<?php
// to pull number of likes
$post_likes = $story['story_likes'];
echo $post_likes;
?>
</span>
</a>
<!-- the same for comments and shares number goes here -->
</div>
</div>
<div>
<!-- This form over here should pull the story_id which is the post ID from the database like the other queries up there and post it next to the url then that will allow me to use it in isset($_GET['story_id']) and trigger a query to increment likes -->
<form action="stories.php?post_id=<?php $post_id = $story['story_id']; echo $post_id; ?>" method="post" id="like_story">
<a href="#" class="btn btn-control" id="post_like">
<svg class="like-post-icon"><use xlink:href="icons/icons.svg#thunder-icon"></use></svg>
</a>
</form>
<!-- the same for comment and share goes here -->
</div>
</article>
</div>
<?php
}
?>
*Please read the comments within the code
Everything works fine but, I still cant get each post's ID linked to the like button (like_story). It only access the most recent post's ID.
Any workaround to reference the post ID (story_id)?

How to pass extracted values from mysql data base from one php file to another

I want to pass the specific extracted userID for each instance to another php file full_details.php so it generates a page dynamically
$stmt = $DB_con->prepare('SELECT userID, userDate, userName, userAge, userSex, userPic, userLocation, userStatus, userDetails FROM tbl_users ORDER BY userID DESC');
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<div class="col-sm-4 col-xs-6">
<div class="single-product">
<figure>
<img src="user_images/<?php echo $row['userPic']; ?>" />
<figcaption>
<div class="read-more">
<i class="fa fa-angle-right"></i> Full Details
</div>
</figcaption>
</figure>
<h4><?php echo $row['userName']." / "."age :".$row['userAge']; ?></h4>
<h5><?php echo $row['userStatus'] ?></h5>
</div> <!-- end .single-product -->
</div> <!-- end .grid-layout -->
Not easy to understand what you are asking...
I want to pass the specific extracted userID for each instance to another php file full_details.php so it generates a page dynamically
You would need to pass that ID in the URI for that href then right? Something like this:
<div class="read-more">
<i class="fa fa-angle-right"></i> Full Details
</div>
Then on full_details.php you can retrieve that GET parameter like so:
$userId = (isset($_GET['user_id')) ? $_GET['user_id'] : null;
Does that help?

Get data from database and use loop in php

I am working on college project and i am stuck at the moment.
I have a table and it have multiple records. Records are saved by user. I want to show all data stored by specific user on webpage (under a separate div).
I am able to fetch all data from database by using below code.
$prj= mysql_query("select * from project where uid=$uid");
$record= mysql_fetch_assoc($prj);
$project_name = "$record['project_name']";
$short_dis= "$record['short_dis']";
$poster= "$record['poster']";
mysql_close($prj);
Here are some php code.
<div class="media services-wrap55 wow fadeInDown">
<img class="img-responsive" src="uploads/poster/photo-original_827156899.jpg"><br>
<h4 class="media-heading">second project for testing</h4>
<p> by user</p>
<p> second project for testing second project for testing second project for testing second project for testing.</p>
<a class="" href="iska-link.html">More ... </a>
<p>
<div id="progressBar2" class="tiny-green"><div></div></div><p>
<div class="counter-inner"><div id=example1" data-countdown="07/02/2015 00:11:00"></div></div><p>
<div class="col-sm-11 text-right">
<div class="entry-meta">
<span><i class="fa fa-comment"></i> 2 Comments </span>
<span><i class="fa fa-thumbs-up"></i> 56 </span>
<span><i class="fa fa-thumbs-down"></i> 56 </span>
<span><i class="fa fa-star"></i> 56 Fav </span>
</div>
</div>
</div>
</div>
As you can see these are static data. I want to put dynamic data from database in this.
Is there any advise how to get the data in div by loop.
I can show one record by using echo. I am not looking for entire code just wanted database part and some sample while using it in php.
I didn't know your table structure, so a sample code is given so that you can take help of it and do accordingly:-
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('hostname','username','password','databasename');
$prj= mysqli_query($conn,"select * from project where uid=$uid") or die(mysqli_error($conn));
$record = array();
while($row = mysql_fetch_assoc($prj)){
$record[] = $row;
}
mysql_close($conn);
?>
<?php foreach($record as $rec){?>
<div class="media services-wrap55 wow fadeInDown">
<img class="img-responsive" src="uploads/poster/<?php echo $rec['image_path'];?>"><br> // assuming that image_path is the field in table where you putted image name
<h4 class="media-heading"><?php echo $rec['project_name'];?></h4>// assuming that project_name is the field in table where you putted project name
<p><?php echo $rec['user_name'];?></p>// assuming that user_name is the field in table where you putted user name
<p><?php echo $rec['project_description'];?></p>// assuming that project_description is the field in table where you putted project name
<!-- in the same way you can do for others also -->
<a class="" href="iska-link.html">More ... </a>
<p>
<div id="progressBar2" class="tiny-green"><div></div></div><p>
<div class="counter-inner"><div id=example1" data-countdown="07/02/2015 00:11:00"></div></div><p>
<div class="col-sm-11 text-right">
<div class="entry-meta">
<span><i class="fa fa-comment"></i> 2 Comments </span>
<span><i class="fa fa-thumbs-up"></i> 56 </span>
<span><i class="fa fa-thumbs-down"></i> 56 </span>
<span><i class="fa fa-star"></i> 56 Fav </span>
</div>
</div>
</div>
</div>
<?php } ?>
Note:- stop using mysql_*, it is officially deprecated. use mysqli_* or PDO.
First off, you should be using PDO to query your database instead of mysql_query(). You code as it is now allows for SQL injection.
Here's how I would do it:
$db = new PDO('mysql:host=CHANGE_THIS_TO_YOUR_HOST_NAME;
dbname=CHANGE_THIS_TO_YOUR_DATABASE',
'CHANGE_THIS_TO_YOUR_USERNAME',
'CHANGE_THIS_TO_YOUR_PASSWORD');
$sql='select * from project where uid = :uid';
$query = $db->prepare($sql);
$query->bindValue(':uid', $_REQUEST['uid']);
$query->execute();
$projects = $query->fetchAll();
Then to display it in a loop, do this:
foreach ($projects as $project) {
echo '<div class="project">';
echo '<span class="project-name">'. $project['project_name'] .'</span>';
echo '<span class="project-dis">'. $project['short_dis'] .'</span>';
echo '<span class="project-poster">'. $project['poster'] .'</span>';
echo '</div>';
}
// database query to get result
$result = mysqli_query($conn, "select * from project where uid=$uid")
// new array for all rows
$rows = array();
// run mysql_fetch_assoc() in a loop (iterate) to get all rows from
// the whole result set and reassign them to the prepared and
// empty $rows array
// so that you can later iterate rows again to output your divs
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
mysql_close($conn);
// now iterate the $rows array to output data for each $row
<?php foreach($rows as $row){ ?>
// debug output to see what data is in a row
var_dump($row);
// print value of a key from the row array
echo $row['some_keyname'];
// use stringconcatenation to build your html output, e.g. div
echo '<div>' . $row['some_keyname'] . '</div>';
<?php } ?>

SELECT COUNT(*) FROM 'accounts' WHERE role = 'engineer' getting the total number of rows using symfony1.4

Hi i have a problem in getting all the data from the table accounts, I want to get the total number of data's from the table accounts using WHERE. Let's say from the table accounts where role = engineer. all the total number of rows from the field role = engineer will get. Im using symfony framework on this
Here's my controller below
<?php
class spFindRoleDetailAction extends sfAction{
function preExecute(){
}
public function execute($request){
$this->setLayout('spLayout');
$this->setTemplate('sp/roleDetail');
$role = $request->getParameter('ro');
$this->roleDetail = AccountTable::getInstance()->getRoleDetail($role);
$this->roleCount = AccountTable::getInstance()->getCountDetail($role);
}
}
Model query
public function getCountDetail($role){
return $this->createQuery('a')
->where('a.role = ?', $role)
->execute();
}
and my module templates view code here
<div id="search-by-role-detail" data-role="page">
<div role="main">
<ul data-role="listview">
<li>
<a href="<?php echo url_for('#find-sp-role-page'); ?>" class="ui-btn ui-btn-icon-left ui-icon-nav-l center">
<h1>エンジニア<span class="header-result-count">(<?php echo $roleCount->count(); ?>)</span></h1>
</a>
</li>
<?php if(isset($roleDetail)): ?>
<?php foreach($roleDetail as $role): ?>
<li>
<a href="<?php echo $role->getSlug(); ?>"class="ui-btn ui-btn-icon-right ui-icon-list-r" data-transition="slide">
<?php if($role->getLogo() == NULL): ?>
<div class="middle v-image">
<img class="image-user" src="/images/sp/sample.png">
</div>
<?php else: ?>
<div class="middle v-image">
<img class="image-user" alt="<?php echo $role->getName(); ?>" src="http://img.creww.me/uploads/account/logos/<?php echo $role->getLogo(); ?>">
</div>
<?php endif; ?>
<div class="middle v-list">
<h3><?php echo $role->getName(); ?></h3>
<p><?php echo $role->getOverview(); ?></p>
</div>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
The one in the H1 tag there is a count in there, How will i able to get the total number of rows in it. Im using there a <?php echo $roleCount->count(); ?> which is wrong
Anyone can help me out on this??
Any help is muchly appreciated
Thanks!
If count() isn't working then you can try something like this.
public function getCountDetail($role)
{
$q = Doctrine_Query::create()
->from('[some table] a')
->where('a.role = ?', $role)
->select('COUNT(a.id)')
->execute(array(),Doctrine_Core::HYDRATE_SINGLE_SCALAR)
return $q;
}
In this case the function will return a single number representing the number of records found.

Categories