yii save dropdown checked state on page refresh - php

In yii save a dropdown checked state of "yes" when the page reloads, if that is chosen? It defaults to "No".. Thanks!!
getAttributeLabel('MULTI_PART_FORM')); ?>
<td class="type-text"><?php echo CHtml::dropDownList('MULTI_PART_PO','',array( 0=>'No',1=>'Yes'), array('onChange' => "$('#MULTI_PART_VIEW').show();")); ?></td>
</tr>
<tr id="MULTI_PART_VIEW" style="display:none">
<th><?php echo CHtml::encode($model->getAttributeLabel('MULTI_PART_VIEW')); ?></th>
<td><?php echo CHtml::activeTextField($model,'MULTI_PART_PO',array('size'=>120,'maxlength'=>64,'value'=>$model->MULTI_PART_PO)); ?></td>
<?php

//VIEW CODE
echo CHtml::beginForm(Yii::app()->createUrl("{controllername}/save",array(/*'{controller parameter}'=>{parameter value},*/)),'post',array('id'=>'form_id'));
echo CHtml::dropDownList('MULTI_PART_PO',$saved_dropdown_value,array( 0=>'No',1=>'Yes'), array('onChange' => "$('#MULTI_PART_VIEW').show();"));
//dropDownList(string $name, string $select="the selected value", array $data, array $htmlOptions=array ( ))
echo CHtml::endForm();
//CONTROLLER CODE (add function Save to accessRules array)
public function actionSave(/*{controller parameter}*/)
{
$model=new {Modelname};
if(isset($_POST["MULTI_PART_PO"]))
{
$model->multi_part_po = $_POST["MULTI_PART_PO"];
$model->save();
}
$this->render('{Viewname}',array(
'saved_dropdown_value'=>$model->multi_part_po,
));
}

Related

Why in my post is only one item? CakePHP 2.x

I have table in my view. This table is in form. I need send all items from table to my controller. I create form but he is sending only one item, last item...
Here is my table in View:
<?php echo $this->Form->create('Goodsandoffer');?>
<table id="GoodSandOffersTable" class="display" cellspacing="0" width="100%">
<thead><tr>
<th><?php echo $this->Paginator->sort('id','ID'); ?></th>
<th><?php echo $this->Paginator->sort('nazwa','Nazwa'); ?></th>
<th><?php echo $this->Paginator->sort('kodKreskowy','Kod EAN'); ?></th>
<th><?php echo $this->Paginator->sort('cenaSprzedazyBrutto','Cena sprzedaży brutto'); ?></th>
<th>Cena Promocyjna</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
<?php $x =0;?>
<?php foreach ($goods as $good): ?>
<tr>
<td><?php echo h($goodID= $good['Good']['id']);?></td>
<td><?php echo h($good['Good']['nazwa']);?></td>
<td><?php echo h($good['Good']['kodKreskowy']);?></td>
<td><?php echo h($good['Good']['cenaSprzedazyBrutto']);?></td>
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
</tr>
<?php $x = $x + 1 ;?>
<?php endforeach; ?>
</tbody>
</table>
<?php echo $this->Form->end(__('Dodaj')); ?>
Here is controller:
if ($this->request->is('post')) {
debug($this->request->data);
}
And when Im debug I got this:
\app\Controller\GoodsandoffersController.php (line 110)
array(
'Goodsandoffer' => array(
'promotionaloffer_id' => '3',
'good_id' => '20',
'cenaPromocyjna' => '3'
)
)
In array I have only last item from table. I need all. What Im doing wrong?
I'm not familiar with CakePHP myself but it looks like all your fields in your table rows have the same name (promotionaloffer_id, good_id, cenaPromocyjna):
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
In order to receive all values, you need to indicate them with array keys like so:
<?php echo $this->Form->input('promotionaloffer_id['.$good['Good']['id'].']',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id['.$good['Good']['id'].']',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna['.$good['Good']['id'].']', array('id' => 'cenaPromocyjna'.$x));?></td>
This would result in input names like
promotionaloffer_id[0], good_id[0], cenaPromocyjna[0]
promotionaloffer_id[1], good_id[1], cenaPromocyjna[1]
...
promotionaloffer_id[x], good_id[x], cenaPromocyjna[x]
Where x is the good-id.

Error in search and display records using yii framework

I am new in yii framework.I am doing seach recoeds in database and display in another page using yii framework.My controller is Sitecontroller.php,view pages are search.php and search_result.php. and my model is job.php.My error is
"urlencode() expects parameter 1 to be string, object given
C:\wamp\www\yii\framework\web\CUrlManager.php(440)".
My controller is Sitecontroler.php
<?php
class SiteController extends Controller
{
public function actionsearch()
{
$user_id = trim($_GET['id']);
$model = new Job ;
if(isset($_POST['Job']))
{
$model->attributes=$_POST['Job'];
$title=$_POST['Job']['title'];
$title=trim($title);
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
$number=count($model);
if($number>0)
{
$this->redirect($this->createUrl('site/search_result',array('title'=>$title)));
}
}
$this->render('search',array('model' =>$model));
}
public function actionsearch_result()
{
$title=$_GET['title'];
$model = new Job ;
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
$this->render('search_result',array('model' =>$model));
}
}?>
My view files-search.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>false,
'htmlOptions' => array(),
'clientOptions'=>array(
'validateOnSubmit'=>true
),
)); ?>
<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
<div class="row">
<?php echo $form->labelEx($model,'Keyword'); ?>
<?php echo $form->textField($model,'title'); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
view file-search_result.php
<div style="float:right;margin-right:285px;">
<h1>Search Jobs</h1>
<table width="200" border="1">
<tr>
<td>SI No</td>
<td>Title</td>
<td>Key Skill</td>
<td>Experince</td>
</tr>
<?php
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
?>
</table>
</div>
Any body help me?
There are problems in this line
$this->redirect(array('site/Search_result',array('model' =>$model)));
You should try this instead
$this->redirect($this->createUrl('site/search_result',array('parameter'=>'value')));
Note:- You cant send $model through URL as it is an object and that is why you are getting this error.
UPDATE
In your search_result.php
change this code
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
to this
<tr>
<td></td>
<td><?php echo $model->title; ?></td>
<td><?php echo $model->key_skills; ?></td
><td><?php echo $model->experience; ?></td
></tr>
Explaination
In this line
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
As you can see you have used find() for fetching the data. SO find() is going to return you a single row (hence a single object) and not an array of objects. If you want to fetch multiple records then you should use findAll()
So when using findAll() your code becomes
$model=Job::model()->findAll(array('select'=>'*',"condition"=>"title like '%$title%'",));
and then you can use the same code as you were using in the search_result.php file like
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
I think the problem may be, that you have a bad naming of your actions.
You should use camelCase:
public function actionSearch()
...
public function actionSearch_result()
this is the naming needed by Yii, so it can find your actions.
The redirect method expects an action and without this I suppose it cannot find it.
Also the redirect expect a controler/action route, that means in your case it should read:
$this->redirect(array('site/search_result','model' =>$model));
Please keep in mind that the function is actionSearch_result with a Capital S but the route itself, is site/search_result with a lowercase s

Wordpress Plugin Function Error?

I recently inherited and transferred a wordpress website that contains various custom plugins. Everything seems to be working correctly except one link in the admin section assocaited with a plugin. The page comes up blank when the link is clicked when it should list all bookings. There are no syntax errors.
I know this is a long shot, but I was wondering if anybody can notice any problems with the below function that handles the page? Any insight would be greatly appreciated.
/**
* List all bookings
*/
function admin_list_bookings($class_id = false, $wrapper=true) {
global $ae_EventManager;
// View single?
if (isset($_GET['view_booking']) && is_numeric($_GET['view_booking'])) { $this->admin_view_booking($_GET['view_booking']); return; }
// Delete posts?
if (isset($_POST['DeleteLogs']) && !empty($_POST['sel_bookings']) && is_array($_POST['sel_bookings'])) {
foreach ($_POST['sel_bookings'] as $delete_id) {
wp_delete_post($delete_id, true);
}
echo '<div class="updated"><p>Selected logs have been deleted.</p></div>'."\n";
}
// Load all bookings
$booking_log_type = 'private';
if (isset($_GET['logtype'])) $booking_log_type = 'any';
$bookings = get_posts(array(
'post_type' => $this->booking_type,
'numberposts' => -1,
'post_status' => $booking_log_type,
'post_parent' => (is_numeric($class_id) ? $class_id : false),
));
// Output
if ($wrapper) {
echo '<div class="wrap">'."\n";
echo '<h2>View Class Bookings</h2>'."\n";
echo '<form class="plugin_settings" method="post" action="'.esc_url($_SERVER['REQUEST_URI']).'">'."\n";
echo wp_nonce_field($this->nonce);
}
?>
<table class="widefat">
<thead><tr><?php if ($wrapper) { ?><th><input type="checkbox" id="check_all" /></th><?php } ?>
<th>Date</th><th>Class</th><th>Name</th><th># Seats</th><th>Booking Cost</th><th>Status</th></tr></thead>
<tbody>
<?php
foreach ($bookings as $booking) {
$booking->post_content = $this->__unserialize($booking->post_content);
$log = $booking->post_content;
// echo '<pre>'; print_r($booking); echo '</pre>';
$view_link = 'edit.php?post_type='.$ae_EventManager->course_type.'&page&view_booking='.$booking->ID;
?>
<tr>
<?php if ($wrapper) { ?>
<td class="ctrl"><input type="checkbox" name="sel_bookings[]" value="<?php echo $booking->ID; ?>" /></td>
<?php } ?>
<td><?php echo $this->format_time_ago($booking->post_date); ?></td>
<td><?php echo $log['course_name'].'<br />'.date('d-m-Y H:ia', strtotime($log['class_date'].' '.$log['class_time'])); ?></td>
<td><?php echo $log['tickets'][0]['firstname'].' '.$log['tickets'][0]['lastname']; ?></td>
<td><?php echo $log['number_seats']; ?></td>
<td><?php echo '$'.number_format($log['transaction_total'], 2); ?></td>
<td><?php echo $log['payment_method'].'<br />'.$log['eway_response']['ResponseMessage']; ?></td>
</tr>
<?php
}
?>
Just add a closing curly bracket } at the end of admin_list_bookings function

Codeigniter can't pass value to controller

i have views here
<?php echo form_open('c_kategorimaterial/tambah'); ?>
<table>
<tr>
<td>Kode Kategori Material / Jasa</td> //primary key of table
<td><?php $inkode=array('name' => 'kkmj', 'disabled' => 'disabled', 'value' => $nextval, 'class' => 'GUI'); echo form_input($inkode) ?></td>
<td class="error"> <?php echo form_error('kkmj'); ?></td>
//i make the form_input disabled, because the value is
//auto generated from the last ID of table in database
</tr>
<tr>
<td>Nama Material / Jasa</td>
<td><?php $innama=array('name' => 'nmj', 'class' => 'GUI'); echo form_input($innama) ?></td>
<td class="error"> <?php echo form_error('nmj'); ?></td>
</tr>
<tr>
<td></td>
<td><?php $intambah=array('name' =>'login','class' =>'button','value' => 'Tambah'); echo form_submit($intambah); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
</tr>
<?php echo form_close(); ?>
and it pass the value to the controller
function tambah()
{
$id = $this->input->post('kkmj');
$nama = $this->input->post('nmj');
$this->form_validation->set_rules('nmj','Nama','trim|required|min_length[2]|max_length[20]|xss_clean');
if($this->form_validation->run() == false)
{
$lastval = $this->m_admin->getlastval('KKMJ','ms_kategori_material','kode_kategori_material_jasa');
$data['nextval'] = $this->m_admin->gencode('KKMJ',3,$lastval);
$data['title'] = 'QB Tambah Kategori Material';
$this->load->view('head',$data);
$this->load->view('content/add_kategori_material',$data);
}
else
{
echo 'id adalah '.$id.' namanya adalah '.$nama; //$id is not printed
}
}
and then i fill the second field with 'water' and click the button. my page showing blank white screen with a text id adalah namanya adalah water
how come the ID is not printed ? how do i resolve this ?
Disabled elements do not get posted when you submit a form. If you need a field with unchangeable value, try to set it as read-only instead of disabled.
Because the field is disabled.
Use readonly attribute.

Update multiple users at once?

On my site, users can create teams (clans) and invite other users to join them.
I’m working on a page at the moment where team leaders can edit the “ranks” of each user in the team. These options are Founder, Captain, Member, Inactive and Kick (which will remove the user from the team altogether).
I’ve included a quick screenshot of how the page looks to help explain what I’m hoping to achieve.
http://cl.ly/E9Gb
So team leaders can go through and change the rank of each user and then hit update.
What would be the best way to go about this?
Here are some extracts from my code at present:
CONTROLLER:
if ($this->input->post('update_roster'))
{
$member_id = $this->input->post('user_id');
$member_rank = $this->input->post('member_rank');
$this->clan_model->update_roster($clan_id, $member_id, $member_rank);
}
MODEL:
public function update_roster($clan_id, $user_id, $rank)
{
$data = array(
'rank' => $rank
);
$this->db->where('clan_id', $clan_id);
$this->db->where('user_id', $user_id);
if ($this->db->update('clan_joined', $data))
{
return TRUE;
}
}
VIEW:
<h3>Roster</h3>
<?php
$member_rank = array(
'1' => 'Founder',
'2' => 'Captain',
'3' => 'Member',
'4' => 'Inactive',
'5' => 'Kick'
);
?>
<?php $attributes = array('id' => 'update_roster', 'class' => 'nice'); ?>
<?php echo form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<table width="100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Username</th>
<th>Rank</th>
</tr>
</thead>
<?php form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<?php foreach ($members AS $member) : ?>
<tr>
<td class="text-center"><?php echo avatar(32, $member->username, $member->avatar_uploaded); ?></td>
<td><?php echo $info->clan_tag; ?> / <?php echo $member->username; ?></td>
<td><?php echo form_dropdown('member_rank', $member_rank, $member->rank); ?></td>
</tr>
<?php echo form_hidden('user_id', $member->username); ?>
<?php endforeach; ?>
</table>
<?php echo form_submit('update_roster', 'Update'); ?>
<?php echo form_close(); ?>
I understand this code is wrong, as it's not working, but thought I would share what I have done so far.
On your HTML form change the dropdown and your hidden input to be arrays:
<td><?php echo form_dropdown('member_rank[]', $member_rank, $member->rank); ?></td>
and
<?php echo form_hidden('user_id[]', $member->username); ?>
Then $this->input->post('member_rank') and 'user_id' will each receive an array. You can loop through the arrays in PHP and call the UPDATE code for each iteration of the loop:
for ($i = 0; $i < count($member_id); $i++) {
$current_member_id = $member_id[$i];
$current_member_rank = $member_rank[$i];
// Call your update code.
}

Categories