Reply to Re: Please Help !!! I need to sort an Array !!!

Your name:

Reply:


Posted by Erwin Moller on 11/18/05 13:17

Angelos wrote:

> Current Case:
> I have a table in a database
>
> PRODUCTS
> --------------
> product_id
> product_name
> product_category
> product_order
> _______________________
> I am trying to create a change order script. With Up/Down links.
>
> So I run my querry and store the data in the database. So I end up with an
> array
>
> ID | 22 | 27 | 30 | 37 | 40 |
> CATEGORY | 2 | 2 | 2 | 2 | 2 |
> ------------------------------------------
> ORDER | 1 | 2 | 3 | 4 | 5 |
>
> Now when I click the UP link for example on ID 30 it sends a querry
> ?move_id=30&to_id=27
> Ofcourse there are other ways of doing that but what I want to do is MOVE
> products from category to category and then the problem is that I may end
> up MOVING for example product with ID 45 CATEGORY 4 and ORDER 2 so the
> above array will look like:
>
> ID | 22 | 45 | 27 | 30 | 37 | 40 |
> CATEGORY | 2 | 2 | 2 | 2 | 2 | 2 |
> ------------------------------------------
> ORDER | 1 | 2 | 2 | 3 | 4 | 5 |
>
> and after that what I want to do is reset the order positions and
> re-number then 1,2,3,4,5,6
>
> Can anyone give me some HELP PLEASE !!!!
> Thank you...


Hi,

It is not very clear to me what your actual array(s) looks like.

So the following is just one of the 1000 approaches you could take in PHP.
And it only show how to make a distinct order and store it.

I think you make your life easier by making an array that contains orders,
and add a position to them.
I like to have my arrays neatly structured this way. It takes up a little
extra mem, but gives the programmer more convinience and clearity.

$allorders = array();
$oneorder = array("ID" => 22, "CAT" => 2, "ORDER" => 17);
$allorders["pos1"] = $oneorder;

$oneorder = array("ID" => 24, "CAT" => 4, "ORDER" => 12);
$allorders["pos2"] = $oneorder;

etc.

In that way you have a simple array ($allorders) with indexing (key) for
position.
The elements contain another array, being the actual order.

So if you have to add a new order:
$neworder = array("ID" => 211, "CAT" => 5, "ORDER" => 1);
$allorders["pos".(count($allorders)+1)] = $neworder;

If you want to switch pos 9 with pos 8:
$someoder = $allorders["pos9"];
$allorders["pos9"] = $allorders["pos8"];
$allorders["pos8"] = $someorder;

This is just an example of how to store your orders.

It is not a great example because if you delete one of the orders in the
middle, your indexing "posXX" breaks, and you have to redo the indexing,
which is annoying with the keys I have choosen.
(So it is a bad example in real world)
I just wanted to show how to make a distinct 'order', and store that in an
array. Then store that 'orderarray' into another array in a way that suits
your needs.
Hope this gets you going.

Regards,
Erwin Moller

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация