Yii2 : Vote for a Post in AJAX or Pjax - php

I'm a begginer in Yii2 framework.
I work on a forum :
In the forum/posts method, there is a list of posts about the topic.
Each Post have a score which I want up and down in AJAX.
In my view posts.php, I open a Pjax block :
<?php Pjax::begin(); ?>
Votes : <?= $val['score'] ?>
<?= Html::a('+', ['/post/voteup','id'=>$val['id']]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$val['id']]) ?>
<?php Pjax::end(); ?>
In my PostController :
public function actionVoteup($id){
//Update request
$postRepo=new PostRepository();
$postRepo->vote('plus', "id=$id");
$post=$postRepo->getAll("id=$id");
return $this->renderAjax('vote', ['id'=>$id, 'score'=>$post[0]['score']]);
}
You can see I return the Vote.php view in Ajax, same code Pjax.
<?php Pjax::begin(); ?>
Votes : <?= $score ?>
<?= Html::a('+', ['/post/voteup','id'=>$id]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$id]) ?>
<?php Pjax::end(); ?>
The update request is OK but I have some problems/questions :
Ex : I want to up the 2nd post score, I click, OK, I click a second time, the part of view which is refresh is the 1st Post score (but in database, it's the good score updated).
I think the problem is about my part of view that I return in my actionVoteup().
Should I return forum/posts or post/vote ?
When I click in the link, my URL is : post/voteup ; how can I return in the original URL forum/posts ?
I don't really understand how works Pjax, and I didn't find good examples about its utilisation.
Thanks for your replies :)

You need to add 'enablePushState' => false in Pjax attribute.
Like as
<?php Pjax::begin(['enablePushState' => false]); ?>
For more info. Visit this Demo

Related

CakePHP 3.6: Change submit button label

I tried to change the label of a Submit button with no luck:
<?= $this->Form->button(__('Submit'), array('name' => 'Create')) ?>
How I can change it?
Just change the text from Submit to Create:
<?= $this->Form->button(__('Create')) ?>
It will generate a button that looks like below:
Simply do this :
$options = [];
<?= $this->Form->button(__('Create'), $options) ?>
In CakePHP 4.x, you can use
<?= $this->Form->submit("Login",['class'=>'any-css-class-if-needed']); ?>
For reference: Creating Submit Elements

Flash data message is not getting displayed in the view

I am trying to set flash data in Controller 1 and redirect to Controller 2, where I store the data in a $page_data variable to get it displayed in View 2. The following are my codes:
Controller 1:
$this->session->set_flashdata('message','my custom message');
redirect('controller2','refresh');
Controller 2:
$page_data['loginmessage'] = $this->session->flashdata('message');
$this->load->view('view2',$page_data);
View 2:
<p> <?php echo $loginmessage ?> </p>
CodeIgniter is able to load View 2, but does not display the login message. To put in another way, I am not getting the flash data message in Controller 2.
No need to use controller 2 for showing flashdata. Use this
Controller1
$this->session->set_flashdata('message','my custom message');
redirect('controller2/any_method');
Now you may use message in view2.php (which you are loading through controller 2) file as
<p> <?php echo $this->session->flashdata('message'); ?> </p>
You need to change the small piece of code your code to redirect it correctly.
Controller 1:
function first()
{
$this->session->set_flashdata('message','my custom message');
redirect("/controller2/second", "refresh");
}
Controller 2:
function second()
{
$page_data['loginmessage'] = $this->session->flashdata('message');
$this->load->view('view2',$page_data);
}
Also, if you refresh the page then flash data will disappear because it will appear only once.
View code is same
<p> <?php echo $loginmessage ?> </p>

How to display values in listBox() for multiple selection using Yii2

I am creating a blog using Yii2. I have basic DB structure having tables:
Posts
Categories
Posts_Categories
I am using Yii2 ActiveForm to create post creation form. There are input fields for Title (text field), Content (text area), Categories (list box for multiple category selection).
I am not able to populate listBox with db values.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Content')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName',['multiple' => true])); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is throwing following error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: backend\models\Category::1
There is error in listbox line. Secondly, after populating the data in listbox, how can I handle data insertion with respect to posts and multiple categories relationship.
You have syntax error in listBox. so, complete end parenthesis ) before multiple property.
Like as,
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName'),['multiple' => true]); ?>
For the second part of your question on data insertion. You may need to traverse the array of the selected (posted) items in the list box. Here is an example that may be included in the actionCreate() or actionUpdate() functions of your controller file (I am assuming PostController.php):
$selectedList = $_POST['model_name']['CategoryId'];
if(isset($selectedList)) {
foreach($selectedList as $value){
$pcmodel = new Post_Categories(); //assumption on model name
//do neccessary check here
$pcmodel->post_id = $postmodel->id;
//assumption that value is being stored
$pcmodel->category_id = $value;
$pcmodel->save();
}
}

Yii 2 Exclude NavBar in Modal

In my index.php view, I have a button, once clicked, a modal will pop up:
<p>
<?= Html::button(
'Create New BizAdmin',
['value' => Url::to(['createbizadmin']),
'class' => 'btn btn-success',
'id' => 'modalButton'
]) ?>
<?php
Modal::begin(['id' => 'modal']);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</p>
My modal file is createbizadmin.php where it has the following codes:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\User */
$this->title = 'Create New Bizadmin';
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_formbizadmin', [
'model1' => $model1,
'model2' => $model2,
]) ?>
</div>
My problem is this:
As you can see, the navbar looks horrible. The menu list seems to overflow outside the modal.
How do I get rid of the navbar inside my modal? I can't seem to find which part of creatbizadmin.php I should edit.
Any thoughts?
I'm guessing that you have a controller somewhere that is handling the url createbizadmin. I'm also guessing that inside that controller action you are rendering the view file like this;
$this->render("createbizadmin");
If so, then that is your problem. By calling a view file directly, Yii will apply default layouts to the view file. You have no control over how this happens from within the called view file, and all your menus etc will be rendered.
To get around this you will probably need to render a partial file. This rendres the file without applying layouts. So use;
$this->renderPartial("createbizadmin")
Alternatively, if the modal content is being generated as a result of an ajax call, you can respond from the controller with;
$this->renderAjax("createbizadmin")
This article seems to have a good explanation of how best to achieve this; Render a form in a modal popup

What is the code inside a paginator view?(Laravel)

<?php echo $users->links('view.name'); ?>
If I specify this view in laravel-4, what will be the code inside it? I can't find an example in the docs. Any example please?
according to this http://laravel.com/docs/pagination
If you use the paginator like this
<?php echo $users->links(); ?>
Laravel will use the default view which is defined inside app/config/view.php as
'pagination' => 'pagination::slider-3',
With this default option, your pagination will use the Illuminate/Pagination/views/slider-3.php view,
which is the following:
<?php if ($paginator->getLastPage() > 1): ?>
<ul class="pagination">
<?php echo $presenter->render(); ?>
</ul>
<?php endif; ?>
You can define your own view, and then use inside it the $paginator object to format it according to your needs.
You can see an example here.

Categories