Have an account? Sign in
Login  Register  Facebook
How to pass values from array into mysql
Updated question to my earlier request.

my original code is this
<tr>
    <th>
        <label for="user_level">
         User Level: *  <?php echo isset($valid_user_level) ? $valid_user_level : NULL; ?>
        </label>
    </th>
</tr>
<td>
    <select name="user_level" id="user_level" class="sel">
        <option value="">Select one…</option>
        <option value="1">User</option>
        <option value="5">Admin</option>
    </select>
</td>
this give me the option to select one of choice from the drop down menu i.e. user and when user is selected and the submit button is pressed this will insert the value 1 into the database which will when the user logs in tell the system that they are are normal user. I want to change the code to the following
<tr>
	<td>
        <select name="user_level" id="user_level" class="sel">
        <option value="">Select one…</option>
        <?php
    		if(!empty($level)) {
    			foreach($level as $value) {
    				echo "<option value='{$value}'";
    				echo getSticky(2,'user_level',$value);
    				echo ">{$value}</option>";
    			}
    		}
    	?>
        </select>
    </td>
</tr>
With this being my array query
$level = array('User','Admin');
How can I pass the values of 1 for user level and 5 for admin in this code so when the user is selected it inouts 1 into the database?
Started: September 22, 2011 Latest Activity: September 22, 2011 php mysql
4 Answers
The foreach loop has to keep the code similar to this code to display the sticky form fields ( the users choice incase he/she presses the submit button acidently)

<?php
if(!empty($level)) {
foreach($level as $value) {
echo "<option value=\"{$value}\"";
echo getSticky(2,'user_level',$value);
echo ">{$value}</option>";
}
}
?>

I think the original array is ok

$level = array(
1 => 'User',
5 => 'Admin'
);

Just the value is not being passed tot the mysql query.

foreach($level as $key => $value) {
echo "<option value=\"$key\">$value</option>";
}
the above is the original foreach loop statement you provided I just need to have the other echo fields displayed also before I close the "option".

the original statement echos the following code also
echo getSticky(2,'user_level',$value);

than closes the loop with
echo ">{$value}</option>";

Thanks

Posted: qakbar
In: September 22, 2011

TRY:
$level = array(
   0 => array('level'=> 5 ,'title' => 'User'),
   1 => array('level'=> 1 ,'title' => 'admin'),
);
and
foreach($level as $val) {
    echo "<option value='".$val['level']."'>".$val['title']."</option>";
}

Posted: MacOS
In: September 22, 2011

I have followed your code but the values are not being added to the database.

I think the problem is in the code for the select box.

my original code is:
<?php
	if(!empty($level)) {
		foreach($level as $value) {
			echo \"<option value=\"{$value}\"\";
			echo getSticky(2,\'user_level\',$value);
			echo \">{$value}</option>\";
		}
	}
?>
I have tried to amend it to the following code but no luck:
<?php
	if(!empty($level)) {
		foreach($level as $key => $value) {
			echo \"<option value=\"{$value}\"\";
			echo getSticky(2,\'user_level\',$value);
			echo \">{$value}</option>\";
		}
	}
?>

I need to keep this code similar to keep the sticky form fileds to display the selection.

Posted: qakbar
In: September 22, 2011

in general to make user or admin you have to make just one field in the user table structure
like manager and its value 0 or 1 | 0 mean user 1 mean admin
and use in any page
$mananger = 0;//i\'m got it as an example | you will get it from the mysql
if(mananger){
    echo \'iam the manager\';
}else{
    echo \'iam just user\';
}
this will return iam just user
$mananger = 1;//i\'m got it as an example | you will get it from the mysql
if(mananger){
    echo \'iam the manager\';
}else{
    echo \'iam just user\';
}
this will return iam the manager any way i think you know that the thing you want to know is Redefine your array to set both the key and values:
$level = array(
   1 => \'User\',
   5 => \'Admin
);
then do
foreach($level as $key => $val) {
    echo \"<option value=\\\"$key\\\">$val</option>\";
}
PHP won\'t know that an Admin user is \'5\' and regular User is \'1\' - you have to tell it, and this is the easiest method.

Posted: MacOS
In: September 22, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer