【问题标题】:loop through an anchor id循环通过锚 id
【发布时间】:2013-07-11 13:43:41
【问题描述】:

抱歉,这看起来不对,这是我第一次在这个网站上提问。

我正在使用 html、css 和 php 创建网页。具体来说,我正在尝试使用数据库中的信息在我的页面上创建子导航链接。

这是我的代码:

foreach ($subArr as $sub => $result)
{
    if (mysql_num_rows($result) > 0)
    {
       $resultString .= '<a id="$sub" style="cursor: poimter; color: #0076cf;" href="$sub">'.'  |  '.$sub.'  |  '.'</a>';
    }
 }

$subArr 是一个子类别数组,我希望用户能够单击带有子类别名称的链接,并将它们带到同一页面的该部分。到目前为止,它所做的只是在所有子类别名称下创建一个巨大的链接,而不是创建每个单独的链接。

显然我需要某种循环,但我不确定如何查看 $resultString 来更改锚 id 和 href。

非常感谢任何帮助!

【问题讨论】:

  • 你应该包含print_r($subArr)的结果我们不知道你的数组的结构。

标签: php html loops menu menubar


【解决方案1】:

(属于主题,但很重要)

你有一个错字,应该是:

style="cursor: pointer; ..."

而不是:

style="cursor: poimter; ..."

【讨论】:

    【解决方案2】:

    您的代码中有错误。

    您将变量放在'' 中,php 不会解析以获得正确的结果,您需要将变量放在"" 中。

    foreach ($subArr as $sub => $result)
    {
        if (mysql_num_rows($result) > 0)
        {
           $resultString .= '<a id="'.$sub.'" style="cursor: pointer; color: #0076cf;" href="'.$sub.'">  |  '.$sub.'  |  </a>';
        }
     }
    

    此外,拥有与 href 相同的 ID 看起来很奇怪。

    我注意到您使用的 mysql_* 函数已被弃用,将来将被删除。考虑改用 PDOMySQLi

    【讨论】:

      【解决方案3】:
      foreach ($subArr as $sub => $result)
      {
        if (mysql_num_rows($result) > 0)
        {
          $resultString = '<a id="$sub" style="cursor: pointer; color: #0076cf;" href="$sub">'.'  |  '.$sub.'  |  '.'</a>';
        }
      
        $resultstring="";
      }
      

      【讨论】:

        【解决方案4】:

        您似乎走在正确的轨道上,但有些事情搞混了。

        菜单

        首先,在制作菜单时要使用unordered list,然后使用 CSS 设置样式。 basic example of this 是:

        <ul class="menu">
            <li><a href="test">Test</a></li>
            <li><a href="test2">Test 2</a></li>
            <li><a href="test3">Test 3</a></li>
        </ul>
        

        然后使用以下 CSS 设置样式

        ul.menu, ul.menu * {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        
        ul.menu {
            width: 100%;
            height: 20px;
            background: #ccc;
            padding: 5px 0; /* Add padding top and bottom */
        }
        
        ul.menu > li {
            height: 20px;
            line-height: 20px;
            float: left;
        }
        
        /* Make a tag fill the entire LI so users can click
        anywhere, not just on the text. */
        ul.menu > li > a { 
            display: block;
            padding: 0 10px; /* Add padding between items */
            color: #000;
            text-decoration: none;
        }
        
        ul.menu > li > a:hover, ul.menu > li > a:active {
            background: #000;
            color: #FFF;
        }
        
        /* Add divider between items, except last item (Does not work with earlier versions of IE) */
        ul.menu > li:not(:last-child) {
            border-right: 1px solid #000;
        }
        

        PHP 循环

        首先是一个注释。您正在使用mysql,即depreciated。这意味着在某个 PHP 版本中,它很快将不再可用。很多人推荐你学习PDO。就我个人而言,我更喜欢MySQLi 而不是准备好的陈述,但这只是我的偏好。两者都可以,但学习其中之一。

        现在为您的循环。您似乎在循环中检查 mysql 查询的结果,这是错误的。我在上面猜测您有一个查询,它将其结果加载到 $subArr 中。在将它们加载到 $subArr 之前,您需要调用 mysql_num_rows

        循环本身很好,除了你将它应用到上面的列表中。您的最终代码应如下所示。请注意,我在示例中使用了 MySQLi,我建议您也这样做,但如果您愿意,将其转换为 MySQL 应该不会太难。

        <?php
        $subArr = array();
        $query = "SELECT something FROM somewhere";
        $result = $mysql->query($query);
        if($result->num_rows) {
            while($row = $result->fetch_assoc()) { //I personally prefer fetch_assoc over the others, but fetch_row or fetch_array are both fine here too.
                $subArr = $row;
            }
        }
        
        //Lets output the menu
        $resultString .= '<ul class="menu">';
        foreach($subArr as $sub => $result) {
            $resultString .= '<li><a href="' . $result['url'] . '">' . $result['name'] . '</a></li>'
        }
        $resultString = '</ul>';
        

        最后一点,您不需要在 a 标签上添加 cursor: pointer,默认情况下它具有该样式。

        我希望这可以为您解决一些问题。

        【讨论】:

          猜你喜欢
          • 2014-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-10
          • 2015-05-21
          • 1970-01-01
          相关资源
          最近更新 更多