我可以进一步优化这个 PHP 脚本以从 MySQL 数据库生成动态导航吗?

发布于 2024-07-19 08:38:37 字数 1386 浏览 5 评论 0原文

我正在开发一个新的 CMS 以用于重复项目。 基本上,这段代码连接到服务器,获取所有表的名称并使用它们生成简单的导航。 它非常适合我的需要,但我只是想知道是否可以进一步优化此代码片段并使其更加简单。 也许制作一个可以自定义格式的类? 等等。我试图使其尽可能“简单”。

我唯一想解释的是,它会检查表名称是否不“包含”,这是我的 CMS 使用的默认表,用于了解要在前端显示哪些数据。

   <?php

              echo '<div class="dynamic_nav_head">Navigation</div>';
              echo '<div class="dynamic_nav">';
                include('data.php');
                $tables = mysql_list_tables($database);
                  while (list($table) = mysql_fetch_row($tables)) { 
                            if($table!='includes'){
                      echo "<div class='cat'>".ucwords($table)."</div>";
                              echo "<div class='cat_item'>";
                                echo "<a href='?page=read&section=".$table."'>View " . ucwords($table) . "</a>";
                              echo "</div>";
                      echo "<div class='cat_item'>";
                                echo "<a href='?page=add&section=".$table."'>Add New ". ucwords($table) ."</a>";
                              echo "</div>";
                            }  // End If not in Includes.
                  } // End While
              echo '</div>';
 ?>

关于如何使这段代码更精简、更干净、更快速,有什么建议吗? 提前致谢!

编辑: MySQL 版本:4.1.22

I'm working on a new CMS to use on repeative projects. Basically this chunk of code connects to a server, grabs all the names of the tables and uses them to generate a simple navigation. It works pretty great for what I need however I'm just wondering if maybe I can optimize this code sniplet even more and make it even more simple. Maybe making a class that could customize the formating? Etc. I tried to make this as "bare-bones" as possible.

The only thing that is there that I would like to explain is that it checks to see if the table name isn't "includes" this is a default table my CMS uses to know what data to display on the front end as far as data.

   <?php

              echo '<div class="dynamic_nav_head">Navigation</div>';
              echo '<div class="dynamic_nav">';
                include('data.php');
                $tables = mysql_list_tables($database);
                  while (list($table) = mysql_fetch_row($tables)) { 
                            if($table!='includes'){
                      echo "<div class='cat'>".ucwords($table)."</div>";
                              echo "<div class='cat_item'>";
                                echo "<a href='?page=read§ion=".$table."'>View " . ucwords($table) . "</a>";
                              echo "</div>";
                      echo "<div class='cat_item'>";
                                echo "<a href='?page=add§ion=".$table."'>Add New ". ucwords($table) ."</a>";
                              echo "</div>";
                            }  // End If not in Includes.
                  } // End While
              echo '</div>';
 ?>

Any Suggestions on how I can make this code even leaner, cleaner, and more swift? Thanks in advance!

Edit:
MySQL Version: 4.1.22

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

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

发布评论

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

评论(3

关于从前 2024-07-26 08:38:37

我建议您访问 http://refactormycode.com/

        echo '<div class="dynamic_nav_head">Navigation</div><div class="dynamic_nav">';  // on less echo
        include('data.php');
        $tables = mysql_list_tables($database);
          while (list($table) = mysql_fetch_row($tables)) {     
                    if($table!='includes'){
                          $ucTable= ucwords($table); // just one function call
                           //  just one echo;
                           // you where also using quotes and double quotes backwards

                           echo '<div class="cat">'.$ucTable.'</div><div class="cat_item"><a href="?page=read§ion='.$table.'">View ' .$ucTable.'</a></div><div class="cat_item"><a href="?page=add§ion='.$table.'">Add New '. $ucTable .'</a></div>';
                    }  // End If not in Includes.
          } // End While
      echo '</div>';

I suggest you to visit http://refactormycode.com/

        echo '<div class="dynamic_nav_head">Navigation</div><div class="dynamic_nav">';  // on less echo
        include('data.php');
        $tables = mysql_list_tables($database);
          while (list($table) = mysql_fetch_row($tables)) {     
                    if($table!='includes'){
                          $ucTable= ucwords($table); // just one function call
                           //  just one echo;
                           // you where also using quotes and double quotes backwards

                           echo '<div class="cat">'.$ucTable.'</div><div class="cat_item"><a href="?page=read§ion='.$table.'">View ' .$ucTable.'</a></div><div class="cat_item"><a href="?page=add§ion='.$table.'">Add New '. $ucTable .'</a></div>';
                    }  // End If not in Includes.
          } // End While
      echo '</div>';
木槿暧夏七纪年 2024-07-26 08:38:37

你怎么知道代码很慢? 您的分析器对代码有何评价? 哪个语句会减慢速度? 你在什么平台? mysql是什么版本的? 该目录中有多少张表? 您是否遭受过早优化的困扰?

How do you know the code is slow? What does your profiler say about the code? Which statement is slowing it down? What platform are you on? What version of mysql? How many tables are in this catalog? Are you suffering from premature optimization?

明月松间行 2024-07-26 08:38:37

代码还不错。 可读性的一项改进是从数据库构建一个数组,而不是在“while”构造中使用 mysql_fetch_row。 这还允许您在进入循环之前过滤掉不需要的名称。 您还可以将 ucwords 方法映射到数组上,从而将其从 while 循环构造中取出。 关于双引号的观点是有效的,但为了可读性,我会保留单独的 echo 语句,因为这实际上不会产生明显的差异。

希望有帮助。

The code is not bad. One improvement for readability also would be build an array from the database rather than having mysql_fetch_row within the 'while' construct. This would also allow you to filter out the unwanted names before entering the loop. You could also map the ucwords method on the array allowing you to take this out of the while loop construct. The point about the double quotes is a valid one but I would keep seperate echo statements for readability since this is not really going to make an appreciable difference here.

Hope that helps.

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