dongyun6003 2016-01-30 14:33
浏览 41
已采纳

PHP脚本打印“错误”

I am making panels which fills up green "row" from online @ database. Here is css im using to draw everything, does not cause any errors.

<style>
.one {
    width: 1px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
}
.three {
    width: 3px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
}
.five {
    width: 5px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
}
.ten {
    width: 10px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
.thirty {
    width: 30px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
}
.fifty {
    width: 30px;
    height: 12px;
    display:block;
    float:left;
    background-color: green;
}
</style>

And there's PHP code which connects to sql.. checks players and draws a css.

<?php
define('mySQL_hostname', 'IP');  //
define('mySQL_database', 'DB');  //
define('mySQL_username', 'US');  //
define('mySQL_password', 'PW');  //
//connects to mysql
$db_link = mysql_connect( mySQL_hostname, mySQL_username, mySQL_password )
or print( 'SQL_CE' );
//connects to Db
$db_select = mysql_select_db( mySQL_database, $db_link )
or print("SQL_DB");
//selects table
$chars= mysql_query("SELECT `online` FROM `users` where `online`=1");
$rows = mysql_num_rows($chars);
// $rows = counted players... which prints out number.

  if ($rows <= 1) {
    print '<span class="one"></span>';
  }
  else if ($rows >= 50) {
    print '<span class="three"></span>'; 
  }
  else if ($rows >= 150) {
    print '<span class="ten"></span>';
    }
  else if ($rows >= 300) {
    print '<span class="ten"></span>';
    print '<span class="ten"></span>';
    }
  else if ($rows >= 500) {
    print '<span class="thirty"></span>';
    print '<span class="one"></span>';
    }
  else if ($rows >= 700) {
    print '<span class="ten"></span>';
    print '<span class="thirty"></span>';
    print '<span class="three"></span>';
  }
  else if ($rows >= 900) {
    print '<span class="ten"></span>';
    print '<span class="five"></span>';
    print '<span class="thirty"></span>';
    }
  else if ($rows >= 1000) {
    print '<span class="thirty"></span>';
    print '<span class="thirty"></span>';
    print '<span class="three"></span>';
    }
  else if ($rows >= 1200) {
    print '<span class="fifty"></span>';
    print '<span class="ten"></span>';
    print '<span class="ten"></span>';
    print '<span class="five"></span>';
    }
  else if ($rows >= 1400) {
    print '<span class="fifty"></span>';
    print '<span class="thirty"></span>';
    print '<span class="five"></span>';
    }
  else if ($rows >= 1600) {
    print '<span class="fifty"></span>';
    print '<span class="fifty"></span>';
    }
  else if ($rows >= 1800) {
    print '<span class="fifty"></span>';
    print '<span class="fifty"></span>';
    print '<span class="ten"></span>';
    }
  else if ($rows >= 2000) {
    print '<span class="fifty"></span>';
    print '<span class="fifty"></span>';
    print '<span class="thirty"></span>';
    }
  else if ($rows >= 2200) {
    print '<span class="fifty"></span>';
    print '<span class="fifty"></span>';
    print '<span class="thirty"></span>';
    print '<span class="ten"></span>';
    }
  else if ($rows >= 2500) {
        print '<span class="fifty"></span>';
        print '<span class="fifty"></span>';
        print '<span class="fifty"></span>';
    }
    else {
      echo "Script error"; // this place i got printed out.
}   
?>

So the thing i get is... "script error" from bottom of doc. Did i misspelled something or what?

  • 写回答

1条回答 默认 最新

  • duanmu8911 2016-01-30 15:31
    关注

    With the way the code was originally structured as soon as the variable $rows is mor ethan 1 but less than 50 there was no condition to match that - hence skipping to the error. I think it should be something more like:

    if( $rows <= 1 ) {
        echo '<span class="one"></span>';
    } elseif( $rows > 1 && $rows < 50 ) {
        echo '<span class="one"></span>';
    } elseif( $rows >= 50 && $rows < 150 ) {
        echo '<span class="three"></span>'; 
    } elseif( $rows >= 150 && $rows < 300 ) {
        echo '<span class="ten"></span>';
    } elseif( $rows >= 300 && $rows < 500 ) {
        echo '
        <span class="ten"></span>
        <span class="ten"></span>';
    }  elseif( $rows >= 500 && $rows < 700 ) {
        echo '
        <span class="thirty"></span>
        <span class="one"></span>';
    } elseif( $rows >= 700 && $rows < 900 ) {
        echo '
        <span class="ten"></span>
        <span class="thirty"></span>
        <span class="three"></span>';
    } elseif( $rows >= 900 && $rows < 1000 ) {
        echo '
        <span class="ten"></span>
        <span class="five"></span>
        <span class="thirty"></span>';
    } elseif( $rows >= 1000 && $rows < 1200 ) {
        echo '
        <span class="thirty"></span>
        <span class="thirty"></span>
        <span class="three"></span>';
    } elseif( $rows >= 1200 && $rows < 1400 ) {
        echo '
        <span class="fifty"></span>
        <span class="ten"></span>
        <span class="ten"></span>
        <span class="five"></span>';
    } elseif( $rows >= 1400 && $rows < 1600 ) {
        echo '
        <span class="fifty"></span>
        <span class="thirty"></span>
        <span class="five"></span>';
    } elseif( $rows >= 1600 && $rows < 1800 ) {
        echo '
        <span class="fifty"></span>
        <span class="fifty"></span>';
    } elseif( $rows >= 1800 && $rows < 2000 ) {
        echo '
        <span class="fifty"></span>
        <span class="fifty"></span>
        <span class="ten"></span>';
    } elseif( $rows >= 2000 && $rows < 2200 ) {
        echo '
        <span class="fifty"></span>
        <span class="fifty"></span>
        <span class="thirty"></span>';
    } elseif( $rows >= 2200 && $rows < 2500 ) {
        echo '
        <span class="fifty"></span>
        <span class="fifty"></span>
        <span class="thirty"></span>
        <span class="ten"></span>';
    } elseif( $rows >= 2500) {
        echo '
        <span class="fifty"></span>
        <span class="fifty"></span>
        <span class="fifty"></span>';
    } else {
        echo "Script error";
    }
    

    That said, have you considered the meter tag? It would seem to be ideal for the purpose and requires much less markup and all calculations are done internally?

    ie:

    <meter min='0' max='2500' value='{$rows}'>{$rows}</meter>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路