您的位置:首页 > 编程语言 > PHP开发

php 新闻简单建表,查询,下拉分类实例

2015-04-05 01:35 260 查看
(一)创建新闻发布系统,表名为message有如下字段 (3分)

  id 文章id

  title 文章标题

  content 文章内容

  category_id 文章分类id

hits 点击量

答:CREATE TABLE 'message'(

'id' int(10) NOT NULL auto_increment,

'title' varchar(200) default NULL,

'content' text,

'category_id' int(10) NOT NULL,

'hits' int(20),

PRIMARY KEY('id');

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

(二)同样上述新闻发布系统:表comment记录用户回复内容,字段如下 (4分)

  comment_id 回复id

  id 文章id,关联message表中的id

  comment_content 回复内容

  现通过查询数据库需要得到以下格式的文章标题列表,并按照回复数量排序,回复最高的排在最前面

  文章id 文章标题 点击量 回复数量

  用一个SQL语句完成上述查询,如果文章没有回复则回复数量显示为0

答:SELECT message.id id,message.title title,IF(message.`hits` IS NULL,0,message.`hits`) hits,

IF(comment.`id` is NULL,0,count(*)) number FROM message LEFT JOIN

comment ON message.id=comment.id GROUP BY message.`id`;

  (三)上述内容管理系统,表category保存分类信息,字段如下 (3分)

  category_id int(4) not null auto_increment;

  categroy_name varchar(40) not null;

  用户输入文章时,通过选择下拉菜单选定文章分类

  写出如何实现这个下拉菜单

答:function categoryList()

{

$result=mysql_query("select category_id,categroy_name from category")

or die("Invalid query: " . mysql_error());

print("<select name='category' value=''>\n");

while($rowArray=mysql_fetch_array($result))

{

print("<option value='".$rowArray['category_id']."'>".$rowArray['categroy_name']."</option>\n");

}

print("</select>");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: