Yii, Looping form fields for Tabular Input - php

I am using the Yii Framework 1.1.17, and have generated three models:
Question, AnswerOption and Response.
The relationships:
Table: Question (list of questions)
id
text
Table: AnswerOption (list of possible answers, associated with question)
id
question_id
text
Table: Response (question and selected answer collector)
id
question_id
answer_option_id
text
I am trying to create a form, and admittedly collect answers for all possible questions.
File: ResponseController
public function actionCreate()
{
// load all questions and with it the possible answer Options
$questions = Question::model()->findAll();
// get number of questions
$count = Question::Model()->count();
$model = array();
$i = 1;
while ($i <= $count) {
$model[] = Response::model();
$i++;
}
if (isset($_POST['Response'])) {
//
}
$this->render('create', array(
'model' => $model,
'questions' => $questions,
));
}
This is the area that I am having trouble with:
File: response/_form
<?php foreach($questions as $i=>$question): ?>
<?php echo CHtml::activehiddenField($question,"[$i]id"); ?> <?php echo $question['text']; ?>
<?php $options = CHtml::listData($question->answerOptions, 'id', 'text');?>
<?php echo CHtml::activeDropDownList(AnswerOption::model(), "[$i]text", $options, array('empty' => 'Select answer...')); ?>
<?php endforeach; ?>
I may have populated my questions and possible answers, but I need to validate and save the results in $model.
It seems like I cannot find a way to efficiently work this out. Can someone please guide me?

I managed to resolve my "loop" problem by:
File: response/_form
<?php $questions = Question::model()->findAll(); ?>
<?php foreach ($questions as $j=>$question): ?>
<div class="row">
<?php echo $form->labelEx($model["$j"], "[$j]question_id"); ?>
<?php echo $form->hiddenField($model["$j"], "[$j]question_id", array('value' => $question["id"])); ?>
<?php echo $question['text']; ?>
</div>
<div class="row">
<?php $options = CHtml::listData($question->answerOptions, 'id', 'text');?>
<?php echo $form->labelEx($model["$j"], "[$j]answer_option_id"); ?>
<?php echo $form->dropDownList($model["$j"], "[$j]answer_option_id", $options, array('empty' => 'Select answer...')); ?>
</div>
<?php endforeach; ?>
Hopefully, this would come in handy for someone, someday.

Related

Creating zend form element array with same name (ZF3)?

Since i've to position checkboxes in design I'm not using MultiCheckbox and using Checkbox instead in zend. I've seen some zf1 solutions but didnt found any zf2 or zf3 solutions.
My Php Code
$languages = new \Zend\Form\Element\Checkbox('languages[]');
$languages->setLabel('English');
$languages->setValue('1');
This produce the following output
<?php
echo $this->formRow($form->get('languages[]'));
//It produce the following
?>
<input type="checkbox" name="languages[]" value="1" checked="checked">
How can I add more items with name "languages[]" without writing direct HTML code ?
You can use a Form collection for this:
1) In your form:
use Zend\Form\Element\Collection;
...
$languages = new \Zend\Form\Element\Checkbox();
$languages->setLabel('English');
$languages->setValue('1');
$languages->setName('language');
$collection = new Collection();
$collection->setName('languages');
$collection->setLabel('Language Collection');
$collection->setCount(2);
$collection->setTargetElement($languages);
$collection->populateValues(array(
1, 0
));
$this->add($collection);
2) Do not forget to prepare your form in your controller action:
$form->prepare();
3) Finally, in your view, get all elements of the collection and render them separately:
<?php $elements = $form->get('languages')->getElements(); ?>
<?php //echo $this->formCollection($form->get('languages')); ?>
<?php echo $this->formRow($elements[0]); ?>
<?php echo $this->formRow($elements[1]); ?>
You can use MultiChebox
https://framework.zend.com/manual/2.2/en/modules/zend.form.element.multicheckbox.html
It works similar to radio form element. Your example implementation
use Zend\Form\Element;
$languages = new Element\MultiCheckbox('languages');
$languages->setLabel('Used languages');
$languages->setValueOptions([
'en_US' => 'english',
'de_DE' => 'german',
'pl_PL' => 'polish',
]);
$languages->setValue('en_US'); //default value; you can use array
My solution (Method 2)
Method 1:
Give name as "language[1]" instead of "language[]". By using this I can call the elements in view seperately.
Creating form.
$language1 = new \Zend\Form\Element\Checkbox('language[1]');
$language2 = new \Zend\Form\Element\Checkbox('language[2]');
$form = new Form('Set');
$form->add($name)
->add($language1)
->add($language2)
->add($submit);
In view file
<div><?php echo $form->get('language[1]');?></div>
<div><?php echo $form->get('language[2]');?></div>
Edit: Method 2
Using fieldset
//Create form
$languages = [
['id' => 1, 'language' => 'English'],
['id' => 2, 'language' => 'Malayalam'],
] ;
$fieldSet = new \Zend\Form\Fieldset('languages') ;
foreach( $languages as $one ) {
$c = new \Zend\Form\Element\Checkbox($one['id']);
$c->setLabel($one['language']) ;
$fieldSet->add($c) ;
}
//Add collection of checkboxes to form
$form->add($fieldSet) ;
In view file
<?php
$language = $form->get('languages') ;
?>
<div class="form-group row">
<label class="control-label col-sm-2" >Choose Languages</label>
<div class="col-sm-10">
<?php foreach($language as $one ) { ?>
<?php echo $this->formCheckbox($one); ?> <span> <?php echo $this->formLabel( $one ) ; ?> </span>
<?php echo $this->formElementErrors($one); ?>
<?php } ?>
</div>
</div>

Can't update data using Mysql and Codeigniter

i want to update my array data from table monitordata, but the data wont update i dont know where's the problem. there's no error in this code too :(
this is my controller
public function ubah($id) {
$data_lama = $this->monitor_m->get($id);
$this->data->tglmonitor = $data_lama->tglmonitor;
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_judul('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
public function ubahku($id) {
$id = $this->input->post('idMonitor_data');
if($this->input->post('idinven')!=NULL){
$idMonitor = $this->input->post('idMonitor');
$kondisi = $this->input->post('kondisi');
$nobrg = $this->input->post('nobrg');
$keterangan = $this->input->post('keterangan');
$kdinven = $this->input->post('kdinven');
$idinven = $_POST['idinven'];
for($i = 0; $i < count($idinven); $i++){
$data_detail = array(
'idMonitor' => $this->input->post('idMonitor'),
'idinven'=> $idinven[$i],
'kdinven'=> $kdinven[$i],
'nobrg'=> $nobrg[$i],
'kondisi'=> $kondisi[$i],
'keterangan' => $keterangan[$i]);
//print_r($data_detail);
$where = array('idMonitor_data' => $id);
$this->monitordata_m->update_by($where,$data_detail);
}
} redirect('monitorcoba');
}
This is my model monitordata_m
class Monitordata_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor_data','idMonitor_data');
}
This is MY_Model model i put in core folder.
public function update_by($where = array(), $data = array()) {
$this->db->where($where);
if ($this->db->update($this->table,$data)){
return true;
}
return false;
}
And this is my view
<?php echo form_open(site_url("monitorcoba/ubahku"),'data-ajax="false"'); ?>
<input data-theme="e" style="float: right;" data-mini="true" data-inline="false" data-icon="check" data-iconpos="right" value="Simpan" type="submit" />
<div data-role="collapsible-set" data-mini="true">
<?php foreach ($detail as $items): ?>
<div data-role="collapsible">
<?php echo form_hidden('idMonitor_data', $items['idMonitor_data'] ); ?>
<?php echo form_hidden('idMonitor', $items['idMonitor'] ); ?>
<h4><?php echo '[ '.$items['kdinven'].' ] '.$items['namabrg'] ?> </h4>
<?php echo form_hidden('kdinven', $items['kdinven'] ); ?>
<?php echo form_hidden('idinven', $items['idinven'] ); ?>
<div data-role="controlgroup">
<?php echo form_label ('Kondisi : ');
echo " <select name='kondisi' data-mini='true'>
<option value=".$items['kondisi'].">".$items['kondisi']."</option>
<option value=''>--Pilih--</option>
<option value='Baik'>Baik</option>
<option value='Rusak'>Rusak</option>
<option value='Hilang'>Hilang</option>";
echo "</select>";
echo form_input('keterangan',#$keterangan,'placeholder="Masukan Keterangan Tambahan"','class="input-text"');
?>
<?php echo form_close(); ?>
even if i use update_by it doesnt work. it's been 2 weeks and i have no clue :( i've tried all of the answer that i found in google but still.. so please help me.
This is the DATABASE result and POST_DATA for method ubahku
You have defined a method named update_by, but you are calling $this->monitordata_m->update($id,$data_detail);. Definitely it should not work. please call $this->monitordata_m->update_by($id,$data_detail); from your controller & check what will happen.
Firstly, Please correction $this->monitordata_m->update($id,$data_detail); to $this->monitordata_m->update_by($id,$data_detail); because your function name is update_by in your monitordata_m model.
Secondly, in your monitordata_m model update_by function have 2 param like $where = array() $data = array(), $where is a array but you calling in controller only $id. Your $id is not array. $where is like that $where = array('id' => $id) //id is where field name from db table
So, ubahku($id) method in your controller call $where in update_by function:
$where = array('id' => $id); // 'id' means "where field name"
$this->monitordata_m->update_by($where,$data_detail);
So, thank you so much for everyone who answer my question. so the problem was when i update the data, system only detect "kondisi[]" and "keterangan[]" as an array because i use this "[]" for both of it, so i just have to add "[]" in the end of every name in html form / views. so system will detect every input as an array. i hope you understand what i'm saying, sorry for my bad english. thank you this case is closed :)

Yii Json to Array dropdownlist

I have a json like this:
{"response":{"count":7,"list":[{"kdTeacher":"001","nmTeacher":"Eulis Irma"},{"kdTeacher":"002","nmTeacher":"Ni Wayan"},{"kdTeacher":"003","nmTeacher":"Dwi Widi"}]},"metaData":{"message":"OK","code":200}}
I want the teacher name and the teacher code display in dropdownlist.
In my controller like this:
$model=new Teacher;
$paises = file_get_contents(Yii::getPathOfAlias('webroot.assets') .
DIRECTORY_SEPARATOR . "teacher.json");
$jsondecode = CJSON::decode($paises, true);
$temp = array();
foreach ($jsondecode as $key => $value) {
$temp[] = $value;
}
$this->render('techer', array(
'data'=>$temp,
'model'=>$model,
));
}
and in view
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->dropDownList($model,'nama',CHtml::listData($data,'kodeTeacher','nmTeacher')); ?>
<?php echo $form->error($model,'name'); ?>
</div>
id
Problem is that you can't get teacher data, because you use wrong data for foreach loop. Teacher code and name are stored at ['response']['list'] index of decoded json. Take a look:
$model=new Teacher;
$paises = file_get_contents(Yii::getPathOfAlias('webroot.assets') .
DIRECTORY_SEPARATOR . "teacher.json");
$decodedJson = json_decode($paises, true);
$temp = array();
//check if list of teachers is available
if(isset($decodedJson['response']) && isset($decodedJson['response']['list']))
{
//use foreach on ['response']['list'] index - here are teachers data stored
foreach($decodedJson['response']['list'] as $teacher)
$temp[$teacher['kdTeacher']] = $teacher['nmTeacher'];
}
$this->render('techer', array(
'data'=>$temp,
'model'=>$model,
));
Working example of get teachers data from JSON: CLICK!!!
And in view (now you don't need to use CHtml::listData method):
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->dropDownList($model,'nama',$data); ?>
<?php echo $form->error($model,'name'); ?>
</div>

Yii multi model with HAS_MANY Tabular form

I have been trying to find a solution to something that I believe is rather simple? What I would like to do is create a tabular form to collect some records for a survey.
Table: survey_record
id
meter_id
status_id
create_time
create_user
Table: survey_inspection
id
survey_record_id (FK)
bay_id
bay_usage_id
So table survey_inspection HAS_MANY inspections from survey_record.
Assuming an inspector is doing a survey for meter_id = 1001, the user identifies the status_id of the meter then, they have to fill in 4 inspection entries, as 1001 has 4 bays.
To generate the 4 bays I simply created a for loop. Which works fine.
View: _form.php
...
$meter=Meter::model()->findByPk($model->meter_id);
$bays = count($meter->bays); //retrieves the number of inspections required
<?php echo $form->dropDownListRow($model,'status_id',$model->getStatusId(),array('prompt'=>'Select Status ...')); ?>
...
<?php for ($i = 1; $i <= $bays; $i++): ?>
<?php echo $form->textFieldRow($inspection, "[$i]bay_id", array('readonly'=>true, 'value'=>"$i", 'class'=>'span1')); ?>
<?php echo $form->dropDownListRow($inspection, "[$i]bay_usage_id", $inspection->getUsageId(), array('prompt'=>'Bay Status ...') ); ?>
<?php endfor; ?>
...
However when I submit the form I am only receiving two (not four) results and I can't seem to display the validated fields correctly. So here's the famous controller file:SurveyRecordController.php
public function actionCreate($survey_id, $meter_id)
{
$model=new SurveyRecord;
$inspection = new SurveyInspection;
$model->survey_id = (int)$survey_id;
$model->meter_id = (int)$meter_id;
if(isset($_POST['SurveyRecord'], $_POST['SurveyInspection']))
{
$model->attributes=$_POST['SurveyRecord'];
$valid= $model->validate();
$i=1;
foreach($_POST['SurveyInspection'][$i] as $inspection)
{
$inspection = new SurveyInspection;
$inspection->bay_id =$_POST['SurveyInspection'][$i]['bay_id'];
$inspection->bay_usage_id =$_POST['SurveyInspection'][$i]['bay_usage_id'];
$valid= $inspection->validate() && $valid;
echo '<br/><br/><br/><pre>';
print_r($_POST['SurveyInspection'][$i]);
echo '</pre>';
if($valid)
{
if($model->save(false))
{
$inspection->survey_record_id = $model->id;
$inspection->save(false);
$this->redirect(array('/meter'));
}
}
$i++;
//$inspection->attributes=$_POST['SurveyInspection'][$i];
}
}
$this->render('create',array(
'model'=>$model,
'inspection'=>$inspection,
));
}
I have a funny feeling that I may not be doing the foreach loop correctly as when I view the array $_POST['SurveyInspection'][$i] I am only returned with two entries when there should be four.
Some information that may be helpful:
PHP version: 5.4.14
Yii version: 1.1.13
PostgreSQL version: 9.1.9
Thank you kindly :)
To my knowledge i think for the validation u have bypassed the default validation by passing
false to save method (ex : $model->save(false)) and u are also using yii's validate function . I think u try removing the false parameter and just use save() for the validation issue
The foreach loop is wrong. You can make it proper and a bit cleaner:
foreach($_POST['SurveyInspection'] as $i => $value)
{
$inspection = new SurveyInspection;
$inspection->bay_id = $value['bay_id'];
$inspection->bay_usage_id = $value['bay_usage_id'];
...
$i++ is not needed, as foreach will loop every element.
I've taken a completely different approach, valuable suggestions are always welcome :)
Hope this will be helpful for some of you.
Controller:
public function actionCreate($survey_id, $meter_id)
{
$bays = $this->getBayCount($meter_id);
for ($i=1;$i<=$bays;$i++)
{
$model=new SurveyRecord;
$model->survey_id = (int)$survey_id;
$model->meter_id = (int)$meter_id;
${'inspection_'.$i} = new SurveyInspection($i);
if(isset($_POST['SurveyRecord'], $_POST["SurveyInspection"][$i]))
{
$model->attributes = $_POST['SurveyRecord'];
${'inspection_'.$i}->attributes = $_POST["SurveyInspection"][$i];
echo '<br/><br/><br/><pre>';
print_r(${'inspection_'.$i}->attributes);
echo '</pre>';
}
}
for ($i=1;$i<=$bays;$i++)
{
${'inspection_'.$i} = new SurveyInspection($i);
$inspection['inspection_'.$i] = ${'inspection_'.$i};
}
$this->render('create',array(
'inspection'=>$inspection,
'model'=>$model
));
}
View:
...
<div class="control">
<?php echo $form->dropDownListRow($model,'status_id',$model->getStatusId(),array('prompt'=>'Select Status ...')); ?>
<?php
$meter=Meter::model()->findByPk($model->meter_id);
$bays = count($meter->bays);
?>
<?php for ($i=1; $i<=$bays; $i++): ?>
<table>
<tr>
<td><?php echo $form->textFieldRow($inspection["inspection_$i"], "[$i]bay_id",
array('readonly'=>true, 'value'=>"$i", 'class'=>'span1')); ?></td>
<td><?php echo $form->dropDownListRow($inspection["inspection_$i"], "[$i]bay_usage_id", $inspection["inspection_$i"]->getUsageId(),
array('prompt'=>'Bay Status ...') ); ?></td>
</tr>
</table>
<?php endfor; ?>
</div>
...
Credit: http://yiiblog.info/blog/index.php/post/108/Rename+ACtiveRecord+$_POST+array's+Name+to+update+multimodels
Note: There's still some work to be done on the view/controller (doesn't seem to be able to select on $_POST for my dropdowns on second model), will post once I have fixed it.
Thanks for your help #PeterM and #Ninad for your input.

Zend Pagination - Seems Not to be limiting number of results on each page

I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.
This the query in my model
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
$query = $db->query($sql);
while ($row = $query->fetchAll()) {
$result[] = $row;
}
return $result;
}
This is code in my controller
function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
Zend_Loader::loadClass('newsArticles');
$allNews = new newsArticles();
$this->view->allNews = $allNews->getAllnews();
//die (var_dump($this->view->allNews));
$data = $this->view->allNews;
// Instantiate the Zend Paginator and give it the Zend_Db_Select instance Argument ($selection)
$paginator = Zend_Paginator::factory($data);
// Set parameters for paginator
$paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1 <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
$paginator->setItemCountPerPage(1);
$paginator->setPageRange(4);
// Make paginator available in your views
$this->view->paginator = $paginator;
//die(var_dump($data));
}
Below is the view that builds the paginator,
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
Next >
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
And finally the code the makes up my view
<div id="rightColumn">
<h3>Latest News</h3>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<?php
foreach ($item as $k => $v)
{
echo "<li>" . $v['title'] . "</li>";
}
?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?>
</div>
I will be greatful for any help you can give me.
Thanks
Sico
$paginator should be set to 4 as you want to show only 4 records at a time :
$paginator->setItemCountPerPage(4);
So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.
Try this:
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
return $db->fetchAll($sql);
}
You sould not pass values to paginator if you want it to limit your query.
There is a code will work for you
$usersTable = $this->getTable();
$registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);

Categories