gpt4 book ai didi

php - 有没有一种方法可以将 "load more"代码实现到我的原始代码中?

转载 作者:搜寻专家 更新时间:2023-10-31 21:28:38 26 4
gpt4 key购买 nike

好吧,我正在尝试创建一个网站,如您所见,但我在使用“加载更多”按钮时遇到了困难,我的设想是,每当有人单击此按钮时,它最好会通过 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>';
?>

配置文件.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,因为我比 js、jquery 等更了解 php。

最佳答案

$postsNum = $_GET['num'];

$query = $con->query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT $postsNum");

带有链接的“加载更多”按钮:YOURURL?num=n

n 可以通过你的 php 代码改变。

关于php - 有没有一种方法可以将 "load more"代码实现到我的原始代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com