趟过另一个“程序员”的PHP / MySQL代码

时间:2011-05-02 19:42:24

标签: php refactoring mysql functional-programming

好的,所以我得到了这段令人讨厌的代码:

<!-- Begin the Product Group Submenu -->
                       <ul>
<?php do  { ?> 
    <li><a href="prodgrouppg.php?groupid=<?php echo $row_menugroup['GroupID']; ?>&amp;groupname=<?php echo $row_menugroup['GroupName']; ?>"><?php echo $row_menugroup['GroupName']; ?></a>

<!-- Retrieve the individual products related to the Group -->                               
<?php                               
$groupprod_menuprod = "-1";
if (isset($row_menugroup['GroupID'])) {
  $groupprod_menuprod = (get_magic_quotes_gpc()) ? $row_menugroup['GroupID'] :     addslashes($row_menugroup['GroupID']);
}
mysql_select_db($database_conn_atex, $conn_atex);
$query_menuprod = sprintf("SELECT products.ProductID, products.ProductName,     products.ProductGroup FROM products WHERE products.ProductGroup=%s ORDER BY products.ProductName", GetSQLValueString($groupprod_menuprod, "int"));
$menuprod = mysql_query($query_menuprod, $conn_atex) or die(mysql_error());
$row_menuprod = mysql_fetch_assoc($menuprod);
$totalRows_menuprod = mysql_num_rows($menuprod);
?>

<!-- Begin the product submenu (final menu) -->

<ul>
<? $row_menuprod = mysql_fetch_assoc($menuprod)) 
{
    echo 
    <li>
        <a href="productpg.php?prodid=<?php echo $row_menuprod['ProductID']; ?>">
            <? echo $row_menuprod['ProductName']; ?>
        </a>
    </li>
    </ul> <!-- end of Product Submenu -->
    </li>
         <?php } while ($row_menugroup = mysql_fetch_assoc($menugroup)); ?>
            </ul> <!-- end of Group Submenu -->
        </li>
        <?php } while ($row_menusubcat = mysql_fetch_assoc($menusubcat)); ?>
    </ul> <!--end of Subcategory Submenu-->


</li> <!--end of Category Submenu-->
<?php } while ($row_menupaging = mysql_fetch_assoc($menupaging)); ?>

 </ul> <!--end of Menu -->

这基本上就是停止了无处不在的工作。如果我删除此代码,请求此文件通过确定,所以我假设这里的东西是问题。我已经尝试重新格式化了大约5次,所以我认为必须有一些我的眼睛缺失的东西。

有什么建议吗?对这段代码的状态道歉..试图一次修好一件......

1 个答案:

答案 0 :(得分:4)

不确定这是复制/粘贴错误还是实际问题,但看起来好像有PHP打开,然后切换到常规HTML而不关闭PHP块。 (我可能没有使用正确的术语;我不是PHP开发人员 - 我只是在Holiday Inn Express住过一次:p)

<!-- Begin the Product Group Submenu -->
                       <ul>
<?php do  { ?> 
    <li><a href="prodgrouppg.php?groupid=<?php echo $row_menugroup['GroupID']; ?>&amp;groupname=<?php echo $row_menugroup['GroupName']; ?>"><?php echo $row_menugroup['GroupName']; ?></a>

<!-- Retrieve the individual products related to the Group -->                               
<?php                               
$groupprod_menuprod = "-1";
if (isset($row_menugroup['GroupID'])) {
  $groupprod_menuprod = (get_magic_quotes_gpc()) ? $row_menugroup['GroupID'] :     addslashes($row_menugroup['GroupID']);
}
mysql_select_db($database_conn_atex, $conn_atex);
$query_menuprod = sprintf("SELECT products.ProductID, products.ProductName,     products.ProductGroup FROM products WHERE products.ProductGroup=%s ORDER BY products.ProductName", GetSQLValueString($groupprod_menuprod, "int"));
$menuprod = mysql_query($query_menuprod, $conn_atex) or die(mysql_error());
$row_menuprod = mysql_fetch_assoc($menuprod);
$totalRows_menuprod = mysql_num_rows($menuprod);
?>

<!-- Begin the product submenu (final menu) -->

<ul>
<? $row_menuprod = mysql_fetch_assoc($menuprod)) 
{
    echo ?> <!-- ### close goes here ### -->
    <li>
        <a href="productpg.php?prodid=<?php echo $row_menuprod['ProductID']; ?>">
            <? echo $row_menuprod['ProductName']; ?>
        </a>
    </li>
    </ul> <!-- end of Product Submenu -->
    </li>
         <?php } while ($row_menugroup = mysql_fetch_assoc($menugroup)); ?>
            </ul> <!-- end of Group Submenu -->
        </li>
        <?php } while ($row_menusubcat = mysql_fetch_assoc($menusubcat)); ?>
    </ul> <!--end of Subcategory Submenu-->


</li> <!--end of Category Submenu-->
<?php } while ($row_menupaging = mysql_fetch_assoc($menupaging)); ?>

 </ul> <!--end of Menu -->

从使用ASP / VBScript开始,我发现将一个不断切换代码/ html的页面迁移到直接代码可以帮助找到一些奇怪的东西,比如忘记关闭一个块或找到一种方法来收紧循环。