I have users in the admin module. Against each user row I have edit user option. when admin clicks on that edit user option. He will see edit user form with fields. Out of those form fields one of the filed is drop down which is user profile type.
Now from the database I will get an array of all the details about that user which admin editing called $user_details. But from the database what I will get is user profile id in the field name like this $user_details['profile_id'];
At the same time I had another array which maps profile id's with profile names. That is called as $profile
Now when admin clicks on edit first he needs to see the profile_name of that user before editing. And when he clicks on that drop down he needs to see what are the profile types available to changes.
I am not able to get idea how to do this.
Bu this is what I have tried
<label>Profile: <?php echo form_error('profile'); ?></label> <br />
<select class="styled" name="profile_id">
<?php foreach($profiles as $profile) { ?>
<option value="<?php echo $profile['profile_id']?>" <?php if ( set_value('profile_id') == $user_details['profile_id']) {?>selected="selected"<? } ?>><?php echo $profile['profile_name']?></option>
<?php } ?>
</select>
It is easy if you are going to use Codeigniter's builtin form_dropdown
$options = array();
foreach($profiles as $profile){
$options[$profile['id']] = $profile['profile_name'];
}
$select = isset($user_details['profile_id']) ? $user_details['profile_id'] : 0;
form_dropdown('profile_id',$options , $select , 'class= "styled" ');
It takes 4 parameters. 3rd and 4th are optional. 3rd is the key you want to be selected and 4th is used for other attributes like class , id etc. The first one is name and 2nd one is array.
For details see the Form Dropdown User guide.
Related
I inherited a YII based web application but I am not really familiar with Yii and don't find the solution yet. So if you can, please help me!
There is a form. In the form there is a dropdown list which get data from a db table. This table contains the name and the price of leases. The dropdown lists only the name.
<?php
$typeNames = array();
foreach(LeaseType::model()->findAll() as $lt) {
$typeNames[$lt->id] = $lt->name;
}
?>
<?php echo $form->dropDownListControlGroup($model, 'lease_type_id',
$typeNames,
array(
'empty'=>'Válassz!',
'onchange'=>'$("#model-payed").val($("#model-lease_type_id option:selected").text());',
)
); ?>
But I need to change the value of a specific input text field with the price of the selected lease.
<?php echo $form->textfield($model,'payed');?>
I can get the name of the lease but I can't set the value of the input for the price of the lease. This field has to be changable based on the customer payed all the price or just partial.
Other problem is that the name and price of the lease in different table than the value needed.
I hope all this are understandable because I am not fluent in English.
Thanks for any help!
Finally I found the solution, so share it maybe someone can use it.
In the views/_form.php I modified the dropdownlist:
<?php
echo $form->dropDownListControlGroup($model, 'lease_type_id', $typeNames, array(
'empty' => 'Válassz!',
'onchange' => '$("#model_comment").val($("#model_lease_type_id option:selected").$typeNames);',
//'onchange'=>'if($(this).val() == "no"){("#quantity").val("0"); }'
)
);
?>
I'm new to yii and I don't understand the extensions much
but I used this extension called jmultiselect2side because I'm trying to make a site where users could reserve stuff like apparatuses in the lab
Anyway, I need a code that would get the Selected Items and then display them in another page for viewing purposes
I haven't put anything in the controller but the name of my controller and model is Apparatus
Here is my view:
<?php
$model= Apparatus::model()->findByAttributes(array('ApparatusCode'=>'1'));
// complete user list to be shown at multiselect order by ApparatusCode
$Apparatus= Apparatus::model()->findAll(
array('order' => 'ApparatusCode'));
?>
<center>
<?php
$this- >widget('application.extensions.jmultiselect2side.Jmultiselect2side',array(
'model'=>$model,
'attribute'=>'ApparatusName', //selected items
'labelsx'=>'Available',
'labeldx'=>'Selected',
'moveOptions'=>false,
'autoSort'=>'true',
'search'=>'Search:',
'list'=>CHtml::listData( // available items
$Apparatus,
'ApparatusCode',
'ApparatusName'),
));
?>
please help as soon as possible :/
put all above elements in a form. Set action for the form. Submit the form then the action in which you are handling this submit request, you can write there
if(isset($_POST))
{
foreach($_POST['Apparatus']['ApparatusName'] as $name)
{
do what ever you want
}
}
$name will represent the each selected item
i work on a registration page in php mysql with yii. i created a registration form and create a auto increment registration number from table student_info in column student_id.when we register any student then it auto assign a registration number .but now i use registration form with above code ,which show a text box , but i want display registration number on that place.my code is ...
<div class="row">
<div class="row-left">
<?php echo $form->labelEx($info,'student_id'); ?>
<?php echo $form->textField($info,'student_id'); ?>
<span class="status"> </span>
</div>
on the place of textfield , what i use , by which it display my auto incement registration number. i am new please help me ?please anyone
Put this code into your model:
public function init() {
if ($this->isNewRecord) {
$this->student_id = $this->generate_code();
}
}
$this->generate_code() - this is your function to generate the registration number :)
Then in your view-template use something like this:
<? echo $info->student_id; ?>
Since the user registration number is a very important one and you probably need it all the time, I would recommend to store it at session when the user login. To do so, you only need the following line at your login component:
$this->setState('registration_number', $user->student_id);
Replace "$user" with the model you use to login.
Then, if you need to display this number just add:
echo Yii::app()->user->registration_number
in the place you want it to be displayed.
I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);
I have been trying to do display a custom field I created in the manage fields section of user accounts for nodes in addition to the profile page. The problem I am having with this code is that it will display the first field it finds and display that for every user, not the field for that particular user.
And ideas? I believe it's finding the first value in the array and not the value for the particular user in the array.
Here is m setup so far:
Added this to my template.php of my theme:
function mythemename_preprocess_node(&$vars) {
global $user;
$user = user_load($user->uid); // Make sure the user object is fully loaded
$team = field_get_items('user', $user, 'field_team');
if ($team) {
$vars['field_team'] = $team[0]['value'];
}
}
Then, added this to my node.tpl.php in order to display it on nodes.
if (isset($field_team) && !empty($field_team)) :
echo '$field_team.'</div>';
endif;
UPDATE:
Found my own aswer here:
http://drupal.org/node/1194506
Code used:
<?php
$node_author = user_load($node->uid);
print ($node_author->roles[3]);
print ($node_author->field_biography['und'][0]['value']);
?>
You can use drupal's 'Author Pane' module for that. Try this:
http://drupal.org/project/author_pane