嗯,我试图使一个网站,你可以看到,我有困难与"LOAD MORE"按钮,什么我在视觉是,每当有人点击这个按钮,它会通过最好的php代码,告诉数据库加载在5个以上的帖子。
show_posts.php
<?php
$query = $con->query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5");
if( isset( $_POST['load'] ) ){
//code goes here
}
while($result = mysqli_fetch_assoc($query)){
// if video is empty then echo this line
if($result["video"] == ""){
$account_assoc = $result["account_assoc"];
$result2 = mysqli_fetch_assoc($con->query("SELECT * FROM `Accounts` WHERE username='$account_assoc' OR email='$account_assoc'"));
if($result2["username"] == ""){
$identifier = $result2["firstname"];
}else{
$identifier = $result2["username"];
}
if($result2['image'] == ""){
$image = "http://jnvbaghmara.nic.in/images/staff/Blank.png";
}else{
$image = 'data:image/jpeg;base64,'.base64_encode($result2['image']);
}
if($result['dislikes'] > 1){
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislikes";
}else{
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislike";
}
if($result['likes'] > 1){
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> likes";
}else{
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> like";
}
echo '<li>
<h4><img src="'.$image.'"/> <label>'.$identifier.'</label></h4>
<div class="all-content">
<label style="color: #777;"> '.$like.' </label><label style="color: #777;"> | '.$dislike.'</label><label style="color: #777;"> | 2 comment</label>
<p>'.$result["text"].'</p>
</div></li>';
}
// if video is not empty then echo this line
elseif($result["video"] != ""){
$account_assoc = $result["account_assoc"];
$result2 = mysqli_fetch_assoc($con->query("SELECT * FROM `Accounts` WHERE username='$account_assoc' OR email='$account_assoc'"));
if($result2["username"] == ""){
$identifier = $result2["firstname"];
}else{
$identifier = $result2["username"];
}
if($result2['image'] == ""){
$image = "http://jnvbaghmara.nic.in/images/staff/Blank.png";
}else{
$image = 'data:image/jpeg;base64,'.base64_encode($result2['image']);
}
if($result['dislikes'] > 1){
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislikes";
}else{
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislike";
}
if($result['likes'] > 1){
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> likes";
}else{
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> like";
}
echo '<li>
<h4><img src="'.$image.'"/> <label>'.$identifier.'</label></h4>
<div class="all-content">
<iframe width="90%" height="90%" src="https://www.youtube.com/embed/'.$result["video"].'" frameborder="0" allowfullscreen></iframe>
<p>'.$result["text"].'</p>
<label style="color: #777;"> '.$like.' </label><label style="color: #777;"> | '.$dislike.'</label><label style="color: #777;"> | 2 comment</label>
</div>
</li>';
}
}
echo '<li>
<div class="all-content">
<form method="post" action="#">
<input type="submit" name="load" value="LOAD MORE..." class="load_more_button"/>
</form>
</div>
</li>';
?>
profile.php
<?php include('include/header.php'); ?>
<?php
$error = "";
if(isset($_POST['post'])){
$video = substr($_POST["video"], 17);
$text = $_POST['paragraph'];
$account_assoc = "";
if($_SESSION['username'] != ""){
$account_assoc = $_SESSION['username'];
}elseif($_SESSION['email'] != ""){
$account_assoc = $_SESSION['email'];
}
$con->query("INSERT INTO `posts` (`id`, `image`, `video`, `text`, `account_assoc`, `likes`, `dislikes`) VALUES ('', '$image', '$video', '$text', '$account_assoc', '0', '0')");
}
?>
<div class="content">
<div class="actual_content">
<div class="right-content">
<div style="padding: 10px;">
<div id="post">
<h3>Latest News</h3>
<ul class="inner-post">
<?php
$sql = $con->query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 5");
function myTruncate($string, $limit, $break=" ", $pad="..."){
if(strlen($string) <= $limit){
return $string;
}
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}
while($result = mysqli_fetch_array($sql)){
$header = $result['header'];
$statement = $result['statement'];
$image = $result['image'];
$shortdesc = myTruncate($statement, 300);
echo '<li><h5><img src="#" /><label>'.$header.'</label></h5><p>'.$shortdesc.'</p></li>';
}
?>
</ul>
</div>
</div>
</div>
<div class="left-content">
<div class="post-holder" width="70%" style="padding: 10px;">
<form class="post" method="post" action="#">
<textarea name="paragraph" placeholder="Hey there! Share a game highlight with a photo or video... <?php echo $error; ?>" maxlength="250"></textarea>
<input type="submit" name="post" class="button_post" value="POST" />
<label class="video"><input type="text" name="video" placeholder="Use a YouTube link in order to post video..."/></label><br />
</form>
</div>
<div class="new-posts" style="padding: 10px;">
<ul>
<?php include('show_posts.php')?>
</ul>
</div>
</div>
</div>
</div>
<div class="footer">
</div>
</div>
</body>
</html>
也如果有任何方式使我的代码更简单,那么我是开放的建议。
*我想要的代码是php,因为我了解php比我做js, jquery等
$postsNum = $_GET['num'];
$query = $con->query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT $postsNum");
带有链接的LOAD MORE按钮:YOURURL?num=n