in my index page I have this code to call search action:
$this->renderPartial('search');
and this is the search action code:
$criteria = new CDbCriteria();
if (isset($_GET['city'])) {
$q = $_GET['city'];
$criteria->compare('fileName', $q, true, 'OR');
$criteria->compare('tags', $q, true, 'OR');
$dataProvider = new CActiveDataProvider("Files", array('criteria' => $criteria));
$this->redirect('result', array('dataProvider'=>$dataProvider));
}
$dataProvider = new CActiveDataProvider("Files", array('criteria' => $criteria));
$this->render('search');
and this is the search view:
<form method="get">
<?php
$url = CHtml::normalizeUrl(array("files/search"));
$model = Files::model();
$c = new FilesController('view');
$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
'model'=>$model,
'id'=>'autocomplete',
'attribute'=>$model->fileName,
'name'=>'city',
'source'=>$c->actionAutoComplete(),
// additional javascript options for the autocomplete plugin
'options'=>array(
'minLength'=>'1',
),
'htmlOptions'=>array(
'style'=>'height:20px;',
'placeholder'=>'search',
'value'=>'isset($_GET["city"]) ? CHtml::encode($_GET["city"]) : "" ',
),));
?>
<input type="submit" value="search" />
</form>
I want to show the search results in another view, so once clicked the search submit button I want to redirect to another view to show the results, but in this code the search result is shown in the index page.
so how to do it?
you can either set the url that you want your form to be send, like:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'action' => Yii::app()->createUrl('anotherController/anotherView'),
'method' => 'post',
));
or you can redirect to another page after your done in index, like:
$this->redirect(array('anotherController/anotherView'));
Related
In the past few days I was trying to create a search.php template for results that contain my CPT. As long as the search works itself, pagination not. Every link in pagination redirect to 404. I was trying a lot of possibility solutions from Stack, FB and others but with no results. Instead of showing to you my code wich is obviously not working correctly, I thought how great woultd be when someone could paste here a tested by himself a simple search.php template wchich contain CPT in results and with working pagination? Then I could compare with my code and find a solution for this annoying popular issue :)
Thanks!
edit:
If somoeone is curious, this is my current search.php:
<div class="center">
<?php
// Define custom query parameters
$custom_query_args = array(
'post_type' => array('drzewa_formowane', 'pre_bonsai'),
'post_status' => 'publish',
'order' => 'asc',
's' => $s,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
<?php include 'product.php' ?>
<? endwhile;
endif;
// Reset postdata
wp_reset_postdata(); ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'prev_text' => __(' « '),
'next_text' => __(' » '),
'type' => 'plain',
) ); ?>
</nav>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
I was also trying with simple
if ( have_posts() ) : while ( have_posts() ) : the_post();
without WP_Query and I was trying include CPT to search results via "pre_get_posts" but then was no results from my CPT...
I'm working on a project with Fuel. I created a pagination , first page has data truly in it but when I click on other pages , url changes but page never refresh and it doesn't show other pages.
codes:
code in Controller :
$config = array(
'pagination_url' => 'http://localhost/body-app/public/app/list/',
'total_items' => 12,
'per_page' => 3,
'uri_segment' => 5,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = DB::select('*')
->from('bodies')
->limit($pagination->per_page)
->offset($pagination->offset)
->execute()
->as_array();
$data['pagination'] = $pagination;
$view=View::forge('app/list',$data);
and my foreach in View is something like this :
<?php foreach($example_data as $per) {
echo $per['name'] ;
} ?>
and finally code to show pagination :
<?php echo Pagination::instance('mypagination')->render(); ?>
If you work with segment number, you should change the route.php file, and add the example 'prod/page/(:page)?' => 'prod' , or add in the you controller one segment string example 'uri_segment' => 'examplenumberpage'.
I want to send the value of textbox to the Action Method for searching the technology for that i want to get the value of textbox in Action.
I have the following code :-
#Html.TextBox("technologyNameBox", "", new { id = "technologyName", #class = "form-control", #placeholder = "Search For Technology" })
<span class="input-group-btn" style="text-align:left">
<a class="btn btn-default" id="searchTechnology"
href="#Url.Action("SearchTechnology", "Technology",
new {technologyName="technologyName",projectId=ProjectId })">
<span class="glyphicon glyphicon-search "></span>
</a>
</span>
Question :- How to get the value of textbox "technologyNameBox" in Action ?
Please help me out. Thanks in Advance!
You'd have to append the value to the URL via JavaScript before directing the user. Using jQuery (since that generally comes packaged with ASP.NET), it might look something like this (with a good bit of manual conditional checks for blank values or query string parameters):
$('#searchTechnology').click(function (e) {
e.preventDefault();
var url = '#Url.Action("SearchTechnology", "Technology", new { projectId=ProjectId })';
var technologyName = $('#technologyName').val();
if (technologyName.length < 1) {
// no value was entered, don't modify the url
window.location.href = url;
} else {
// a value was entered, add it to the url
if (url.indexOf('?') >= 0) {
// this is not the first query string parameter
window.location.href = url + '&technologyName=' + technologyName;
} else {
// this is the first query string parameter
window.location.href = url + '?technologyName=' + technologyName;
}
}
return false;
});
The idea is that when the user clicks that link, you would fetch the value entered in the input and append it to the URL as a query string parameter. Then redirect the user to the new modified URL.
After the user submits a (uncomplete) form, I want the form to show the already entered data + an error message.
Using this code, the form is empty after submitting the form:
$request = $app['request'];
$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array( 'label' => 'Ihre Name:'))
->add('comment', 'text', array('constraints' => new Assert\Length(array('min' => 15))))
->getForm();
$twig_context = array('form' => $form->createView());
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
return 'valid!';
// Send form...
} else {
// display the form
return $app['twig']->render('contact.html.twig', $twig_context);
}
Twig-template:
{{ form_start(form) }}
{{ form_widget(form) }}
<div>
<input type="submit" value="Send" />
</div>
{{ form_end(form) }}
You should create the form view last, (could be right before you render your template). In your case, the view is created before the data from Request is applied.
This:
$twig_context = array('form' => $form->createView());
$form->handleRequest($request);
Should be:
$form->handleRequest($request);
And your render method should be:
return $app['twig']->render('contact.html.twig',
array(
'form' => $form->createView()
)
);
I have a post type called 'faq' and taxonomy called 'type' within my template and a few taxonomy terms created "Design", "Print", "Display" etc.
The idea I am trying to implement is to display only the posts that belong to assigned taxonomies (types) without duplication. Each post may be assigned to multiple taxonomies (types).
My current code works fine as long as the post have got only one taxonomy assigned to it. As soon as I assign more then one taxonomy it shows duplicate posts like this:
Question 6
Question 5
Question 1
Question 1
Here is my current code:
<?php
$post_type = 'faq';
$tax = 'type';
$faq_types = get_field('types');
$filtered = array();
$termargs = array( 'include' => $faq_types );
$tax_terms = get_terms($tax, $termargs);
if ($tax_terms) {
$i = 1;
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
$tax => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-<?php echo $i; ?>"><i class="fa fa-chevron-right"></i> <?php the_title(); ?></a>
<div id="accordion-<?php echo $i; ?>" class="accordion-section-content">
<?php the_content(); ?>
</div>
</div>
<?php
$i++;
endwhile;
}
wp_reset_query();
}
}
?>
I'd really appreciate any help with getting this working the way I need.
Your current loop is saying "For each taxonomy term, show all posts associated with that term", so of course it will duplicate if there is one post associated with multiple terms. Take your query out of the foreach loop and use a single tax query with an array of terms:
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term_slugs,
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
EDIT
By the way, you'll need to convert your array of term objects to an array of term slugs for this to work properly:
$term_slugs = array();
foreach( $tax_terms as $term ) {
$terms_slugs[] = $term->slug;
}