当前位置导航:炫浪网>>网络学院>>网页制作>>PHP教程

使用PHP制作新闻系统的思路

我们可以用新闻系统来存储新闻,我们可以对新闻进行添加、删除等操作,这样减少了大家的工作量,为什么不实验一下。

首先,建立一个表。
create table news (
n_id int(255) not null auto_increment,
n_date datetime not null,
news text not null,
primary key(n_id)
);

第二步,设置你的登陆信息
$database_user_name="root";                         
$database_password="";                                                 
$database_name="news";                              
$time_offset="0";

第三步,让我们把后面程序用到的东西作成函数,节省篇幅啊!

function connect_db()
    {
    // connects to the database
     
    global $database_user_name, $database_password;
    $db=mysql_connect("localhost",$database_user_name,$database_password);
         
    return $db;
     
    }


function db_name()
    {
    // returns the name of the database
     
    global $database_name;
    $db_name=$database_name;
     
    return $db_name;
     
    }
     
     
     
function get_now()
    {
    // gets current date and time
     
    $db=connect_db();
    $db_name=db_name();
    mysql_select_db($db_name,$db);
    $sql="select now() as now";
    $result=mysql_query($sql,$db);
    $myrow=mysql_fetch_array($result);
    $now=$myrow["now"];
     
    return $now;
     
    }
第四步,让我们考虑如何将新闻显示出来


//上面已经定义的函数库...
//表格的定义......
<center><FONT SIZE=5 FACE="'georgia',verdana,Arial,Lucida Sans,Gill Sans">$title ";
if ($admin==1)
    {
     
    echo " Admin ";
    }  
     
echo " - News</font>
</td></tr>
";


$db=connect_db();
$db_name=db_name();
mysql_select_db($db_name,$db);

if ($show_news_sub)
    {
     $show_news = $show_news_sub;
    }
     

if (!ereg("([0-9]{".strlen($show_news)."})",$show_news))
    {
    $show_news="ALL";
    }

if (strtoupper($show_news)=="ALL")
    {
     
    $sql="select date_format(n_date,'%m/%d/%Y') as n_date, date_format(n_date,'%H:%i') as n_time ,news , n_date as date from news order by date desc";
     
    }
else  
    {
     
    $link="<a href=$REQUEST_URL?show_news_sub=ALL><FONT SIZE=-2  FACE="'georgia',verdana,Arial,Lucida Sans,Gill Sans">All news</font></a>";
     
     
    $sql="select date_format(n_date,'%m/%d/%Y') as n_date, date_format(n_date,'%H:%i') as n_time , news , n_date as date from news order by n_date desc limit $show_news";
    }
$result=mysql_query($sql,$db);
$num_rows=mysql_num_rows($result);
if($num_rows!=0)
    {
echo "


";

    while($myrow=mysql_fetch_array($result))
        {
        $date=$myrow["n_date"];     
        $time=$myrow["n_time"];         
        $news=$myrow["news"];
        $n_id=$myrow["n_id"];
        if (strtoupper($show_date)=="Y" && strtoupper($show_time)=="Y")
            {
            $show=$date." ".$time;
             
            }
        else if (strtoupper($show_date)=="Y" && strtoupper($show_time)!="Y")     
            {
            $show=$date;
             
            }
        if (strtoupper($show_date)!="Y" && strtoupper($show_time)=="Y")
            {
            $show=$time;
             
            }     
             
        $cleannews=(nl2br($news));

  
<TD align=left><FONT SIZE=-2  FACE="'verdana',Arial,Lucida Sans,Gill Sans">$show<br>$cleannews</font</TD>



";
}
echo "     

";

         


    }
if ($admin==1)
{
echo "
<center><a href=admin/index.php><FONT SIZE=-2 FACE="'verdana',Arial,Lucida Sans,Gill Sans">返回</font></a></center>
";
}

if ($link)
    {
    echo "<center><br><br>$link</center>";
     
    }
说明:省略了格式方面的标签,这样看起来思路清晰些,你可要加上啊


【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】    
上一篇:截获网站</a> <font style="color:red">下一篇:</font><a href="/html/75319.html" target="_self">怎样在PHP中通过ADO调用Asscess数据库和COM程序</a></div> <div class="clearbox"></div> <div class="lm_xxlb_title"> <div class="title">相关内容</div> <div class="more"> <a href="/list_218/index.html">[更多] </a> </div> </div> <div class="flm_xxlb_main"> <div class="clearbox"></div> <ul> <li> <div class="tydlmxxlb_title"><a href="/html/76412.html" target="_blank">详解PHP异常机制的流程及原理</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76411.html" target="_blank">Linux下安装php-soap通过重新编译php过程</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76410.html" target="_blank">php中单引号和双引号的区别</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76409.html" target="_blank">php生成xml和转换[源码]</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76408.html" target="_blank">smarty局部缓存技术[源码分析]</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76407.html" target="_blank">PHP绘制心电形状曲线统计图表[源码]</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76406.html" target="_blank">PHP服务器端多进程编程实战</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76405.html" target="_blank">数据源架构模式之活动记录</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76404.html" target="_blank">通过PATH_INFO的方式来实现搜索引擎友好 隐藏index.…</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76403.html" target="_blank">基础补习:PHP易混淆函数的区分方法及意义</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76402.html" target="_blank">php5.3以下使用fileinfo获得文件的mime类型</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76401.html" target="_blank">PHP+MySQL+jQuery打造自己的微博程序</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76400.html" target="_blank">php中使用exec,system等函数调用系统命令</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76399.html" target="_blank">常见的PHP缓存技术分析[概念版]</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76398.html" target="_blank">又道PHP5中的魔术方法</a></div> </li> <li> <div class="tydlmxxlb_title"><a href="/html/76397.html" target="_blank">什么是PHP中可变变量以及如何应用</a></div> </li> </ul> <div class="clearbox"></div> </div> </div> <!-- 主体内容结束 --> </div> <div class="main_right_nr"> <div class="main_right_tit">赞助商链接</div> <div class="ad_lmsct"> <script type="text/javascript" src='/IAA/200902/12.js'></script> </div> </div> <div class="clearbox"></div> </div> <span style="display:none;"> <script language="JavaScript" type="text/JavaScript" src="/Common/GetHits.aspx?id=75318"></script> </span> <div class="main"> <div id="bottom_all"> <div id="foot"> <div id="copyright"> 【注意】炫浪网 版权所有,未经书面授权禁止模仿风格或建立镜像网站.如有违反,追究法律责任.<br /> 【声明】本网站部分内容属社区网友发布,本网站仅提供网友交流平台,但有权在本网站范围内引用、发布、<br /> 转载来自社区网友发布的内容。网友发布内容纯属个人行为,与本网站立场无关。本网站对于网友发布的<br /> 内容所引发的版权、署名权的异议及纠纷,不承担任何责任。 粤ICP备<a href="http://www.miibeian.gov.cn">11064832</a>号 公安备案编号:4453023010550 <script src="http://s96.cnzz.com/stat.php?id=372893&web_id=372893" language="JavaScript"></script> </div> </div> </div> </div> </body> </html>