[SOLVED] Move Up and Move Down mysql results....

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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

[SOLVED] Move Up and Move Down mysql results....

Post by Mr Tech »

Hi there!

I'm setting up a script that allows user to add links which are saved in a MySQL Database. First of all, this is what the database looks like:

Code: Select all

place | title         | url  

01    | Jig Heads     | jig_heads.php 
02    | Soft Plastics |soft_plastics.php 
03    | Wire Bait     | wire_baits.php 
04    | Metal Lures   | metal_lures.php
Now what I want it to do is allow the user to move the place up or down a number so that when the script displays the results on the page, it will put the links in the correct order. For example, I could "switch" the place numbered 03 up to the place numbered 02.

This works similar to the phpBB admin script which allows you to move forums and categories up and down...

Code: Select all

place | title         | url  

01    | Jig Heads     | jig_heads.php 
03    | Soft Plastics |soft_plastics.php 
02    | Wire Bait     | wire_baits.php 
04    | Metal Lures   | metal_lures.php
At the moment I've go this code below. It works if it finds a number one place less then the first number but not if you don't have a place numbered one less then the first one:

Code: Select all

<?php
if ($place == "01") {

echo msg("Cannot move up! <a href=index.php?file=filesfolders&mode=nav>Back to Modifying Side Navigation Links</a>");

} else {

function fix_num($num) {
if ($num < "10") {
$num = "0".$num;
}
return $num;
}

function check_before($num) {
global $tbl_name;

$check_num = mysql_fetch_array(mysql_query("select place from {$tbl_name}nav where place = $num - 1"));

if ($check_num[place] == "") {
$next_num = $num - 1;
$next_num = fix_num($next_num);
check_before($next_num);
} else {
return $check_num[place];
}

} // end check before

$number = check_before($place);

echo $number; // this should be the first page number below $place
?>
Basically what it's supposed to do is check if the number below it exisits.

E.g: If $place is 03, it needs to check if 02 exisits in the database. And if it does exisit, it will change the place numbered 03 to 02 and the place number 02 to 03 in the database.

And if a place below it does not exisit in the database, it needs to search the number below the one it last searched.

E.g: If $place is 03 it needs to check if 02 exisits in the database. If 02 does not exisit in the database, it needs to check if 01 exists. If 01 exisits in the database, it will change the place numbered 03 to 01 and the place number 01 to 03 in the database.

Anyone have and ideas why this is working.

Hopefully you understand... If not please let me know.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT MAX( place ) place WHERE place < x       # move up
SELECT MIN( place ) place WHERE place > x       # move down
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Thanks Feyd,

I tried the code. It works fine if the $place is set to 04. It will display 03 as the next number below.

But if I set the place to 06, it displays 0504 asn the next numebr below when it should just show 04.

Code: Select all

$number = mysql_fetch_array(mysql_query("SELECT MAX( place ) place from &#123;$tbl_name&#125;nav WHERE place < $place"));
echo $number&#1111;place];
Any ideas?
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

I think I worked out the problem... I had some of the old function code still there which may of done the problem. I'll keep testing and if it still stuffs up I'll let you know.

Thanks
Post Reply