0

Using PHP I would like to take a list of categories stored in MySQL and order them based on a user defined order. Here is how the code works now:

Main File

<?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   
}   
?>

File containg the 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;            
}

I have considered making a JQuery interface where the user can drag & arrange then save the order that the categories should be displayed in. I was thinking that the information in the database could be each category would have a field that stored the order number. This order number could then be used to order the categories. What would be the most efficient way of achieving this using 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 に答える