$_POST multiple entries from a list

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

$_POST multiple entries from a list

Post by IGGt »

I have a php script that I run which queries a set of databases. At present all the possible databases are listed on a page (connections_array.php), along with their attributes similar to:

Code: Select all

$db1_array[] = array('server' => '172.x.x.x:3306',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'Master',
                             'sendmail' => '0',
                             'repeat' => '0',
                             'dbID' => '01'
                                           );
At the bottom of that page, I then group them together as I need a different set for different parts of the script.

Code: Select all

$master_array = array_merge	(	(array)$db1_array,
							(array)$db2_array,
							(array)$db3_array
							);
I then refer to these groups when I need them. However, in order to remove a database (if it is malfunctioning, or offline for any amount of time) requires me to manually edit this file. So I wanted to add a section to my config page where I could select which databases were in which groups, and then save this back to my config database for the script to refer to when it starts (thus enabling other people to remove databases in my absence).

But then I got stuck.

So far I have created a page that contains a form, that lists all the databases, with a tick box underneath.

Code: Select all

<form name="ConfigDBList" id="DBList" action="configChange.php" method="Post" >
<?php
include ('connections_array.php');  
print "SLAVE DATABASES (for check slave status queries)";
print "<table style=\"width:98%\">";
print "<tr>";
foreach($connections_array as $db) {
	$dbname = $db['database'];
	$dbid = $db['dbID'];
		print "<td>$dbname</td>";
	};
	
	print "</tr><tr>";
	
foreach($connections_array as $db) {
	$dbname = $db['database'];
	$dbid = $db['dbID'];
		print "<td><INPUT TYPE=\"checkbox\" NAME=\"$dbid\" VALUE=\"$dbname\"></td>"	;
	};
		
print "</tr>";
print "</table>";

print "</br>";

//REPEAT THIS 4 TIMES FOR THE 4 GROUPS I NEED

?>
<p class="submit"><input type="submit"/></p>
	</form>

My plan was to POST the result back to the config page, and save the result to the database in the format of "1,2,3,6,7,8" using the dbID for the connections_array to refer to.

But this is where I run into problems, as I can't see a way to loop the incoming POST statement correctly, avoiding the need to write 80 POST statements on the receiving page (currently 20 databases listed x 4 groups).
Plus the dbID would be used 4 times, so I don't think I could actually refer to it anyway? Making it unique in the form (e.g. "sl_$dbID" = sl_01) would prevent me using to refer back to the original Array.

Any help / thoughts would be greatly appreciated.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_POST multiple entries from a list

Post by AbraCadaver »

Make your checkboxes an array and make the value the name or id whichever one is more useful on the processing page:
[text]<input type="checkbox" name="db[]" value="1">
<input type="checkbox" name="db[]" value="2">[/text]
Then it is easy to loop through:

Code: Select all

foreach($_POST['db'] as $db) {
   //do something
}
Or if you just need a list to put in a query:

Code: Select all

$list = implode(',', array_map('mysql_real_escape_string', $_POST['db']));
You could even have a multi-dimensional array so that you have 4 groups with an array of dbs:
[text]<input type="checkbox" name="db[1][]" value="1">
<input type="checkbox" name="db[1][]" value="1">
<input type="checkbox" name="db[1][]" value="2">
<input type="checkbox" name="db[2][]" value="3">
<input type="checkbox" name="db[2][]" value="4">
<input type="checkbox" name="db[3][]" value="5">
<input type="checkbox" name="db[3][]" value="6">
<input type="checkbox" name="db[4][]" value="7">
<input type="checkbox" name="db[4][]" value="8">
[/text]
However, I would store all of the db details in the database with a column called active and then just retrieve the ones where active = 1. It would make this whole process a lot easier.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: $_POST multiple entries from a list

Post by IGGt »

I like the idea of putting it all the details in a database, and then loading them from there. That is definitely the best idea. Cheers.
Post Reply