我真的只是把脚趾伸进了PHP和MySQL的水里。我已经按照我想要的方式做了这件事,但我知道它很难看,我想知道如果有人知道他们在做什么,他们会怎么做。
function displayCategoryMenu($db){
echo '<ul class="class">';
foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "aaa0001" ORDER BY category, thingName') as $row){
if($categoryName!== $row['categoryName']){
$categoryName= $row['CategoryName'];
echo '<li class="category">'.$categoryName.'</li>';
}
echo'<li><a class="fader" href="?&page='.$row['thingId']">'.$row['thingName'].'</a></li>';
}
echo '</ul>';
echo '<ul class="class">';
foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "aaa0002" ORDER BY category, thingName') as $row){
if($categoryName!== $row['categoryName']){
$categoryName= $row['CategoryName'];
echo '<li class="category">'.$categoryName.'</li>';
}
echo'<li><a class="fader" href="?&page='.$row['thingId']">'.$row['thingName'].'</a></li>';
}
echo '</ul>';
$db=null;
}这基本上会导致无序列表菜单,其中第一个子菜单充当标题:
<ul>
<li>category1Name</li>
<li><a>thing</a></li>
<li><a>thing</a></li>
<li><a>thing</a></li>
</ul>
<ul>
<li>category2Name</li>
<li><a>thing</a></li>
<li><a>thing</a></li>
<li><a>thing</a></li>
</ul>或者:
Category1
Category2
等等..。
每个ul代码的唯一区别是"categoryId“(aaa0001,aaa0002等.)我只能想象,我要用我能收集到的最丑陋的代码来做这件事。我很想看看该怎么做。
发布于 2012-06-28 10:05:48
您应该将类别ID的列表存储在数组中的某个位置,在使用变量代替类别ID时对其进行迭代。
function displayCategoryMenu($db){
$categoryIds = array('aaa0001', 'aaa0002');
foreach ($categoryIds as $categoryId) {
echo '<ul class="class">';
foreach($db->query('SELECT * FROM categoryDb, thingDb WHERE category = category AND categoryId= "' . $categoryId . '" ORDER BY category, thingName') as $row) {
if($categoryName !== $row['categoryName']){
$categoryName = $row['CategoryName'];
echo '<li class="category">' . $categoryName . '</li>';
}
echo '<li><a class="fader" href="?&page=' . $row['thingId'] . '">' . $row['thingName'] . '</a></li>';
}
echo '</ul>';
}
}在这里,数组$categoryIds包含您必须显示的ID列表,您可以动态生成它,也可以根据您的意愿将它作为函数参数传递,它很容易扩展。foreach循环迭代此数组的每个元素,并对循环中由$categoryId变量表示的每个ID执行完全相同的HTML和SQL作业。
关于您的行的:如果您不传递$db函数参数作为参考,那么在您的函数中修改它的值将不会对原始变量产生任何影响,这行在这里是不必要的。
发布于 2012-06-28 12:16:34
使用您所做的相同的功能,我将同时选择所有类别和事物,如下所示:
function renderCategoryMenus($db, $category_id_array)
{
// Convery array('a00','b00') to string "'a00','b00'"
$category_ids = "'" . implode("','", $category_id_array) "'";
// Change * to just the fields you need
// Use a join
// WHERE id IN ('a00','b00')
$query =
'SELECT CategoryName, thingName
FROM categoryDb
INNER JOIN thingDb
ON categoryDb.category = thingDb.category
WHERE categoryId IN \''. $category_ids .'\'
ORDER BY category, thingName';
$results = $db->query($query);
// Initialize your variables
$output = '';
$categoryName = '';
foreach($results as $row)
{
if ($categoryName != $row['categoryName']) // If this is a new category
{
if ($categoryName != '') // And it is NOT the first new category
{
$output .= '</ul>'; // End off the previous category
}
$categoryName = $row['CategoryName'];
// Store your output into a variable so you can return it
$output .= '<ul class="class"><li class="category">' .
$categoryName . '</li>';
}
$output .= '<li><a class="fader" href="?&page=' .
$row['thingId'] . '>' . $row['thingName'] . '</a></li>';
}
// End off the last category
if ($output != '') // If we had any categories whatsoever
{
$output .= '</ul>';
}
return $output;
}
echo renderCategoryMenus($db, array('aaa0001', 'aaa0002'));一个更简单的方法是像Yii一样使用对象关系映射器。
使用Yii,您可以做以下简单的事情:
function renderCategoryAsMenu($category)
{
$output .= '<ul class="class"><li class="category">' .
$categoryName . '</li>';
// This bit actually selects all 'things' in this 'category'
foreach ($category->things as $thing)
$output .= '<li><a class="fader" href="?&page=' .
$thing->thingId . '>' . $thing->thingName . '</a></li>';
}
$output .= '</ul>';
return $output;
}
$category_keys = array('a00','b00');
$categories = Category::model()->findAllByPk($category_keys);
$output = '';
foreach ($categories as $category)
{
$output .= renderCategoryAsMenu();
}
echo output;您甚至可以在模型上使renderCategoryAsMenu成为一个函数,在这种情况下,您只需说$category->renderAsMenu()。
发布于 2012-06-28 10:19:01
根据您希望显示的类别to:
我建议进行1次查询,并在结果集中循环。
那要么是
1)没有WHERE clausule (除非您需要它来连接,因为您有2个WHERE条件)
SELECT * FROM categoryDb, thingDb ORDER BY category, thingName( 2)或扩展到
WHERE categoryId="aaa001" OR categoryId="aaa002" ...此外,我还建议:
if($categoryName!== $row['categoryName'])
{
echo '<li class="category">' . $row['CategoryName'] . '</li>';
}您可以将函数重命名为"writeCategoryMenu“。
https://stackoverflow.com/questions/11242181
复制相似问题