Query to select similar post like current - php

I have table posts with following rows:
post_id
post_title
post_image
post_body
category
Second table category where categories are stored
cat_id
cat_name
When I click on post 1 it is open in new page post.php?post_id=1.. What I trying to do now is on the new page to have similar post only from the same category.
This is the code that I have on single post page
function truncate–s($text, $chars = 80) {
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text." ...";
return $text;
}
if(isset($_GET['post_id']) && is_numeric($_GET['post_id'])){
$post_id = $_GET['post_id'];
$query = $pdo->query("SELECT * FROM posts WHERE post_id = $post_id");
foreach ($query as $row) {
echo '<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
<div class="portfolio-slider-wrapper">
<ul id="portfolio-slider">
<h4>'.$row['post_title'].'</h4>
<hr>
<li><img src="'.$row['post_image'].'" alt="" class="img-responsive"/> </li>
</ul>
</div>
<div class="blog-desc">
<ul class="post-meta-links list-inline">
<li><span> <i class="fa fa-calendar"></i></span>'.$row['post_date'].'</li>
</ul>
<p>
'.$row['post_body'].'
</p>
</div>
}
echo ' <div class="clearfix"></div>
<div class="related-post">
<h4>Related</h4>
<hr>';
$query1 = $pdo->query("SELECT * FROM posts INNER JOIN category ON posts.category=category.cat_id LIMIT 3");
foreach ($query1 as $post) {
echo '<div class="col-md-4 col-sm-4">
<div class="rel-post">
<a href="#">
<img src="'.$post['post_image'].'" alt="" lass="img-responsive">
<div class="caption">
<h4>'.$post['post_title'].'</h4>
<p>'.truncate–s($post['post_body']).'</p>
</div>
</a>
</div>
</div>';
}
Currently when I open some post it doesn't mater what ID have it's always load first 3 post from the database table.
How to load the posts from same category?

Just try with that query.
"SELECT * FROM posts INNER JOIN category ON posts.category=category.cat_id WHERE posts.category = '".$row['category']."' LIMIT 3"
Be sure that you have only one element in $query.

Related

How to display the datas from two tables using search query?

I need to display datas from particular based on the results.
I have two tables movies_info => has all movies info & tv_shows_info => has TV shows info. I have search box if i enter the search it should search from both the table and display the datas but the condition in the results if it has movie_id, it should show movie details and if the result has tv_show_id it should show tv-show details.
as of now i have used basic search to fetch datas from one table and i have tried of using two but its not working.
<?php
// sql query for retrieving data from database
$sql_query = "SELECT * FROM `movies_info`";
$result = mysqli_query($connection, $sql_query);
// SQL Query for filter
if(isset($_POST['search_button']))
{
$value_to_search = $_POST['value_to_search'];
// search in all table columns
// using concat mysql function
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`, `movie_original_name`, `release_year`, `movie_genre`, `movie_country`, `movie_stars`, `movie_director`) LIKE '%".$value_to_search."%'";
//$search_query = "SELECT * FROM `movies_info`,`tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%' GROUP BY `movie_id`";
//$search_query = "SELECT * FROM `movies_info` UNION ALL `tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%'";
//$search_query2 = "SELECT * FROM `tv_shows_info` WHERE CONCAT(`tv_show_name`, `tv_show_start_year`, `tv_show_end_year`, `tv_show_genre`, `tv_show_country`) LIKE '%".$value_to_search."%'";
//$search_query .= $search_query2;
$search_result = filterTable($search_query);
}
else {
$search_query = "SELECT * FROM `movies_info`";
$search_result = filterTable($search_query);
//echo 'No Search Results Found';
}
?>
<!-- /w3l-medile-movies-grids -->
<div class="general-agileits-w3l">
<div class="w3l-medile-movies-grids">
<!-- /movie-browse-agile -->
<div class="movie-browse-agile">
<!--/browse-agile-w3ls -->
<div class="browse-agile-w3ls general-w3ls">
<div class="tittle-head">
<h4 class="latest-text">Search Results for : "<?php echo $value_to_search ?>"</h4>
<div class="container">
<div class="agileits-single-top">
<ol class="breadcrumb">
<li>Home</li>
<li class="active" style="text-transform:Capitalize;">Search Results </li>
</ol>
</div>
</div>
</div>
<div class="container">
<div class="browse-inner">
<?php
echo $search_result;
$rowcount = mysqli_num_rows($search_result);
for($i=1;$i<=$rowcount;$i++){
$row=mysqli_fetch_array($search_result);
?>
<div class="col-md-2 w3l-movie-gride-agile">
<a href="movie.php?movie_id=<?php echo $row['movie_id']; ?>" class="hvr-shutter-out-horizontal"><img src="<?php echo $row['movie_image']; ?>" title="<?php echo $row['movie_name']; ?>" alt=" " />
<div class="w3l-action-icon"><i class="fa fa-play-circle" aria-hidden="true"></i></div>
</a>
<div class="mid-1">
<div class="w3l-movie-text">
<h6><?php echo $row['movie_name']; ?></h6>
</div>
<div class="mid-2">
<p><?php echo $row['release_year']; ?></p>
<div class="block-stars">
<ul class="w3l-ratings">
<li>
<span>IMDB <i class="fa fa-star" aria-hidden="true"></i> <?php echo $row['movie_rating']; ?> </span>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="ribben two">
<p>NEW</p>
</div>
</div> <?php } ?>
</div>
as of now i can get the values from one table.,
My exact need is it can be Movie or TV-Show but i should get the datas from both the table if it is a movie it should show some particular info and if thats a TV-show it should show someother info.
First, you need to use prepared statements which you can use even if you are using the LIKE function in MySQL. Then you need to add spaces between the column names to prevent the values from being blended together like "star warsstar wars". This will cause a movie like "star wars to be a result when a user searches for "ss" which would be inaccurate.
$search_result = array();
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`,' ', `movie_original_name`,' ',`release_year`,' ',`movie_genre`,' ',`movie_country`,' ',`movie_stars`,' ',`movie_director`) LIKE CONCAT('%',?,'%')";
if($stmt = $db->prepare($search_query)){
$stmt->bind_param('ii',$_SESSION['iidn_reference'],$o['parent_ref_id']);
if($stmt and $stmt->execute()){
$result = $stmt->get_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$search_result[] = $row;
}
}
}
After that, you could combine these tables with MySQL UNION but I strongly recommend just searching both tables and aliasing the table names to match.
SELECT 'movie' as `type`, `movie_id` as `id`, `movie_name` as `name`...
SELECT 'show' as `type`, `show_id` as `id`, `show_name` as `name`...

How to limit the number of 'Recent Blogs' created in CakePHP?

I'm trying to create a blog, and in every single blog it shows the recent blogs that has been created on the bottom of the page. Is there a way i can limit this number to 4 recent blogs? Because currently all the blogs that has been created show's up on the "Recent Blogs" area when i generate it.
<div class="container" id="newsextra">
<h4>MORE NEWS</h4>
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($blogsinfos)) {
foreach ($blogsinfos as $blogs): ?>
<div class="col-md-3">
<a href="/news-single/<?= h($blogs->id)?>">
<img src="<?= h($blogs->mainimg)?>" class="img-responsive">
<h5><?= h($blogs->title)?></h5>
<h6><?= h($blogs->created)?></h6>
</a>
</div>
<?php
endforeach;
}
?>
</div>
</div>
From the controller you should limit the number of posts generated. Thanks #Rik.esh for the answer.
$this->loadModel('Blogs');
$opts1['conditions'] = array('Blogs.status' => 1);
$opts1['limit'] = 4;
$opts1['order'] = array('Blogs.created' => 'desc');
$blogsinfos = $this->Blogs->find('all',$opts1);
$this->set('blogsinfos', $blogsinfos);
$this->set('_serialize', ['blogsinfos']);
foreach ($blogsinfos as $blogs) {
$proid = $blogs['id'];
}
try this
$opts1['order'] = 'Blogs.created desc';

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

Category is not placed at the correct section

I have a problem while printing categories and sections (for a forum). So I have 2 tables : forum_section and forum_category -> the cat_id column from forum_section is linked with the section column from forum_category. Here's my query:
$forum = $con->query("SELECT a.*, b.*
FROM `forum_section` a
INNER JOIN `forum_category` b ON a.`cat_id` = b.`section` GROUP BY a.`cat_name` ORDER BY a.`cat_id`");
And that's how I'm trying to print them:
while($row = mysqli_fetch_object($forum)) {
echo '<div id="section">
<div class="section-head"><h3 class="yellow_text">'.$row->cat_name.'</h3></div>
'.printCategories($row->cat_id).'
</div>';
}
}
That's my printCategories function:
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
while($row = mysqli_fetch_object($categories)) {
echo '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
}
So it does print them but not in the right place as you can see in this picture:
printCategories needs to return the result, not echo it, so that the concatenation in the caller will put it into the DIV that it's echoing. Your code is echoing before it returns anything, so it gets printed before the caller concatenates the result and prints its DIV.
function printCategories($id) {
global $con;
$categories = $con->query("SELECT * FROM `forum_category` WHERE `section`='$id'");
$result = '';
while($row = mysqli_fetch_object($categories)) {
$result += '<div class="cat">
<div class="cat-title">
<img src="styles/default/images/icons/'.$row->icon.'.png" alt="Icon">
<span>'.$row->name.'</span>
</div>
<div class="cat-topics">
asd
</div>
<div class="cat-posts">
asd
</div>
<div class="cat-lasttopic">
last topic name goes here
</div>
</div>';
}
return $result;
}

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();
}

Categories