Prbolem in view when refreshing page - php

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.

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 !

Magento static Blocks is not displaying properly.

I have created few static blocks just based on the category Id trying to display different blocks .
The problem being some times the block is displayed while some other times it is not .
I guess there is prob with code ? or the way magneto displays static blocks not sure ?
CODE:
<?php
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==14): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==15): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==16): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==18): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==19): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==86): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==25): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==13): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information')->toHtml();?>
<?php elseif($category->getId()==98): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_fbyz')->toHtml();?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('products_information_others')->toHtml();?>
<?php endif; ?>
</div>
<?php endif; ?>
please resolve my problem.
If you put this code in product view page then it it not properly way to get current category in product page..
Used Mage::registry("current_category") instead of Mage::getModel('catalog/layer')->getCurrentCategory();
If you come to category directly then you can get current category id product page.then you used to fetch current product categories ids.
$categoryIds = Mage::registry('current_product')->getCategoryIds();
if(Mage::registry("current_category"))
{
$category = Mage::getModel('catalog/category')
->load(Mage::registry("current_category")->getId());
}elseif(is_array($categoryIds )){
//multiple categories id of product. that way i was taken on list category
$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
}
elseif(!is_null($categoryIds)){
$category = Mage::getModel('catalog/category')->load($categoryIds);
}
else{
//no categoies
}

Loop through post array

Ive got a form which adds a text fields dynamically when i want to add more records to a one to many relationship table.. for example movie has more than one video.. i can get one record to add but cant seem to get the rest to work...
form --
<div class="row clone">
<?php echo $form->labelEx($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->textField($modelYoutubeVideo,'embed_code',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->labelEx($modelYoutubeVideo,'description'); ?>
<?php echo $form->textField($modelYoutubeVideo,'description',array('size'=>50,'maxlength'=>250)); ?>
<?php echo $form->error($modelYoutubeVideo,'description'); ?>
</div>
<?php
$this->widget('ext.widgets.reCopy.ReCopyWidget', array(
'targetClass'=>'clone',
));
?>
Controller --
if( isset( $_POST['YoutubeVideo']['embed_code'] ) ) {
for($i=0; $i<count( $_POST['YoutubeVideo']['embed_code'] ); $i++) {
$modelYoutubeVideo = new YoutubeVideo();
$modelYoutubeVideo->embed_code = $_POST['YoutubeVideo']['embed_code'][$i];
$modelYoutubeVideo->description = $_POST['YoutubeVideo']['description'][$i];
$modelYoutubeVideo->movie_id = $model->id;
$modelYoutubeVideo->save();
}
}

php codeigniter controller not performing action after post

I have a form that posts a search query to my 'search' controller:
<div id="search_box">
<?php echo form_open('search'); ?>
<?php echo form_input('searchvalue', 'search...'); ?>
<?php echo form_submit('submit', 'Search!'); ?>
<?php echo form_close(); ?>
</div>
In my search controller I have the following code:
public function index()
{
$page = 'search';
$category = 'search';
if($this->input->post('searchvalue')) {
redirect('search/query');
};
......
}
My problem is that it won't do the redirect. I have the form helper autoloaded. What can I do to solve this mystery. Is it a case for the batman?
In Codeigniter if you ask for a POST variable from the Input class it will return the value or FALSE if the value is empty (Personal Experience) or not found.
So Maybe instead put in a Hidden field with a Dummy value and just check for that on each submit. You can then perform validation and redirect. This will then work if the search query is provided or not.
And the ; after you if is not valid PHP.
For Example:
<div id="search_box">
<?php echo form_open('search'); ?>
<?php echo form_hidden('mysearchform', 'true'); ?>
<?php echo form_input('searchvalue', 'search...'); ?>
<?php echo form_submit('submit', 'Search!'); ?>
<?php echo form_close(); ?>
</div>
public function index() {
$page = 'search';
$category = 'search';
if($this->input->post('mysearchform') != FALSE) {
// Remember to Validate your Query
redirect('search/query');
}
}
Hope I understood that correctly.

Categories