How to display count posts using PHP? - php

I want to display the number of posts but unfortunately nothing is displayed. I created a function in which the query counts the number of user's posts, and then it should display this number in html, but it doesn't. I don't know where the problem is.
public function count_photos()
{
$session = $_SESSION['id'];
$query = $this->database->connect()->prepare("SELECT count(*) as photo from photo where user_id= :id");
$query->bindParam(':id',$session, PDO::PARAM_INT);
$query->execute();
if($query->rowcount())
{
while($row = $query->fetch())
{
echo $row['photo'];
}
}
}
<?php
$object = new user_statystics($object);
$post = $object->count_posts();
?>
<main>
<div>Count post:<span> <?php echo $post; ?></span></div>
</main>

Related

PHP - failed to output no data where no data in table

I've tried to find any reference to display data where no data in table for an hours, but I'm not yet found a code to fix the issue
I've this code:
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
// not yet fix how to display no data
//if($row3==false){ tried to change $row3==0 still won't work
//echo "No Data";
//}
//die(var_dump($row3)); showing `bool(false)`
echo $row3['ket_nilai'] (echo $row3['jum_nilai'])
}
any idea how to do?
I want to display some text when no data found in database table
You need to fetch the results before entering the loop, check if you do get the results then if you do enter the loop else display the message
see bellow code :
<?php
//function
function readAll(){
$query = "SELECT * FROM ".$this->table_name." ORDER BY id_nilai ASC";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
return $stmt;
}
//execute
$pro3 = new Nilai($db);
$stmt3 = $pro3->readAll();
$results = $stmt3->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0){
foreach($results as $row3){
//display them
echo $row3['ket_nilai'] ;
echo $row3['jum_nilai'];
}
}else{
echo "No data available";
}
?>

Php code returns empty page. tried all other solutions

I am trying to learn php. I wrote code to insert post to database and get posts to display on the home page. I wrote pagination code as well and tried to run it. but as a result php is returning an empty page with no code and no error.
Tried every thing. Went to through most of the answers here. I still can figure out the error.
<?php
$con = mysqli_connect("localhost","root","password","social") or die ("Connection was not established");
//function for getting topics
function getTopics() {
global $con;
$get_topics = "select * from topics";
$run_topics = mysqli_query($con,$get_topics);
while($row=mysqli_fetch_array($run_topics)) {
$topic_id = $row['topic_id'];
$topic_title = $row['topic_title'];
echo "<option value='$topic_id'>$topic_title</option>";
}
}
//function for insert posts to database
function insertPost() {
if(isset($_POST['sub'])) {
global $con;
$title = $_POST['title'];
$content = $_POST['content'];
$topic = $_POST['topic'];
$user = $_SESSION['user_email'];
$get_user = "SELECT * FROM users WHERE user_email = '$user'";
$run_user = mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_id = $row['user_id'];
$insert = "INSERT INTO posts (user_id,topic_id,post_title,post_content,post_date) VALUES ('$user_id','$topic','$title','$content',NOW())";
$run = mysqli_query($con,$insert);
if($run) {
echo "<h3>Posted to timeline. Looks great.</h3>";
$update = "Update users set posts='yes' where user_id='$user_id'";
$run_update = mysqli_query($con,$update);
}
}
}
//function to get posts
function get_posts() {
global $con;
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$start_from = ($page-1)*$per_page;
$get_posts = "SELECT * FROM posts ORDER by 1 DESC LIMIT $start_from, $per_page";
$run_posts = mysqli_query($con,$get_posts);
while($row_posts=mysqli_fetch_array($run_posts)) {
$post_id = $row_posts['post_id'];
$user_id = $row_posts['user_id'];
$post_title = $row_posts['post_title'];
$content = $row_posts['post_content'];
$post_date = $row_posts['post_date'];
//gettin the user who posted the thread
$user = "SELECT * FROM users WHERE user_id='$user_id' AND posts='yes'";
$run_user = mysqli_query($con,$user);
$row_user = mysqli_fetch_array($run_user);
$user_name = $row_user['user_name'];
$user_image = $row_user['user_image'];
//displaying all at once.
echo "<div id='posts'>
<p><img src='user_images/$user_image' width='50' height='50'</p>
<h3><a href='user_profile.php?luser_id=$user_id'>$user_name</h3>
<h3>$post_title</h3>
<p>$post_date</p>
<a href='single.php?post_id=$post_id' style='float:right'><button>See replies</button></a>
</div>";
}
include("pagination.php");
}
?>
pagination code goes like this.
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($con,$query);
//count the total records
$total_posts = mysqli_num_rows($result);
//using ceil function to divide the total records per page
$total_pages = ceil($total_posts / $per_page);
//going to first page
echo "
<center>
<div id='pagination'>
<a href='welcome.php?page=1'>First Page</a>
";
for ($i=1; $i=$total_pages; $i++) {
echo "<a href='welcome.php?page=$i'>$i</a>";
}
//going to last page
echo "<a href='welcome.php?page=$total_pages'>Last Page</a></center>";
?>

How can I get the column of my sql database with $_GET?

I am successfull in getting the id as this is a page called shirt.php?id=2 which is a template to display a specific shirt based on its id. That works but in my database i have a column named "name" and that has the name of the shirt so "shirt2". How can I echo or grab this based on the current id of the shirt. is the code below too redundant? How can I simplify this?
<?php
require 'inc/header.php';
$streamId = (isset($_GET['id']) ? $_GET['id']:NULL);
if ($streamId) {
$sql = 'SELECT * FROM streams WHERE id = :id';
$query = $pdo->prepare($sql);
$query->execute(array(':id' => $streamId));
$listStreamEach = $query->fetchAll(PDO::FETCH_ASSOC);
}else {
echo "not working";
}
$streamIdName = (isset($_GET['name']) ? $_GET['name']:NULL);
if ($streamIdName) {
$sql = 'SELECT * FROM streams WHERE name = :name';
$query = $pdo->prepare($sql);
$query->execute(array(':name' => $streamIdName));
$listStreamEach1 = $query->fetchAll(PDO::FETCH_ASSOC);
print_r($listStreamEach1);
}else {
echo "not working";
}
?>
<?php
require 'inc/footer.php';
?>

how to go to previous article mysql id

I created a php script that I think will work but i do not know why I am getting an Undefined index: id error for the isset($_GET['previous']) code.
this is my php code which includes the script that grabs a the specific clicked article on previous page($_GET['id']) script which creates it own unique ex.(?=2) and is working but might have something to do with it.
include('config.php');
$pdo = connect();
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
try {
$sql = 'SELECT * FROM items where id ='.$list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
if ($list == FALSE) {
header("location: index.php");
}
}
if(isset($_GET['previous'])){
$list_id = intval(($_GET['id']));
try {
$sql = "SELECT * from items WHERE id < '$list_id' ORDER by id DESC LIMIT 1";
$query = $pdo->prepare($sql);
$query->execute();
}
catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
}
html:
?php
if(isset($list)) {
?>
<div class="article-wrapper"><p class="specific-article-header"><?php echo $list['title'];?></p>
<p><img src="<?php echo $list['photo']; ?>" class="article-images"> </p>
<p class="article-text"><?php echo $list['description']; ?></p>
<a href="?previous" style='color:white;'>Previus</button>
</div>
<?php
}
?>
Perhaps try replace your first query with the following:
$sql = "SELECT * FROM items where id ='$list_id'";

How to assign php while loop into smarty to get required result

i want to show featured ads in slider problem is there is two table have to use to get ads. First one is class_ads that have all info of ad like title, description, price etc and second table is class_ads_pictures from where i have to match id of each ad and get the name of folder and picture name from table ....
Please review my poor logic and teach me
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($tmp = mysql_fetch_array($featured_ads)) {
$ad_id = $temp['id'];
$ad_title = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select from class_ads_pictures where ad_id=$ad_id order by order_no");
$img = mysql_fetch_array($featured_ads_images);
$img_id = $img['id'];
$ad_img = $img['picture'];
$img_folder = $img['folder'];
// Problem is how to assign and what have to assign, to get display in html file ...
}
$smarty->assign('what', $what);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
Below code should work. However you should not use mysql but PDO or mysqli because mysql is deprecated.
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
In Smarty you can simple use it:
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}

Categories