使用PHP我想列出存储在mySQL中的类别列表,并根据用户定义的顺序订购它们。这是代码现在的工作方式:
主文件
<?php
$categoryList = getCategoryList();
$categoriesPerRow = 4;
$numCategory = count($categoryList);
$columnWidth = (int)(100 / $categoriesPerRow);
if ($numCategory > 0) {
$i = 0;
$c = 0;
for ($i; $i < $numCategory; $i++) {
//We skip the dragster category.
if($categoryList[$i]['name'] == "Dragsters"){
continue;
}else{
if ($c % $categoriesPerRow == 0) {
echo '<tr style="vertical-align:top;">';
}
// we have $url, $image, $name, $price
extract ($categoryList[$i]);
echo "<td width="$columnWidth%" align="center" style="vertical-align:top;"><a href="$url"><img src="$image" border="0"><br>$name</a><br></td>rn";
if ($c % $categoriesPerRow == $categoriesPerRow - 1) {
echo '</tr>';
}
}
$c++;
}
if ($i % $categoriesPerRow > 0) {
echo '<td colspan="' . ($categoriesPerRow - ($i % $categoriesPerRow)) . '"> </td>';
}
} else {
?>
<tr><td width="100%" align="center" valign="center">No categories yet</td></tr>
<?php
}
?>
文件conta conta function getCategoryList()
function getCategoryList()
{
$sql = "SELECT cat_id, cat_name, cat_image
FROM tbl_category
WHERE cat_parent_id = 0
ORDER BY cat_name";
$result = dbQuery($sql);
$cat = array();
while ($row = dbFetchAssoc($result)) {
extract($row);
if ($cat_image) {
$cat_image = WEB_ROOT . 'images/category/' . $cat_image;
} else {
$cat_image = WEB_ROOT . 'images/no-image-small.png';
}
$cat[] = array('url' => $_SERVER['PHP_SELF'] . '?c=' . $cat_id,
'image' => $cat_image,
'name' => $cat_name);
}
return $cat;
}
我考虑过制作一个用户可以拖动&amp;的jQuery界面。安排然后保存应该显示类别的顺序。我认为数据库中的信息可能是每个类别都有一个存储订单号的字段。然后可以使用此订单号来订购类别。使用PHP最有效的方法是什么?
愚蠢的我,我把它弄出来了。
SELECT cat_id, cat_name, cat_image
FROM tbl_category
WHERE cat_parent_id = 0
ORDER BY cat_order
cat_order是一个表示其位置的数字。jQuery设计将允许用户订购。