PDA

View Full Version : Dynamic links, order and administration....


Psychoman
09-01-2004, 06:15 AM
Hey guys, I have a question for you,

I basically have the system down, I just need your help figuring out the best way to do it.

I want to be able to have dynamic links that use a database to display them. each row has an id, an order_id, the link text and the page to link to. now, I would like to be able to sort the display of links by the "order_id" that can do something like this.

Link order id

Books 1
Magazines 2
Catalogs 3

then use an administrative php script to move the links arround, say select magazines then "move up" and it adds 1 to books and subtracts 1 to magazines ending up with

Magazines 1
Books 2
Catalogs 3

Like i said, the basics is simple enough, however, what would be the best way to apply this to php so that it always knows which entries to modify? I can come up with something, but its not much.... I'm kinda zoned out as it is so that may be my whole problem. either way, i can use some outsider's help.

Here's what I can come up with

$sql="SELECT * FROM links WHERE order_id='$current_order";
$result = @mysql_query($sql,$db);

if ($moveup == on){
$new_order_id = $current_order + 1;

$sql="UPDATE order_id='$new_order_id' WHERE order_id='$current_order";
$result = @mysql_query($sql,$db);

// I'm actually stuck here.... how can I check to see if there is an entry under the one I am editing, using count?
} else {
$new_order_id = $current_order - 1;

$sql="UPDATE order_id='$new_order_id' WHERE order_id='$current_order";
$result = @mysql_query($sql,$db);

// I'm actually stuck here.... how can I check to see if there is an entry under the one I am editing, using count?
}

sorry its really crude, thats basically off the top of my head... can somone help me out?

Thanks,
Ray

Psychoman
09-03-2004, 11:44 PM
Ok, since no one offered any help or a post asking me to refrase I have tried my best to resolve the issue... this is the best I have right now


if($new){
$sql = "SELECT order_id FROM links WHERE order_id='" . $pos . "'";
$result = mysql_query($sql, $db) or die ('Queryproblem:' . mysql_error());
$id_check = mysql_num_rows($result);

if(($id_check > 0)){
$rows = mysql_query("SELECT order_id FROM links ORDER BY order_id");
if ($myrow = mysql_fetch_array($rows)) {
do {
$order = $myrow["order_id"];
if ($order > $pos){
$new_order = $new_order + 1;
$result = mysql_query("UPDATE links SET order_id='$new_order' WHERE order_id='$order'");
} elseif($order == $pos){
$new_order = $order + 1;
$result = mysql_query("UPDATE links SET order_id='$new_order' WHERE order_id='$order'");
$result = mysql_query("INSERT INTO links (link,linktext,order_id) VALUES ('$link','$text','$pos')");
} else {
// ignore
}

} while ($myrow = mysql_fetch_array($result));
}
} else {
$result = mysql_query("INSERT INTO links (link,linktext,order_id) VALUES ('$link','$text','$pos')");
}
}

This code first looks to see if the entry being created has a $pos that already exists in the table. If it does not exist, it simply skips to the

} else {
$result = mysql_query("INSERT INTO links (link,linktext,order_id) VALUES ('$link','$text','$pos')");
}

and ignores the conditions.

If it does exist however, it goes trough every row to compare the $pos with the order id to see if its ==, > or <

If the order is < the pos it simply ignores it (this works fine)
if the order is == it adds one to the order_id of the existing row and then inserts the new row. (this also works fine)
the one I am having problems getting to work is the condition if order > pos

it simply does not work at all.

Can someone please help me?
Ray

dumpfi
09-04-2004, 10:49 AM
if($move_up = $_GET['move_up']) {
$current_order = $_GET['current_order'];
$result = @mysql_query('SELECT COUNT(*) FROM links WHERE order_id = "'.$currend_order.'"', $db);
if(mysql_num_rows($result)) {
$replace_order = ($move_up == 'up') ? $current_order + 1 : $current_order - 1;
if($replace_order > 0) {
$replaced = @mysql_query('SELECT * FROM links WHERE order_id = "'.$replace_order.'"', $db);
if(mysql_num_rows($replaced)) {
@mysql_query('DELETE FROM links WHERE order_id = "'.$replace_order.'"', $db);
@mysql_query('UPDATE links SET order_id = "'.$replace_order.'" WHERE order_id = "'.$current_order.'"', $db);
$fields = '';
$values = '';
$value = @mysql_fetch_assoc($replaced);
for($field in $value) {
$fields .= $field.',';
$values .= ($field == 'order_id') ? '"'.$current_order.'",' : '"'.$value[$field].'",';
}
@mysql_query('INSERT INTO links ('.substr($fields, 0, -1).') VALUES('.substr($values, 0, -1).')', $db);
}
else echo 'Can\'t be moved down any further.';
}
else echo 'Can\'t be moved up any further.';
}
else echo 'Link does not exist.';
}
else echo 'Not specified whether to move up or down.';
This should move up / down your links. To move them the move-link needs to be something like file_name.php?move_up=up&current_order=3 or file_name.php?move_up=down&current_order=7.

dumpfi

Kurashu
09-04-2004, 04:27 PM
Actually, I think, using POST would be better for this sort of data. Just my opinion. And as for the script, seems like an interesting script, I might have to build one for my site some time.

Psychoman
09-08-2004, 10:21 PM
Thank you dumpfi, however there seems to be a parse error in your script

Parse error: parse error, unexpected T_STRING, expecting ';' in linkmanage2.php on line 14

(sorry it took me so long to look at it, The hurricane was bad enough to stop me from working)
Ray

Psychoman
09-13-2004, 02:22 PM
I am still having problems trying to make out that code, can anyone lend me a hand?