0

使用 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>\r\n";

            if ($c % $categoriesPerRow == $categoriesPerRow - 1) {
                echo '</tr>';
            }
        }
        $c++;

    }

    if ($i % $categoriesPerRow > 0) {
        echo '<td colspan="' . ($categoriesPerRow - ($i % $categoriesPerRow)) . '">&nbsp;</td>';
    }
} else {
?>
    <tr><td width="100%" align="center" valign="center">No categories yet</td></tr>
<?php   
}   
?>

包含函数 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;            
}

我考虑过制作一个 JQuery 界面,用户可以在其中拖动和排列,然后保存类别应该显示的顺序。我在想数据库中的信息可能是每个类别都有一个存储订单号的字段。然后可以使用此订单号对类别进行排序。使用 PHP 实现这一目标的最有效方法是什么?

4

1 回答 1

0

愚蠢的我,我想出来了。

SELECT cat_id, cat_name, cat_image
    FROM tbl_category
    WHERE cat_parent_id = 0
    ORDER BY cat_order

cat_order 是一个数字,表示其按顺序排列的位置。jquery 设计将允许用户订购它。

于 2012-10-24T18:33:26.943 回答