V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kennedy32
V2EX  ›  PHP

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

  •  
  •   kennedy32 · 2013-08-31 21:27:44 +08:00 · 5199 次点击
    这是一个创建于 3891 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这么写的人是个什么心态。。。 !$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>";
    }
    }
    第 1 条附言  ·  2013-09-01 02:55:09 +08:00
    这是PHP and MySQL web development第四版的例子,马上就要出版第五版了
    13 条回复    1970-01-01 08:00:00 +08:00
    mantianyu
        1
    mantianyu  
       2013-08-31 21:36:24 +08:00
    珍爱生命,远离框架
    yakczh
        2
    yakczh  
       2013-08-31 21:52:21 +08:00
    还停留在服务端拼字符串的阶段,现在已经是angularjs,react的时代
    raincious
        3
    raincious  
       2013-08-31 21:54:02 +08:00
    这代码应该很老了吧?

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

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

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

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

    @zhouitpro 老书
    sharpnk
        11
    sharpnk  
       2013-09-03 23:37:14 +08:00
    @kennedy32

    return strpos($_SERVER['PHP_SELF'], $url );
    msg7086
        12
    msg7086  
       2013-09-03 23:53:21 +08:00
    @sharpnk 别忘记strpos与===的著名大坑

    @kennedy32 呃……这应该不用解释了吧?
    gaody
        13
    gaody  
       2013-09-04 15:00:32 +08:00
    @msg7086 返回0 啊啊啊啊啊,我疯了,不要管我
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1484 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:32 · PVG 01:32 · LAX 10:32 · JFK 13:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.