Smart, Sortable Product Lists
There comes a time in everyone’s life when you need to do a little sorting. Whether this is sorting your room or sorting out relationships some form of arrangement and re-arrangement of data/things/spouses is required. When it comes to magento you may want to do the same thing to your store.
The following code and file is not simply a finished product for you to take away and use and that’s the end of it, it’s more of an open source compilation written outside of magento for people to have a quick look at their product listings – it acts almost like the exported spreadsheet from the back end but you can adapt this file in any way you like to bring out any data you want! Depending how creative you are you can do almost anything with the code that I have written for you – take it as the foundations to perhaps an entire dynamic product catalogue that someone who may or may not be me, may or may not write in the future…
Smartening up your product lists – simply copy and paste the following file onto your root magento layer and run it in the browser.
Here is the FILE (magentofox-productlisting.php).
Here is the code:
<?php
function msort($array, $id, $sort_ascending=true) {
$temp_array = array();
while(count($array)>0) {
$lowest_id = 0;
$index=0;
foreach ($array as $item) {
if (isset($item[$id])) {
if ($array[$lowest_id][$id]) {
if ($item[$id]<$array[$lowest_id][$id]) {
$lowest_id = $index;
}
}
}
$index++;
}
$temp_array[] = $array[$lowest_id];
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
}
if ($sort_ascending) {
return $temp_array;
} else {
return array_reverse($temp_array);
}
}
?>
<table width="100%">
<tr><td><img src="http://a3.twimg.com/profile_images/949933723/magento-fox.gif" alt="Magento Fox" align="left" /></td><td colspan="3"><h1>Magento Fox Product Listing</h1></td><td>Sorted by <strong><?php if(isset($_GET['sort'])){ echo $_GET['sort']; } else { echo "product_id"; } ?></strong></td></tr>
<tr><th><a href="?sort=category_name">Category</a> (<a href="?sort=category_id">ID</a>)</th><th><a href="?sort=product_id">Product Id</a></th><th><a href="?sort=product_sku">SKU</a></th><th><a href="?sort=product_name">Name</a></th><th><a href="?sort=product_price">Price</a></th></tr>
<?php
require_once 'app/Mage.php';
Mage::app('default');
// Setting up our Array structures
$category_array = array();
$product_array = array();
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT product_id, category_id FROM catalog_category_product ORDER BY product_id");
while ($row = $readresult->fetch() ) {
$write2 = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult2=$write2->query("SELECT sku FROM catalog_product_entity WHERE entity_id = ".$row['product_id']." LIMIT 1");
$prodid = $row['product_id'];
$catid = $row['category_id'];
while ($row2 = $readresult2->fetch() ) { $sku = $row2['sku']; }
$_product = new Mage_Catalog_Model_Product();
$_product->load($prodid);
$_category = new Mage_Catalog_Model_Category();
$_category->load($catid);
$products[] = array("category_name" => $_category->getName(), "category_id" => $catid, "product_id" => $prodid, "product_sku" => $sku, "product_name" => $_product->getName(), "product_price" => $_product->getFinalPrice());
}
if(isset($_GET['sort'])){
$products = msort($products, $_GET['sort']);
}
$myx = 0;
foreach($products as $prod){ ?>
<trzero"; $myx = 1; } else { echo "one"; $myx = 0; } ?>"><td><?php echo $prod["category_name"]; ?> (<?php echo $prod["category_id"]; ?>)</td><td><?php echo $prod["product_id"]; ?></td><td><?php echo $prod["product_sku"]; ?></td><td><?php echo $prod["product_name"]; ?></td><td>£<?php echo number_format(round($prod["product_price"], 2), 2); ?></td></tr>
<?php } ?>
</table>
Here is how it works:
What you see here is the output of our php file – the file connects to magento and extracts the product list from the database. It then loads the category and the products and outputs the data required.
I notice I have made a small error here and ran the SKU as a separate mysql command. This could be transformed to $_product->getSKU() I’m guessing but as I said before it really is yours to play with.
Next we run a command that stores the values we return into a nice array. This array is then outputted into a table format. We can sort this table by clicking on the table headings – this will create a get function and sort the array before it is outputted.
Warning
- this code outputs ALL of your products – I tried it on a store with 750 products on and it took 20 seconds to load the page. Use this as a rule of thumb – however I would recommend adding a form of pagination to this code and also limiting the sql array to something smaller like 50 per page.
(to do the limit simply add LIMIT 50 after the PHP code on line 57 of the file).
I hope this is of use to someone and thank you for visiting our magento blog at ecommerce website design!

