Big small problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deleet
Forum Commoner
Posts: 28
Joined: Thu Mar 23, 2006 10:05 am

Big small problem.

Post by deleet »

Here's the thing. I need to create a sorting script. I need some serious help.

Basically there's a news script that's displayed and I need to add a textbox (it must be a textbox) so that the user is able to sort the news out (with integers, 1 to $counter).

Till here fine, I have the values stored in a MySql db and I can get / update them just fine. The problem is that I have to:

a) Check for double used values and AUTOMATICALLY correct them. Eg: news1 - position7, news6 - position 7 -> would end up position 7 / 8 respectively.

b) Store the new values (or just the changed ones, I can add a hidden field with the old value for comparison) into the db.

I have no idea of how to validate a).

I also thought of a mathematical solution that involved subtracting the old_value from the new_value, if it yields a positive number it changes values of the fields in between and changes the values of all the fields after the old_value. If it yields a negative value, it changes the values after new_value and subtracts a value from old_value+difference.

It's a mess and I have no idea of how to do this.

Many thanks for the help.
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

So basically you you want:

Code: Select all

NAME         POSITION
News Article1 [__1__]
News Article2 [__2__]
News Article3 [__3__]
News Article4 [__4__]
News Article5 [__5__]
News Article6 [__6__]
News Article7 [__7__]
News Article8 [__8__]
And so on, so if you changed these values:

Code: Select all

NAME         POSITION
News Article1 [__1__]
News Article2 [__7__]
News Article3 [__3__]
News Article4 [__4__]
News Article5 [__3__]
News Article6 [__6__]
News Article7 [__7__]
News Article8 [__8__]
Then you press update, it would make the listing:

Code: Select all

NAME         POSITION
News Article1 [__1__]
News Article3 [__2__]
News Article5 [__3__]
News Article4 [__4__]
News Article6 [__5__]
News Article2 [__6__]
News Article7 [__7__]
News Article8 [__8__]
Did I get that right?
deleet
Forum Commoner
Posts: 28
Joined: Thu Mar 23, 2006 10:05 am

Post by deleet »

Yes, that's correct. But how to change the values automatically is out of my reach.
deleet
Forum Commoner
Posts: 28
Joined: Thu Mar 23, 2006 10:05 am

Post by deleet »

*bump* Can anyone help?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Maybe try sorting the array instead of in the database.
deleet
Forum Commoner
Posts: 28
Joined: Thu Mar 23, 2006 10:05 am

Post by deleet »

That was my original idea but how can I check for duplicates and automatically correct them ( the php function array_unique doesn't work since it returns a changed array ) ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are a bunch of array functins to choose from. Read the array section of the PHP manual for a list of array functions.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

deleet, you waited just over 12 hours to bump your thread. This is against forum rules. Do not do it again.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
Post Reply