Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-显示更多文章功能反馈_Php_Mysql_Database_Forms_Performance - Fatal编程技术网

PHP-显示更多文章功能反馈

PHP-显示更多文章功能反馈,php,mysql,database,forms,performance,Php,Mysql,Database,Forms,Performance,目前,我有一个articles页面,它读取数据库表中的所有文章,并打印页面上的每一篇文章,从最新的开始 然而,我正在制作的系统将被使用很长一段时间(一个小公司的学生项目),并且会有很多文章。我不希望用户或服务器必须同时加载网页上的所有文章。我有办法做到这一点,但我想先让其他人来实施这个想法 从概念上讲,我想做的是在页面上加载一些文章。在底部,会有一个“显示更多”按钮或链接,可以从数据库中读取和显示更多的文章(希望不会重新加载页面,但我非常怀疑这是否可行) 实际上,它可能是这样的:使用一个表单,在

目前,我有一个articles页面,它读取数据库表中的所有文章,并打印页面上的每一篇文章,从最新的开始

然而,我正在制作的系统将被使用很长一段时间(一个小公司的学生项目),并且会有很多文章。我不希望用户或服务器必须同时加载网页上的所有文章。我有办法做到这一点,但我想先让其他人来实施这个想法

从概念上讲,我想做的是在页面上加载一些文章。在底部,会有一个“显示更多”按钮或链接,可以从数据库中读取和显示更多的文章(希望不会重新加载页面,但我非常怀疑这是否可行)

实际上,它可能是这样的:使用一个表单,在隐藏字段中存储所显示文章的数量,在提交“Show more”时重新加载页面,并使用$\u POST和while循环显示所存储的数量+更多的文章。下面是一个简化的例子

<form method="post" action="articlePage.php">
   <?php
   if(isset($_POST["articleCount"])){
      $articleCount = $_POST["articleCount"] + 5;
   }
   else{
      $articleCount = 5
   }

   //Assuming we open the connect, execute the query and then close the connection
   $count = 0;
   while($result = mysqli_fetch_array($selectNews) && $count <= $articleCount){
      echo $result["articleName"] . "<br/>";
      count = count + 1;
   }

   echo "<input type='hidden' value='" . $articleCount . '" name='articleCount'/>";
   ?>
   <input type="submit" value="Show more"/>
</form>


您可以将PHP代码放在一个单独的文件中,并使用AJAX调用它。你可以这样做:

function getArticleCount(){
   if(isset($_POST["articleCount"])){
      $articleCount = $_POST["articleCount"] + 5;
   }
   else{
      $articleCount = 5
   }

   //Assuming we open the connect, execute the query and then close the connection
   $count = 0;
   while($result = mysqli_fetch_array($selectNews) && $count <= $articleCount){
      echo $result["articleName"] . "<br/>";
      count = count + 1;
   }

   echo "<input type='hidden' value='" . $articleCount . '" name='articleCount'/>";
}

getArticleCount();
函数getArticleCount(){ 如果(isset($_POST[“articleCount”])){ $articleCount=$_POST[“articleCount”]+5; } 否则{ $articleCount=5 } //假设我们打开连接,执行查询,然后关闭连接 $count=0;
while($result=mysqli\u fetch\u array($selectNews)&$count你听说过ajax吗?我没有听说,但是有人发布了一个包含ajax的答案,所以我现在正在查找它!是的,ajax就是答案,谢谢!谢谢,看起来很有趣。我将试着了解它(第一次看到ajax)然后返回给你,看起来它在不重新加载页面的情况下工作得很好(还是我错了?)但是,AJAX部分看起来像JavaScript,我听说使用数据库是不安全的。我猜这不完全是JS,这个谣言不适用?是的AJAX是JavaScript。你的JavaScript不会访问你的数据库,PHP会。你在PHP代码中保护你的数据库。太棒了,这就是为什么我喜欢编程XD谢谢!