我刚刚学习了一些简单的php和数据库知识,现在想做一个供求信息发布系统,有这样一些需求:首页导航栏里是信息分类链接,点击查看某一类的信息;另外我还设置了供应信息、求购信息、所有信息的链接,意思是在某一类信息下可以单独查看供应信息或求购信息、所有信息。此外,由于信息条数逐渐增多,还涉及到分页的问题。我上面说了3类链接,我现在大体是这样想的:通过链接把所需信息分类($type)、供应还是求购($gongqiu)以及页数($page)传递到另一个用于显示信息的处理页。感谢您耐心的看到这里,现在说一下我的问题:就是怎样根据这些获得的变量值在数据库里查找呢?我的数据库里有这样几个字段:id,gongying(用于标记是供应信息还是求购信息,值为gong或qiu),type(信息类型),content(内容)等。因为用户可能不去点我上述某些链接,也可能点完一个连接后再点另一个,例如点击“水果”信息后,再点“求购”信息。所以我感觉有点乱,大概写了一下:
<?php
if(($_GET[type])==""){
  $type="*";//*代表所有信息
}else{
$type=$_GET[type];
}
if(($_GET[page])==""){
  $page=1;
}else{
$page=intval($_GET[page]);
}
if(($_GET[gongqiu])==""||($_GET[gongqiu])=="*"){
  $gongqiu="*";//同样,*代表所有信息,如果用户选择“所有信息”,则$gongqiu="*"
}else{
  $gongqiu=$_GET[gongqiu];
}
if($type=="*"&&$gongqiu=="*"){
$sql=mysql_query("select count(*) as total from free where id>0 order by id");}
if($type=="*"&&$gongqiu!="*"){
$sql=mysql_query("select count(*) as total from free where id>0 and gongqiu = '".$gongqiu."' order by id");}
if($type!="*"&&$gongqiu=="*"){
$sql=mysql_query("select count(*) as total from free where id>0 and type = '".$type."' order by id");}
?>
我上面的代码还没有考虑分页的问题,自我感觉代码写的也很笨,请求各位帮我进行一下设计,万分感谢,无论如何,先谢谢您看我啰嗦了这么半天!,谢谢!!!

解决方案 »

  1.   

    把 $_GET 前加个 @, 后面二个 if 改成 elseif , 其它的好像没什么了
      

  2.   

    点击“水果”信息后,再点“求购”信息,没有什么乱的
    最后面二个 if 改成 elseif 会减少判断次数从而提高执行效率
      

  3.   

    <?php
    if( ($_GET['type']) == "" ) {
    $type = "*"; //*代表所有信息
    } else {
    $type = $_GET['type'];
    }if( ($_GET['page']) == "" ) {
    $page = 1;
    } else {
    $page = intval($_GET['page']);
    }if( ($_GET['gongqiu']) == "" || ($_GET['gongqiu']) == "*" ){
    $gongqiu = "*"; //同样,*代表所有信息,如果用户选择“所有信息”,则$gongqiu="*"
    } else {
    $gongqiu = $_GET['gongqiu'];
    }if( $type == "*") {
    if( $gongqiu == "*" )
    {
    $sql = mysql_query("select count(*) as total from free where id>0 order by id");
    } else {
    $sql = mysql_query("select count(*) as total from free where id>0 and gongqiu = '".$gongqiu."' order by id");
    }
    } elseif( $gongqiu=="*" ) {
    $sql = mysql_query("select count(*) as total from free where id>0 and type = '".$type."' order by id");
    }
    ?>
     只有2、3个变量的时候这样取没什么大问题。
      

  4.   

    加个 @ 是防止为空的 那种 情况, 不加是会 报错的 ex:  if(($_GET['type'])==""){  // 如果 无值,可能是会报错的可以  if( @$_GET['type'] == "" ){或者  if( empty( $_GET['type'] ) )
      

  5.   

    if($type=="*"&&$gongqiu=="*"){
    $sql=mysql_query("select count(*) as total from free where id>0 order by id");}
    if($type=="*"&&$gongqiu!="*"){
    $sql=mysql_query("select count(*) as total from free where id>0 and gongqiu = '".$gongqiu."' order by id");}
    if($type!="*"&&$gongqiu=="*"){
    $sql=mysql_query("select count(*) as total from free where id>0 and type = '".$type."' order by id");}你看可不可以改成这样:这样是不是可读性更好些??
    if($type=="*"&&$gongqiu=="*"){
    $sql="select count(*) as total from free where id>0 order by id";}
    if($type=="*"&&$gongqiu!="*"){
    $sql="select count(*) as total from free where id>0 and gongqiu = '".$gongqiu."' order by id";}
    if($type!="*"&&$gongqiu=="*"){
    $sql="select count(*) as total from free where id>0 and type = '".$type."' order by id";}$res=mysql_query($sql);