PHP 列表菜单框 - 循环的最佳方法?

发布于 2024-12-04 07:44:27 字数 1240 浏览 5 评论 0原文

这是我的后台页面的代码的一部分。 (是一个 edit.php 页面,供用户编辑/修改)

// 第一个查询从用户表中获取猫

$query = "select * from user where name='".$_SESSION['username']."' order by id ASC  limit 1";
$result=mysql_query($query);
if (mysql_num_rows($result)) {
while($row=mysql_fetch_array($result)){


$cat1 = $row['cat1'];
$cat2 = $row['cat2'];
$cat3 = $row['cat3'];
$cat4 = $row['cat4'];
$cat5 = $row['cat5'];
$cat6 = $row['cat6'];
$cat7 = $row['cat7'];
$cat8 = $row['cat8'];
$cat9 = $row['cat9'];
$cat10 = $row['cat10'];

}
}

// 现在我想构建 10 个选择框,并根据用户表 $cats

// 下面是我可以做的构建到第一个框 $cat1

// 有没有一种方法可以为 10 个选择框生成此内容,而无需制作 10 个循环的波纹管代码

<select name="theme" id="theme">
<?php
$q1 = 'SELECT * FROM cats ORDER BY title ASC';
$r1 = mysql_query($q1);
while( $row = mysql_fetch_array($r1)) {

if ( $cat1 == $row['id'] ) { 

print "<option class=\"cssoption\" selected=\"selected\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";

}

else {

print "<option class=\"cssoption\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";

}


}
?>
</select>

我不是编码员,因此这可能不是有效的代码。

希望有人可以在这里帮助我并理解我正在尝试做什么。

非常感谢。

This is part of code from my backoffice page. ( is an edit.php page for a user to edit / modify )

// first query to get cats from user table

$query = "select * from user where name='".$_SESSION['username']."' order by id ASC  limit 1";
$result=mysql_query($query);
if (mysql_num_rows($result)) {
while($row=mysql_fetch_array($result)){


$cat1 = $row['cat1'];
$cat2 = $row['cat2'];
$cat3 = $row['cat3'];
$cat4 = $row['cat4'];
$cat5 = $row['cat5'];
$cat6 = $row['cat6'];
$cat7 = $row['cat7'];
$cat8 = $row['cat8'];
$cat9 = $row['cat9'];
$cat10 = $row['cat10'];

}
}

// now i want to build 10 select boxes with selected according the user table $cats

// below is what i can build to first box $cat1

// is there a way i can produce this for the 10 select boxes whitout having to make 10 cycles of bellow code

<select name="theme" id="theme">
<?php
$q1 = 'SELECT * FROM cats ORDER BY title ASC';
$r1 = mysql_query($q1);
while( $row = mysql_fetch_array($r1)) {

if ( $cat1 == $row['id'] ) { 

print "<option class=\"cssoption\" selected=\"selected\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";

}

else {

print "<option class=\"cssoption\" value=\"".$row['id']."\">".htmlentities($row['title'])."</option>";

}


}
?>
</select>

I am not a coder so this might not be effective code.

Hope someone can help me here and understands what i am trying to do.

Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-12-11 07:44:27
  • 代码没问题。正如您所说,这 10 个周期的成本几乎为零。

  • 这是我们通常的做法,我们逐条顺序地获取记录。

  • 此外,要求不执行 10 个循环是没有意义的,因为您同时应用了 if else 条件,这意味着您需要检查每条记录的 cat id 是否与该行相同,因此您需要

  • 另一方面,如果由于某种原因你想跳过某些记录,你可以使用mysql的seek函数从所需的记录开始获取。

    for($i=0;$i<99999;$i++)
        (9999*9999);
    
    echo '这次循环的成本是:',(microtime()-$start),'秒。';
    

    ?>

  • The code is fine. This 10 cycles as you name it is a almost zero cost.

  • This is the usual way we do it, we fetch sequentialy the records one by one.

  • In addition it makes no sense to ask not to do the 10 cycles because you are applying an if else condition in the same time, this means that you check every record if the cat id is the same with the row so you need the loop.

  • On the other hand if for some reason you want to skip some records, you can use the mysql seek function to start fetching from the desired record.

    for($i=0;$i<99999;$i++)
        (9999*9999);
    
    echo 'This time the cost of this loop was:',(microtime()-$start),' seconds.';
    

    ?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文