How to fix `Get all images from database` - php

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>

Related

Codeigniter 3 - fetch data from another table where current ID

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

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?

Take value from array and select it in mysql php

I have problem that I store in DB column (video_id) and the value of it like that store (1,3,4,7) the number mean the id of video in the Video table which store the video title and text and all information about the video but when I wrote code to select all video the result give me one video
<div class="panel-body">
<?
$qu="SELECT video_id FROM `training_questions` WHERE id=$questions_id AND category_id=$training_id";
$query_c= mysqli_query($con, $qu);
while ($row7 = mysqli_fetch_assoc($query_c)) {
$vedoes=$row['video_id'];
$pieces = explode(",", $vedoes);
}
for($i=0;$i<=count($pieces);$i++){
$query = "SELECT * FROM `video` WHERE id IN $pieces[$i] ";
$query_che= mysqli_query($con, $query);
while ($row2 = mysqli_fetch_assoc($query_che)) { ?>
<div class="col-lg-3 col-sm-6">
<div class="thumbnail">
<div class="video-container">
<div class="thumb"> <img src="../upload/<?= $row2['img'] ?>" class="img-responsive img-rounded media-preview" alt=""> <span class="zoom-image"><i class="icon-play3"></i></span> </div>
</div>
<div class="caption">
<h6 class="no-margin">
<a href ="video_details.php?id=<?= $row2['id'] ?>" class="text-default"><?= $row2['title'] ?>
</a>
</h6>
</div>
</div>
</div>
<?
}
} ?>
</div>
How I can fix it?
Remove the
$pieces = explode(",", $vedoes);
its useless
make your for loop condition look like this
for($i=0;$i<=count($vedoes);$i++)
Hope it Helps

i want to display only those images according to the category_id selected

i want to display only those images according to the category_id selected but iam not getting images here and and for all category_id the value showing is category_id=9
here is my controller
public function men_clothing($p_id=null)
{
$data['active_mn']='men_clothing';
$data['men']=$this->roxmodel->get_category_by_parent($p_id=8);
$data['kids']=$this->roxmodel->get_category_by_parent($p_id=9);
$config['base_url'] = base_url().'roxcontrol/men_clothing';
$config['per_page'] = 9;
$config['uri_segment'] = 3;
$config['total_rows'] = $this->roxmodel->count_gallery();
$data['galllery']=$this->roxmodel->get_gallery_men_images($p_id,$config['per_page'],$this->uri->segment(3));
var_dump($data['galllery']);
$this->load->library('pagination',$config);
$data['page_links'] = $this->pagination->create_links();
$this->load->view('men_clothing',$data);
}
here is my model
public function get_gallery_men_images($p_id=null)
{
$this->db->where('gallery.category_id',$p_id);
$this->db->select('gallery.*,category.category_name');
$this->db->join('category','category.id=gallery.category_id');
return $this->db->get('gallery')->result();
}
here is my view page
<ul class="sidebar-tags">
<li>View All </li>
<?php foreach($men as $row) {?>
<li><?php echo $row->category_name; ?> <span> </span></li>
<?php } ?>
</ul>
<?php if( !empty($galllery) ) {
foreach($galllery as $row){?>
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="two-product">
<!-- single-product start -->
<div class="single-product">
<!-- <span class="sale-text">Sale</span>-->
<div class="product-img">
<a href="#">
<img class="primary-image" ima="<?php echo base_url();?>images/<?php echo $row->image;?>" src="<?php echo base_url();?>images/<?php echo $row->image;?>" alt="" />
<img class="secondary-image" ima="<?php echo base_url();?>images/<?php echo $row->image;?>" src="<?php echo base_url();?>images/<?php echo $row->image;?>" alt="" />
</a>
<div class="action-zoom">
<div class="add-to-cart">
<i class="fa fa-search-plus"></i>
</div>
</div>
<!-- <div class="price-box">
<span class="new-price">$110.00</span>
</div>-->
</div>
<div class="product-content">
<h2 class="product-name"><?php echo $row->title;?></h2>
<p><?php echo $row->description;?></p>
</div>
</div>
<!-- single-product end -->
</div>
</div>
<?php }}?>
here the image is not displaying and getting like this
SELECT `gallery`.*, `category`.`category_name` FROM `gallery` JOIN `category` ON `category`.`id`=`gallery`.`category_id` WHERE `gallery`.`category_id` = 9
my table(gallery) looks like this
id image category_id
93 img1455604030.jpg 10
94 img1455605183.jpg 11
95 img1455616291.jpg 11
96 img1455617201.jpg 10
97 img1455617299.jpg 10
98 img1455681918.jpg 13
99 img1455681957.jpg 12
there is a problem in you join query
$this->db->select('*');
$this->db->from('gallery');
$this->db->where('category.id',$p_id);
$this->db->join('category', 'category.id = gallery.category_id');
$query = $this->db->get()->result();
return $query;
If what you want is to get or Filter result which has the same category_id: the query should be like this
public function get_gallery_men_images($p_id=null)
{
$img = $this->db->select('gallery.*')
// why not add condition IF p_id = null
// execute only if p_id not null otherwise it return error
if($p_id != null):
->where('gallery.category_id',$p_id)
endif;
// we are using LEFT JOIN to fetch the category data's
->join('category','category.id=gallery.category_id', 'left')
->get('gallery');
return $img->result();
}

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 } ?>

Categories