Drop Down list in yii.? - php

I am facing hard times working with yii framework,The latest I ecounterd is while populating data from database in a drop down.i have tried few things including codes from the site,but no use,I have the following codes,Have a model named as PostJob.PHP.
Do i have to write a seprate function in model.?
view: postjob.php
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo CHtml::dropDownList('category', $model->title, $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
and in view section , I have given :
<?php
$list = CHtml::listData(PostJob::model()->findAll(array('jobpost','description')),'id','title');
?>
geting nothing.Pls Help.

you can do this:
<?php $list = CHtml::listData(PostJob::model()->findAll(),'id','title'); ?>
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model , 'category', $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>

Related

For each inside other for each on view MVC PHP

I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>

Codeigniter updating database with checkbox

i have a table named group where i have columns like admin,editor,user,guest they are all boolean.
i want to create a group using checkbox. so in my view i got checkbox like admin,editor,user,guest and a create button. When i click the create button i want the checked options to be true in database and unchecked remains false. how can i do that?
I am new in codeigniter.
<html>
<head>
<title>Create A group</title>
</head>
<body>
<?php echo form_open('group/create_group'); ?>
<div>
<?php echo form_label("Gruop Name"); ?>
<?php
$data=array(
'name'=>'group_name',
'placeholder'=>'enter group name'
);
?>
<?php echo form_input($data); ?>
</div>
<div>
<?php $data=array(
'name'=>'role[]',
'value'=>'1',
'type'=>'checkbox'
);
?>
<?php echo form_label('Admin'); ?>
<?php echo form_checkbox($data); ?>
</div>
<div>
<?php $data=array(
'name'=>'role[]',
'value'=>'2'
);
?>
<?php echo form_label('User'); ?>
<?php echo form_checkbox($data); ?>
</div>
<div>
<?php $data=array(
'name'=>'role[]',
'value'=>'3'
);
?>
<?php echo form_label('Editor'); ?>
<?php echo form_checkbox($data); ?>
</div>
<div>
<?php $data=array(
'name'=>'role[]',
'value'=>'4'
);
?>
<?php echo form_label('Others'); ?>
<?php echo form_checkbox($data); ?>
</div>
<div>
<?php $data=array(
'type'=>'submit',
'value'=>'Create'
); ?>
<?php echo form_submit($data); ?>
</div>
<?php echo form_close(); ?>
</body>
First
Set "false" as default value at all those fields.
Second
If you can set only one role per user then you should use radiobox instead checkbox
Third
Capture the parameters sent by the post.
Assuming that you created a route for group/create_group that goes to the data_submitted method
public function data_submitted() {
$data = array(
'role' => $this->input->post('role'),
//more info that you get from the post..
);
Now load your model and save it...
}
Put off [] and in your php file logic your can do : $this->input->post("role"); and get value. If you want to do multi-choice's checkbox your must change to an unique name and set value to true for each of them.
Have nice day !

Multiple inserts at same time in Yii

im trying to create 2 records from the same model, in 2 diferent tables, my tables are cruge_user and profile, The table profile (iduser ForeignKey) belongs to cruge_user (iduser PrimaryKey) . So im working with the profile model ($model) and i import the cruge_user model ($modelCruge), the problem comes when I save a new record, i get 7 records in the DB, i notice that I also have 7 input in my _form.php so maybe is the reason but cant find the problem.
Here my action:
public function actionCreate()
{
$model=new Users;
$modelCruge=new CrugeStoredUser;
if(isset($_POST['CrugeStoredUser'], $_POST['Users']))
{
$model->attributes=$_POST['Users'];
$modelCruge->attributes=$_POST['CrugeStoredUser'];
if ($modelCruge->save(false)){
$model->iduser = $modelCruge->iduser;
$model->save(false);
}
}
$this->render('create', array(
'model'=>$model,
'modelCruge'=>$modelCruge,
));
}
and here is my _form.php
<div class="row">
<?php echo $form->labelEx($modelCruge,'username'); ?>
<?php echo $form->textField($modelCruge,'username'); ?>
<?php echo $form->error($modelCruge,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($modelCruge,'email'); ?>
<?php echo $form->textField($modelCruge,'email'); ?>
<?php echo $form->error($modelCruge,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($modelCruge,'password'); ?>
<?php echo $form->textField($modelCruge,'password'); ?>
<?php echo $form->error($modelCruge,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'surname'); ?>
<?php echo $form->textField($model,'surname',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'surname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'rol'); ?>
<?php $data = array(
'1' => 'Padre',
'2' => 'Profesor',
); ?>
<?php echo $form->dropDownList($model,'rol', $data); ?>
<?php echo $form->error($model,'rol'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'idschool'); ?>
<?php echo $form->textField($model,'idschool'); ?>
<?php echo $form->error($model,'idschool'); ?>
</div>
<br/>
<div class="row buttons">
<?php echo CHtml::submitButton($modelCruge->isNewRecord ? 'Crear' : 'Guardar'); ?>
</div>
What i get in the DB when I submit is:
Table cruge_user:
iduser->1 , username->test, email->EMPTY, password->EMPTY...
iduser->2 , username->test, email->test#test.com, password->EMPTY...
iduser->3 , username->test, email->test#test.com, password->test...
iduser->4 , username->test, email->test#test.com, password->test...
iduser->5 , username->test, email->test#test.com, password->test...
iduser->6 , username->test, email->test#test.com, password->test...
iduser->7 , username->test, email->test#test.com, password->test...
in Table profile:
iduser->6 , name->test, surname->test, rol->1...
iduser->7 , name->test, surname->test, rol->1..
It creates 7 records in table cruge_user and 2 records in table profile :/
When its supossed to create only 1 record in table cruge:user and with the same iduser create a record in table profile with extra data.
Some help?

cake php: HTML links for certain id?

I'm new to CakePHP, I was wondering if there is a way to echo information from the database using a foreach loop but only have HTML links on images where the id is 1 & 7. What's the best way of achieving this?
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endforeach; ?>
<?php } ?>
This is how it looks in the veiw, my database in looks image data is stored like this:
suki-burberry-sq_500_500_90_s_c1.jpg
Would it be best to echo all the data individually without the foreach loop or could I write an if statement?
If you want only to get the 1 & 7 id you can do this, assuming that it is a predefined number. If it is dynamic or changeable you can do that in your controller.
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php if ($article['Post']['id']==1 || if ($article['Post']['id']==7) ): ?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php } ?>
Best way is to find from your Model only what you needed.
with the condition $conditions=array('Post.id'=>array(1,7));

DropDownList from database in YII

i am creating yii application first time, i have used following code to create dropdownlist in my view
echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll());
it show me error
include(Forms.php): failed to open stream: No such file or directory
C:\wamp\www\LearningYii\protected\views\codelookup_form.php(35):
<?php echo $form->textField($model,'ShortDesc',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'ShortDesc'); ?>
</div>
<div class="row">
<?php echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll()); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
$opts = CHtml::listData(Codelookup::model()->findAll(),'CodeLookupId','CodeDesc');
echo $form->dropDownList($model,'ParentCodeLookupId',$opts,array('empty'=>''));
This code is working
use CHtml::listData to convert the db object to list data:
Here ID , NAME is your table values which should be used in option value and option text.
<?php
$opts = CHtml::listData(Forms::model()->findAll(),'CodeLookupId','CodeLookupId');
//to check list
print_r($opts);
echo $form->dropDownList($model,'CodeLookupId',$opts);
?>
try hope this will help..
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php
$records = loaddataform::model()->findAll();
$list = CHtml::listData($records, 'id', 'username');
echo CHtml::dropDownList('loaddataform', $model,
CHtml::listData($records,
'a_id', 'a_name'),
array('empty' => '(Select a name)'));
?>
</div>
and in controller
public function actionloaddata()
{
$model = new loaddataform;
$models = loaddataform::model()->findAll(array('order' => 'a_name'));
$list = CHtml::listData($models, 'a_id', 'a_name');
$this->render('loaddata',array('model'=>$model));
}
This works correctly..it retrives name from database.

Categories