I sent you guys a screenshot of error, the form is working properly the only issue is in "from_label()" if I comment it the input field is working properly without any error
<?php $attributes = array('id' => 'login_form','class' => 'form_horizontal'); ?>
<?php echo form_open($login_user, $attributes); ?>
<div class="from-group">
<?php echo from_label(); ?>
<?php echo from_input(); ?>
</div>
<?php echo form_close(); ?>
change
<?php echo from_label(); ?>
^^
<?php echo from_input(); ?>
^^
to
<?php echo form_label(); ?>
<?php echo form_input(); ?>
Reference
form_label([$label_text = ''[, $id = ''[, $attributes =
array()]]])
form_input([$data = ''[, $value = ''[, $extra = '']]])
FYI:
form_label() need the first argument as run smoothly. echo
form_label('What is your Name'); (even with form-input)
Even your class from-group should be form-group
Related
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
}
?>
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'); ?>
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.
I am retrieving a column from my database and storing that values in an array in my controller.
I am passing that array to the view page, and displaying that values to the user.
All things upto this are working fine.
But the problem is when I am refreshing the page its not showing the values obtained by the controller from the model and just showing blank page..\
I also tried to store the value in a session and use that value but that doesn't seem to work for me.. :(
here is my controller code :-
function full_post_view(){
$data= array(
'ticket_id' => $this->input->post('ticket_id')
);
$this->session->set_userdata($data);
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
// $post_content = $this->session->userdata('post_content');
echo $ticket_id;
$this->load->model('helpdesk_model');
$all_comments = $this->helpdesk_model->fetchComments($ticket_id);
$is_post_closed = $this->helpdesk_model->fetchPostStatus($ticket_id);
if($all_comments->num_rows > 0) {
foreach ($all_comments->result() as $comments_value) {
$comments = $comments_value->comments;
}
$count=1;
Template::set('all_comments',$all_comments);
Template::set_view('helpdesk/full_post_view');
}
else {
$count=0;
Template::set('post_content',$this->input->post('post_content'));
}
Template::set('is_post_close',$is_post_closed);
Template::set('ticket_id',$ticket_id);
Template::set('is_post_closed',$is_post_closed);
Template::set('post_content',$post_content);
Template::set('count',$count);
Template::set('is_post_closed',$is_post_closed);
Template::render();
}
here is my view :-
<?php $post=Template::get('post_content'); ?>
<?php $ticket_id=Template::get('ticket_id'); ?>
<?php $is_close = Template::get('is_close'); ?>
<h4>Your Problem :- </h4>
<?php echo $post; ?>
<hr/>
<?php foreach ($is_post_closed->result() as $value) {
$is_close = $value->is_close;
} ?>
<?php if(Template::get('count') > 0) : ?>
<?php foreach($all_comments->result_array() as $commentsRow) : ?>
<?php echo word_wrap($commentsRow['comments'],15); ?>
<?php echo " by->"; ?>
<?php echo $commentsRow['username']; ?>
<?php echo $commentsRow['role_name']; ?>
<hr/>
<?php endforeach; ?>
<?php else : ?>
<br/>
No Comments Yet
<?php endif; ?>
<?php if($is_close == 0 ) : ?>
<?php echo form_open('helpdesk/newComment'); ?>
<?php echo form_hidden('ticket_id',$ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_textarea('comment_from_user'); ?>
<?php echo form_submit('submit', 'Comment '); ?>
<?php echo form_close(); ?>
<?php endif; ?>
<?php if($is_close==0) : ?>
<?php echo form_open('helpdesk/closePost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_submit('submit','close post'); ?>
<?php echo form_close(); ?>
<?php else : ?>
<?php echo form_open('helpdesk/reopenPost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_submit('submit','Reopen post'); ?>
<?php echo form_close(); ?>
<?php endif; ?>
Edit :
I want to ask that how to load the database content again to the view when user refreshes the page.
Edited my Controller and view code
When you refresh the page, do you resend the form ? I'm asking this because from your controller code, it looks like $ticket_id is fetched from _POST and you use pass this variable to the model. If you don't resend the form, the $ticket_id is null and nothing is fetched from DB.
Edit:
Replace in your controller:
$ticket_id = $this->input->post('ticket_id');
with:
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
and make sure on each request ticket_id exists at least in one from the two possibilities (POST and SESSION)
Solved :)
the problem was when I was refreshing the page, ticket_id was set to 0 and that value was passed as a post .
So the session value was the new ticket_id i.e., 0 and so it was displaying nothing..
I did this:-
if(isset($_POST['ticket_id'])) {
$data= array(
'ticket_id' => $this->input->post('ticket_id'),
'post_content' => $this->input->post('post_content')
);
$this->session->set_userdata($data);
}
$ticket_id =$this->session->userdata('ticket_id');
$post_content =$this->session->userdata('post_content');
i.e., I am storing ticket_id in session when post in set i.e., for the first time. When page is refreshed next time ticket_id post is not set and data is taken from session.
I'm developing a website using Codeigniter. The structure site is simple, I have dynamic menu and content retrieving DB in Model. Then assign a method to call both of them in Controller.
I have that weird error message keeps coming back saying the array is not defined.
I can get tabPhotos, it contains datas.
View:
<div id="container">
<div id="photos">
<?php echo "Count: ". count($tabPhotos); ?>
<ul>
<?php if (!is_array($tabPhotos) || empty($tabPhotos)) :?>
<?php return null; ?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
Controller:
public function loadMenu($file) {
$data['tabMenuItems'] = $this->qdphoto_model->getAllMenuItems();
$this->load->view($file, $data);
}
public function loadCategoryPage($file, $category='Book 1') {
$data['tabPhotos'] = $this->qdphoto_model->getAllPicturesByCategory($category, 'url, model');
$this->load->view($file, $data);
}
Am I reading this correctly? if $tabPhotos IS NOT an array OR it is and it happens to be EMPTY then return null and continue to run the foreach loop with a mysterious endif following?
<?php if (!is_array($tabPhotos) || empty($tabPhotos)) :?>
<?php return null; ?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
Shouldn't it read this:
<?php if (is_array($tabPhotos) && !empty($tabPhotos)) :?>
<?php foreach ($tabPhotos as $item) : ?>
<?php echo "Model: " . $item->url; ?>
<?php endforeach; ?>
<?php endif; ?>
no need for that return null statement.
Also, I believe you can return views as strings. If you want to load more than one view you can certainly load any fragment of your view as a string...
$string = $this->load->view($file, $data, true);
Please note that you should review this:
http://codeigniter.com/user_guide/general/views.html
See bottom. It says how to return as string, except that I am used to slightly different syntax thanks to custom controllers and such. Perhaps this example I provided is not accurate.