【问题标题】:menu problems using php and mysql使用 php 和 mysql 的菜单问题
【发布时间】:2014-06-17 21:22:50
【问题描述】:

我有一个包含两列名为 rugby_union 和 matchleague 的表。 我创建了 2 个查询来显示不同的 rugby_union 和 matchleague。当我在菜单上有下拉菜单时,它必须仅显示来自 rugby_union 的 matchleague。 我正在使用的代码:

$sql="select distinct(union_league) from teams where league_complete ='No' ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){ 
$matchleaguecomp[] = $row; 
}

$sql1="select distinct(rugby_union) AS rugby_union from teams where league_complete ='No' ";
$query1 = mysql_query($sql1);
while($row1 = mysql_fetch_array($query1)){ 
$union_list[] = $row1; 
}

导航栏中的 PHP 代码:

<?php                       
    for($i=0;$i<count ($union_list);$i++){  
?>
<li>
    <a href="#"><?php echo $union_list[$i][0]; ?></a>
    <ul class="dropdown-menu" 
         style="position: absolute; left: 160px; top: 0px; display: none;
                height: <?php if(count($matchleaguecomp)>0) {
                                  echo '260px;'; 
                              } else { 
                                  echo (count($matchleaguecomp)*26).'px'; 
                              } ?>  
                overflow: auto;">
        <?php
            for($i=0;$i<count($matchleaguecomp);$i++){                      
                //$fixture_name=$url->encode("name=".$matchleaguecomp[$i][0]);
            $fixture_name="name=".$matchleaguecomp[$i][0];
        ?>
        <li>
            <a href="<?php echo $include_path.'new_logs.php?'.$fixture_name; ?>">
                <?php echo $matchleaguecomp[$i][0]; ?>
            </a>
        </li>
        <?php  } ?>
    </ul>
</li>
<?php  } ?> 

提前谢谢,我不知道为什么它不显示联盟的工会列表

【问题讨论】:

  • 你永远不会从 $union_list 输出任何东西,而且你构建的数组是错误的 - 你将所有内容都转储到数组键 [0] 中,这意味着你用下一个获取的记录覆盖之前的每条记录.
  • 嗨,Marc B,请您编辑代码以正确显示信息
  • while(...) { $union_list[] = $row1 }
  • 我确实修复了它,但无法显示联合列表
  • mysql_query 是一个过时的接口,不应在新应用程序中使用,并将在未来的 PHP 版本中删除。像PDO is not hard to learn 这样的现代替代品。如果您是 PHP 新手,PHP The Right Way 之类的指南可以帮助您解释最佳实践。

标签: php mysql menubar


【解决方案1】:

首先,请注意@tadman 所说的 mysql_query 已过时... 这是您的固定代码:

$sql="select distinct(union_league) as `union_league` from teams where league_complete ='No' ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){ 
$matchleaguecomp[] = $row['union_league']; // you need to get the returned value from the array
}

$sql1="select distinct(rugby_union) AS rugby_union from teams where league_complete ='No' ";
$query1 = mysql_query($sql1);
while($row1 = mysql_fetch_array($query1)){ 
$union_list[] = $row1['rugby_union']; // you need to get the returned value from the array
}

【讨论】:

  • Len,感谢 union_league 的工作,但 rugby_unions 仍然有相同的结果,只显示 1 个联合
  • 当您从命令行或 PHPMyAdmin 中运行该查询时,您会得到多少?
  • Len,当我在 PHPMyadmin 中运行时,我得到 2,当我运行 print_r 时,它会显示所有内容
  • 为了好玩,将 AS rugby_union 更改为 AS rugby_union。它不应该有什么不同,但至少查询的结构将与给你成功的那个结构相匹配。我见过奇怪的东西......
  • sql 查询 100% 正确,是 PHP 脚本导致问题。对于 union_league,它 100% 有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
相关资源
最近更新 更多