Open another page and send data in php? - php

I'm developing a simple online portfolio website for my final project. I have a page to show all design stored in database. This is the template of a item,
+---------------------+
+ +
+ +
+ image +
+ +
+ +
+ +
+---------------------+
+ Title +
+ +
+---------------------+
This is the code that i used to load all items from databse.
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$title = $row['title'];
$image = $row['image'];
$designer = $row['designer'];
$views = $row['views'];
$likes = $row['likes'];
$price = $row['price'];
?>
<div class = "col-md-3 col-sm-6"> <div class = "wow fadeInUp animated portfolio-item" data-wow-duration = "500ms" data-wow-delay = "600ms">
<div class = "img-wrapper">
<img src = "<?php echo $image; ?>" class = "img-responsive" alt = "" ></div>
<div class = "portfolio-item-text"><h4><a href = "preview.php?title">
<?php echo $title; ?>
</a>
</h4>
<p id = "portfolio-subtitle">
by <?php echo $designer; ?>
</p>
<div class="row" style="margin-top:10px;">
<div class="col-md-3" style="float:left;">
<h6><?php echo $price; ?></h6>
</div>
<div class="col-md-7" style="float:right;">
<div class="portfolio-icons">
<ul class="list-inline">
<li><i class="fa fa-eye"></i> <?php echo $views; ?></li>
<li><i class="fa fa-thumbs-o-up"></i> <?php echo $likes ?></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
I need to open another page to preview each design. So i used $_SESSION to bring data to my preview page inside the while loop.
$_SESSION['id'] = $row['design_id'];
$_SESSION['title'] = $row['title'];
but it only preview the last item. How can i solve this.

You need to change the link
<?php echo $title; ?>
to something like this:
<?php echo $title; ?>
Then you can get in the preview.php file the id with $_GET['id']. But don't forget to check if it is a number before usage. (is_numeric() or int_val())

Related

How to load more comments without refreshing the browser

Am working on comment system, but am stuck here, after loading my PHP on my browser, it shows me exactly 2 comments that I want to see, when I click the link it fetches the other 2 comments as I programmed it on jQuery, but after that the button disappears and I cant load more comments.
Please help!
Here is my code,
<script>
$(document).ready(function() {
var commentCount = 2;
$("button").click(function() {
commentCount = commentCount + 2;
$("#comments").load("2.php", {
commentNewCount: commentCount
});
});
});
</script>
<body>
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
<button>More comments</button>
</body>
and this down here is my load-coments.php (i decided to call it 2.php)
<?php
include 'include/dbconnect.php';
$commentNewCount = $_POST['commentNewCount'];
$sql = "SELECT * FROM comments ORDER BY id LIMIT $commentNewCount";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
What do you mean with "button disappears"?. The load function of jquery replaces the html with the recieved html so maybe your button is in the div you're replacing. It's hard to debug your code because of the lack of indents.

Show result of query with count

I'm making a query with a count statement and I wanna show the result
something like this
I have a file with name functions.php where I make the query in a (obviously) function.
those are my functions:
function conection($bd_config){
try {
$conection = new PDO('mysql:host=localhost;dbname='.$bd_config['database'], $bd_config['user'], $bd_config['pass']);
return $conection;
} catch (PDOException $e) {
return false;
}
}
function checksession(){
if (!isset($_SESSION['user'])) {
header('Location: ' . ROUTE. '/login.php');
}
}
#$us = $_SESSION['USER'][0];
function notification_count($conexion, $us){
$count = $conection->prepare('SELECT * FROM postulate WHERE du = $us AND seen = 0');
$count->execute();
$total = $count->rowCount();
$total = $count->fetchColumn();
return $total;
}
I have another file with name lateral.php where I "call" the function and the view of latera.view.php
lateral.php:
<?php session_start();
require 'extras/config.php';
include 'functions.php';
checksession();
$conection = conection($bd_config);
$quantity = notification_count($conection, $us);
require "views/lateral.php";
lateral.view.php:
<div class="block-section text-center ">
<img src="images/<?php echo($_SESSION['user'][14]) ?>" class="img-rounded" alt="">
<div class="white-space-20"></div>
<h4><?php echo($_SESSION['user'][3]) ?></h4>
<div class="white-space-20"></div>
<ul class="list-unstyled">
<li> Messages </li>
<li> Notifications<?php here is where I want the total of the query ?></li>
<li> Change password</li>
</ul>
<div class="white-space-20"></div>
</div>
</div>
<div class="col-md-9 col-sm-9">
<!-- Block side right -->
in the file of functions I need to make my query like this:
function notifications_count($conection, $us){
$count= $conection->prepare("SELECT COUNT(*) FROM postul WHERE du = $us AND seen = 0");
$count->execute();
$number_notu = $count->fetchAll();
return $number_notu;
}
then in the file of notifications.php where onlye I print code and make a require of the view(HTML) I need to put something like this:
<?php session_start();
require 'extra/config.php';
require 'functions.php';
$conection = conection($bd_config);
if (!$conection) {
header('Location: error.php');
}
$postul= get_people($notu_config['post_notu'], $conection, $us);
if (!$postul) {
$vacio = "<li>The aren't any notifications</li>";
}
$total_notu = notu_count($conection, $us);
require "views/notifications.php";
and in the view of notifications where I print only HTML I need to make a foreach:
<div class="block-section text-center ">
<img src="images/<?php echo($_SESSION['user'][14]) ?>" class="img-rounded" alt="">
<div class="white-space-20"></div>
<h4><?php echo($_SESSION['user'][3]) ?></h4>
<div class="white-space-20"></div>
<ul class="list-unstyled">
<li> Messages</li>
<?php foreach ($total_notu as $notu) : ?>
<li> Notificaciones <span class="badge "><?php echo "$notu[0]"; ?></span></li>
<?php endforeach; ?>
<li> Change password</li>
</ul>
<div class="white-space-20"></div>
</div>
</div>
<div class="col-md-9 col-sm-9">
<!-- Block side right -->
I hope I can help someone ho are on the same troble like me :)

How do I position nested comments in PHP/CSS?

I have comments stored in a database with the post id that the comment is on, the comments id, the comments parent id, and when it was created, the content, etc.. Now I've got a query that will select all of the comments for a particular post and loop through each one displaying it. However, how can I 'indent' the comments to show that they are a child of another comment?
My current idea (somewhat pseudo-code) was to do:
get comments
for each comments as comment {
if comment has parent
margin += 16;
style = margin-left: margin
else
margin = 0
<div class="post" style="<?php echo $style; ?>">
...
</div>
}
However, this doesn't seem to be working as some of the replies are not positioned correctly, or sometimes don't even position under the parent comment. Am I doing something wrong here, is there a better way to do this?
Actual Code (it's very ugly):
<?php
$comments = $backend->getCommentsFromPost($post_id);
$level = 0;
$margin = 0;
foreach ($comments as $comment) {
$author = $backend->getUserById($comment['comment_author_id']);
$author_name = $author[0]['user_name'];
$author_id_62 = $backend->to62($author[0]['user_id']);
$when = $backend->getTimePosted($comment['comment_created']);
$comment_karma = 2;
$style = "";
if ($comment['comment_parent'] != NULL) {
$margin = $margin + 60;
$style = "style=\"margin-left: " . $margin . "px;\"";
}
else {
$margin = 0;
}
?>
<div <?php echo $style; ?> class="post">
<div class="row">
<div class="col-md-8">
<div class="comment">
<p class="comment-data">
<?php echo $author_name; ?> | <?php echo $comment_karma; ?> points | <?php echo $when; ?></p>
<?php echo $comment['comment_content']; ?>
<p class="comment-footer">
<a class='cbtn btn-primary' title='upvote' href='#'><span class='fa fa-arrow-up'></span></a>
<a class='cbtn btn-primary' title='downvote' href='#'><span class='fa fa-arrow-down'></span></a>
<?php
if ($backend->isLoggedIn()) {
$user_id = $_SESSION['user_id'];
$comment_id = $backend->to62($comment['comment_id']); ?>
<a class='cbtn btn-primary' title='post reply' href='comment-reply.php?comment=<?php echo $comment_id; ?>'><span class='fa fa-reply'></span></a>
<?php
if ($user_id == $author[0]['user_id'] || $backend->isUserNotPeasant($user_id)) {
?>
<a class='cbtn btn-danger' title='delete comment' href="delete-comment.php?id=<?php echo $comment_id; ?>"><span class='fa fa-trash-o'></span></a>
<?php
}
}
?>
</p>
</div>
</div>
</div>
</div>
<?php } ?>
Cheers
It should be simply:
<style>
.post {margin-left: 0;}
.post div {margin-left: 16px;}
</style>
<div class="post">
POST HERE
<div>
reply
<div>
2nd reply
<div>
4th level
</div>
</div>
</div>
<div>
reply 2
</div>
</div>
<div class="post">
another post
</div>
http://jsfiddle.net/4f2wzwne/
You can prepend the comment with any number of   as desired. One per level seems good. Track the level depth of the comment, which you can get from your query, and translate that to number of   to prepend with.
First get comments without parent, then loop in them and get child comments. That will fix your problem I think.

Trouble with html link tag and image

Got some trouble when i tried to use an url to image.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$query = mysql_query($sql);
if ($query > 0){
?>
<div class="container">
<div class="description-plate">
<?php
while
($row = mysql_fetch_array($query)){
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<!--div class="caption-btm">
<p style="margin-left:6px; margin-top:175px;">Start Airing From:</p>
<h5 style="margin-left:10px;"><?php echo $start; ?></h5>
<p style="margin-left:6px;">Airing Schedule:</p>
<h5 style="margin-left:10px;"><?php echo $schedule; ?></h5>
</div-->
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
So when i tried to call the image using php, the tag only appear on the last image. What i'm trying to do is having the tag on every images. Would appreciate any help, thanks :)
Right now you are going through the loop (not sure why you are using while) and each time creating
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
What you want to do is build up an html string on each pass appending the next image tag, something more like
...
$myimages = '';
while // skipped details
$myimages .= ' <div class="thumbnail-fluid">
<a href=". $row['image'] . '>
<div id="og-plate">
<div><img src="admin/' . $row['image'] . '></div>'
. '</div>
</a>
</div>';
}
Its appear last image because ORDER BY id and the condition status = 'On Going' can return one image. Your html structure should be like this.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$result = mysql_query($sql);
$n = mysql_num_rows($result);
if ($n > 0) {
?>
<div class="container">
<div class="description-plate">
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
</div>
</a>
</div>
<?php } ?>
</div>
</div>
<?php } ?>

Pagination in PHP blog

I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.

Categories