Page 1 of 1

Post form data as array? or.. Please share input!

Posted: Fri May 28, 2004 7:24 pm
by idotcom
Hi

I have a page I'm working on and its a bit confusing to me right now.. :?
I havent been coding for a little while maybe thats the reason I cant seem to figure out this problem.


I use a database(mysql) to store menu links for my site. What I am trying to do is pull the links from the database and use form input fields to control the order that they are displayed in the menu.

example of page..

file_name ----------------------------- link_order

home.php .................................. (input here)
contact.php ................................ (input here)
about.php .................................. (input here)

Now the (input here) is where the text input field is. Using this to add a number(1,2,3,4,5.. so on) to control the order.

How can I do this? to post the form and correctly process it... I am trying to use the page file name and the order value like this

$query="UPDATE pages SET link_order='$link_order' WHERE file_name='$file_name'";


Please help!! I'm sitting here looking at this page as you are reading it :roll:

Thanks... Chris

Posted: Fri May 28, 2004 8:21 pm
by kettle_drum
You doing it right except that if your posting the data you should refer to the variables using $_POST['file_name'] etc.

Posted: Fri May 28, 2004 8:55 pm
by idotcom
thanks for your reply...

but how would I relate the post filename to the order value

could you offer some example code?? please..

All i need is a method to relate the file name with the link order value.

so when updating, I could do update link order where file name..


Thanks again

Posted: Fri May 28, 2004 9:18 pm
by kettle_drum
I would simply name the text imput field the name of the page, minus the .php part - since im sure they will all be .php, maybe also have an identifier to show identify the fields which are passing menu orders. Then you just use the key() command to get the name of the array key in the $_POST[] array. So:

Code: Select all

<form action="blah" method="post">
   <input type="text" name="MENU-contact">
</form>
Then the php bit:

Code: Select all

foreach(key($_POST) as $x){ #think that should work
   echo "the page name is ".substr($x, 4).".php";
   echo "the menu order is ".$_POST[$x];
}

Posted: Fri May 28, 2004 9:56 pm
by idotcom
Thanks for helping me out kettle_drum 8)

I'll give it a shot... looks straight forward.

:D

Posted: Sat May 29, 2004 6:29 am
by dull1554
my suggestion would be to have an id field in the database, relate the text fields to the id(name=$id) then use a loop and a isset statement to loop through until all the links have been handeled.

that way it does not matter how many links you have they will all be taken care of....the only problem is it wont work if you delete a link at the middle of the array, like if you delete link with the id 1, then none of then would work because when it looks for isset(1) it will say false

Posted: Sat May 29, 2004 8:06 am
by idotcom
hi,

I ended up using link_id as input name because the script was setting the file names as variable names and converting periods to underscores, plus, it should have been link_id from the start, its just smarter : )

So this is what I have so far...

Code: Select all

<?
if($_POST) 
{
	mysql_connect("$db_host", "$db_user", "$db_pass") or die ("DataBase connection error! Please try again later");

	foreach (array_keys($_POST) as $key) 
{ 
	$$key = $_POST[$key]; 

$link_id = $key;
$link_order = ${$key};

	$query="UPDATE menu SET link_order='$link_order' WHERE link_id='$link_id'";
}
	mysql_db_query($db_db,$query);
 
}
?>

This is what the input looks like.. populated in loop
              <input type="text" name="<?=$link_id?>" value="<?=$link_order?>" maxlength="3" size="3">
Using "Variable Variables" I managed to get the the link_id and link_order values together.

Now the really messed up part is that when I echo everything prints just fine, correct, and in order. But the query only updates the last link in the loop. What a pain...

I wish this unusual script would work already... :?

Any comments?

Thank you for your help :D