kennedy32
V2EX  ›  PHP

这么写的人是个什么心态。。。

  •  
  •   kennedy32 · Aug 31, 2013 · 5845 views
    This topic created in 4661 days ago, the information mentioned may be changed or developed.
    这么写的人是个什么心态。。。 !$this->IsURLCurrentPage($url)和active=true,看得好纠结。。。

    public function DisplayMenu($buttons)
    {
    echo "<table width=\"100%\" bgcolor=\"white\"
    cellpadding=\"4\" cellspacing=\"4\">\n";
    echo "<tr>\n";

    //calculate button size
    $width = 100/count($buttons);

    while (list($name, $url) = each($buttons)) {
    $this -> DisplayButton($width, $name, $url,
    !$this->IsURLCurrentPage($url));
    }
    echo "</tr>\n";
    echo "</table>\n";
    }

    public function IsURLCurrentPage($url)
    {
    if(strpos($_SERVER['PHP_SELF'], $url )==false)
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    public function
    DisplayButton($width,$name,$url,$active = true)
    {
    if ($active) {
    echo "<td width = \"".$width."%\">
    <a href=\"".$url."\">
    <img src=\"s-logo.gif\" alt=\"".$name."\" border=\"0\" /></a>
    <a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
    </td>";
    } else {
    echo "<td width=\"".$width."%\">
    <img src=\"side-logo.gif\">
    <span class=\"menu\">".$name."</span>
    </td>";
    }
    }
    Supplement 1  ·  Sep 1, 2013
    这是PHP and MySQL web development第四版的例子,马上就要出版第五版了
    13 replies    1970-01-01 08:00:00 +08:00
    mantianyu
        1
    mantianyu  
       Aug 31, 2013
    珍爱生命,远离框架
    yakczh
        2
    yakczh  
       Aug 31, 2013
    还停留在服务端拼字符串的阶段,现在已经是angularjs,react的时代
    raincious
        3
    raincious  
       Aug 31, 2013
    这代码应该很老了吧?

    while (list($name, $url) = each($buttons));

    怎么不写成foreach($buttons AS $key => $val)?

    看似目的就是为了平均的产生按钮。

    但是,这样组织代码也太悲剧了。上Smarty吧至少。。。
    Golevka
        4
    Golevka  
       Sep 1, 2013
    我感觉那一坨字符串拼接才是最反人类的
    yopming
        5
    yopming  
       Sep 1, 2013
    拼接HTML代码是很不友好的,不使用模板引擎非常不利于协作分离
    darasion
        6
    darasion  
       Sep 1, 2013   ❤️ 1
    看 php 的书,都不如看手册。
    hanf
        7
    hanf  
       Sep 3, 2013
    php程序员真悲催啊~~
    zhouitpro
        8
    zhouitpro  
       Sep 3, 2013
    应该是个老程序员
    msg7086
        9
    msg7086  
       Sep 3, 2013
    IsURLCurrentPage里的代码简直是鬼斧神工
    kennedy32
        10
    kennedy32  
    OP
       Sep 3, 2013
    @msg7086 求解释?

    @zhouitpro 老书
    sharpnk
        11
    sharpnk  
       Sep 3, 2013
    @kennedy32

    return strpos($_SERVER['PHP_SELF'], $url );
    msg7086
        12
    msg7086  
       Sep 3, 2013
    @sharpnk 别忘记strpos与===的著名大坑

    @kennedy32 呃……这应该不用解释了吧?
    gaody
        13
    gaody  
       Sep 4, 2013
    @msg7086 返回0 啊啊啊啊啊,我疯了,不要管我
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2503 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 15:53 · PVG 23:53 · LAX 08:53 · JFK 11:53
    ♥ Do have faith in what you're doing.