I'm currently writing an application in Yii in which i want to create a search box in the header (either in the navigation menu or just above it). This search box should be able to be accessed from every part of the site, and it should be able to search on different columns of different tables.
I have no idea on how to do this, and almost all posts on the web about it involves using the grid-view, or an extension (I'd like to create the code without an extension if that's possible).
Do you have an idea to how the search code should look (what i should put in which controller etc.)?
-- EDIT --
I still don't know how to do this but I will show you what I have at the moment anyway. It is not much and it is pretty obvious where i am missing some code.
/view/layout/main.php:
<?php echo CHtml::form(Yii::app()->createUrl('product/search'), 'get') ?>
<?php echo CHtml::textField('search_key','',array('placeholder' => 'Search')); ?>
<?php echo CHtml::submitButton('Go'); ?>
<?php echo CHtml::endForm() ?>
/view/product/search.php:
//Not sure by any means what to write here, but I'll like a list view populated with the search results
/controllers/productController.php
/**
* Search through model.
*/
public function actionSearch()
{
if(isset($_GET['search_key'])){
$search = $_GET['search_key'];
$model->name = $search;
}
$this -> render('search', array(
'model' => $model,
));
}
/models/Product.php
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
You can perform like this:
Find #mainmeu in /protected/views/layout/main.php
<div id="mainmenu">
<div style="width: 80%;float: right">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'home', 'url'=>array('/site/index')),
array('label'=>'about', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'contact', 'url'=>array('/site/contact')),
),
)); ?>
</div>
<div style='float: left;direction: rtl; color: #ffffff; margin: 5px 0 0 5px; font-size: 13px'>
<?php echo CHtml::form(Yii::app()->createUrl('product/search'),'get') ?>
<?php echo CHtml::textField('search_key', 'search') ?>
<?php echo CHtml::submitButton(); ?>
<?php echo CHtml::endForm() ?>
</div>
</div><!-- mainmenu -->
Edit:
/models/Product.php:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/controllers/productController.php -> actionSearch():
public function actionSearch()
{
$model = new Product('search');
$model->unsetAttributes();
if(isset($_GET['search_key']))
$model->name = $_GET['search_key'];
$this -> render('search', array(
'model' => $model,
));
}
/view/product/search.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Related
I heard building Data in View is not very good, but anyway, i am wondering why its not working:
View
<?php $form = ActiveForm::begin();
$alleSpieler = \common\models\Spieler::find()->all();
if ($alleSpieler) {
unset($types);
foreach ($alleSpieler as $value) {
$types[$value->id] = $value->email . ' ' . $value->vorname . ' ' . $value->nachname;
}
}
echo $form->field($model, 'spielerId')->dropDownList($types, 'prompt'=>'Spieler manuell hinzufügen']);
ActiveForm::end();
?>
<?= AnmeldungDurchfuehrung2::widget(['durchfuehrungId' => $model->id, 'spielerId' => $model->spielerId]) ?>
Model
public $spielerId;
But spielerId ist not set in my Case. If i, for example, set 'spielerId' => 1120 in the widget call, it is working. But if i want the value from the dropdownlist, the action is saying that spielerId is missing. I am newbie and perhaps i forgot something? Thank you!
You must add $spielerId; in validation array in your model, som like this:
public function rules()
{
return [
[['spielerId'], 'integer'], //type of atribute value
[['spielerId'], 'required'], //if need
/*... other atributes ...*/
];
}
for more detail check the documentation.
okay now i know what i need:
echo $form->field($model, 'spielerId')->dropDownList($types,['prompt'=>'Waehlen Sie einen Spieler']);
echo Html::submitButton('Auswählen', ['class' => 'btn btn-primary']);
This is my Dropdown.
I need something like:
<?php if(!empty($_GET['spielerId'])) {
echo AnmeldungDurchfuehrung2::widget(['durchfuehrungId' => $model->id, 'spielerId' => $_GET['subject']]); }?>
I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.
<?php get_header(); ?>
<?php
$args = array(
'posts_per_page' => 30,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'attorneys'
);
query_posts($args);
if ( have_posts() ) :
?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="attorney-preview">
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />
<p><strong><?php echo get_post_meta($post->ID,'attorney_name', true); ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php get_footer(); ?>
When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />
Try this
<?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));
if (have_posts()) : while (have_posts()) : the_post();
$attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);
$attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>
<div class="attorney-preview">
<img src="<?php echo $attorney_photo; ?>" />
<p><strong><?php echo $attorney_name; ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
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;
}
I'm looking for to get "get_post_meta" from a post type which is different of the display post ?
here is my code , but this display the post_meta of all posts in the post type !
I think it could be compare the posts who have the same slug (post_name) , but I don't know how to do !
<?php $sectionscontact = new WP_query(array('post_type' => 'sections-contact', 'post_count'=>1));if ($sectionscontact->have_posts()): while ( $sectionscontact->have_posts() ) : $sectionscontact->the_post(); $telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;"> Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_ID(), 'telephone', true); ?>
</div>
<?php } ?>
What I've done here is to tidy it up for you.
Also, it displays the post_meta for everything in this post type because you're using post_count as opposed to posts_per_page, which is the correct term to display some certain amount of posts in one query.
So, Instead of this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'post_count' => 1
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta(get_the_id(), 'telephone', true); ?>
</div>
<?php } ?>
You want this:
<?php
$sectionscontact = new WP_query( array(
'post_type' => 'sections-contact',
'posts_per_page' => 1 //changed post_count to posts_per_page
));
if ($sectionscontact->have_posts()):
while ( $sectionscontact->have_posts() ) :
$sectionscontact->the_post();
$telephone_meta_empty = get_post_meta(451,'telephone', true);
if ( ! empty ( $telephone_meta_empty ) ) { ?>
<div class="tp_titre_bloc" style="border-right:1px solid #999999;">Téléphone</div>
<div class="tp_content_bloc">
<?php echo get_post_meta($post->ID, 'telephone', true); // changed get_the_id() to $post->ID?>
</div>
<?php } ?>
Let me know if that helps.
More info on WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query
Thanks.
Edited: Answered below.
I'm using the following query to paginate my posts on a Custom Page template that pulls a Custom Post type. this exact code works on Author.php, but this custom template Page, "Filmmaking" does not show the pagination. I have a simple, tried and true pagination function after my loop. Why would it work for author.php, but not for page-filmmaking.php?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$film_query = array(
'post_type' => 'filmmaking', // Custom Post Type
'paged' => $paged, // Set it up to be paged so you can use pagination
'posts_per_page' => 2, // How many to show per page
);
// The Query
$the_query = new WP_Query( $film_query );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Answer: Get rid of if has_posts.
Working Code:
<?php
/**
* Template Name: pagination test
*/
get_header(); ?>
<?php //get_sidebar(); ?>
<div id="page-left">
<?php
$wp_query = new WP_Query();
$wp_query->query(array(
'post_type'=>'filmmaking',
'paged' => $paged,
'posts_per_page' => 2,
));
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php previous_posts_link('« Nyare') ?>
<?php next_posts_link(' Äldre »') ?>
</div><!-- page left -->
<?php get_footer(); ?>