Categories

Posts Tagged ‘php’

wordpress显示最新评论代码

很多使用wordpress 的博客会在首页或者sidebar显示网友的最新评论, 其实这样的功能实现起来很简单,主要思路是从数据库中的评论数据表中取得最晚发布的N条记录即可。 代码在下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <?php 
	global $wpdb; 
 
	$sql = &quot;SELECT DISTINCT ID, post_title, post_password, comment_ID, 
    comment_post_ID, comment_author, comment_date_gmt, comment_approved, 
    comment_type,comment_author_url, 
     SUBSTRING(comment_content,1,30) AS com_excerpt 
    FROM $wpdb-&gt;comments 
    LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID = 
    $wpdb-&gt;posts.ID) 
   WHERE comment_approved = '1' AND comment_type = '' AND 
   post_password = '' 
   ORDER BY comment_date_gmt DESC 
   LIMIT 10&quot;; 
 
   $comments = $wpdb-&gt;get_results($sql); 
   $output = $pre_HTML; 
   $output .= &quot;\n&lt;ul&gt;&quot;; 
   foreach ($comments as $comment) { 
	$output .= &quot;\n&lt;li&gt;&quot;. &quot;&lt;p&gt;&lt;a href=\&quot;&quot; . get_permalink($comment-&gt;ID) . 
		&quot;#comment-&quot; . $comment-&gt;comment_ID . &quot;\&quot; title=\&quot;on &quot; . 
		$comment-&gt;post_title . &quot;\&quot;&gt;&quot; . strip_tags($comment-&gt;com_excerpt) 
		.'&lt;/a&gt;('.strip_tags($comment-&gt;comment_author). ')&lt;/p&gt;&lt;/li&gt;'; 
   } 
   $output .= &quot;\n&lt;/ul&gt;&quot;; 
   $output .= $post_HTML; 
   echo $output;
   ?>

php中include、require以及include_once、require_once的区别

PHP文件中如果想要引用另外一个源文件的变量、函数、语句或者类都需要用到include, require以及include_once,require_once这四种方式, 那它们之间有区别吗? 答案是肯定的。

首先 include、 require 都是用于在一个源文件中包含其它源文件中的代码的, 在PHP 4.3.5及以前的版本二者区别在于require如果引用了一个不存在的文件会使整个php程序退出并报错,而include则只会报错但并不终止整个进程,也就是说如果include了一个不存在的问题,只会给出警告信息但仍然会继续运行。 这一问题在PHP 4.3.5以后的版本中得以统一, include和require如果导入一个不存在的源文件都将终止进程并报错。 至于include_once 和 requre_once就是只导入被包含的文件一次, 这样可以避免函数重复申明的错误出现。 比如:
hello.php


	function showmessage(){
		echo 'hello, word';
	}

include.php


	include 'hello.php';

message.php


	include 'include.php';
	// some code here
	include 'hello.php';

这时message.php 会出错,因为showmessage()函数重复定义,如果用include_once 替换include 就不会出此错误。