Kohana simple read from database - kohana

i am trying to retrieve some simple data from the database, and i don't know where i am wrong.
i want to get all the inactive users from my database. For that, i have in my controller:
public function action_useremails()
{
$users = Model::factory('user')->where('user_status', '=', 2);
$this->view->users = $users;
}
and in the view:
<? foreach ($users as $user): ?>
<tr>
<td><span><?= $user; ?></span></td>
</tr>
<? endforeach; ?>
butm even though i have inactive users, i don't see anything in the view. I wonder where am i wrong?
}

You're building the query, but not executing it. Try:
$this->view->users = $users->execute();
also consider that the $users variable in the view will be an array of arrays, you'll have to echo the right element.

If you are talking about Kohana 2, your query must be:
$users = Model::factory('user')->where('user_status', 2)->find();
Note the args in where() block & the find() at last. Without find(), you are just building the query, but not running it.
Docs: http://docs.kohanaphp.com/libraries/orm#find
If it is KO3, it has to be similar to the above.

for v3.0 add the find_all() method at the end:
$users = Model::factory('user')->where('user_status', '=', 2)->find_all();
It executes the query you built before with the other methods.

Related

Woocommerce add attribute info to product shortcode

I want to add a product's size attribute to the product information shown by the Woocommerce [product] shortcode.
Is there a way to do this? I don't think it is a standard parameter to the shortcode so am assuming the code will need amending?
You could either unregister and re-register your own version of the shortcode OR the [product] shortcode displays content via
<?php wc_get_template_part( 'content', 'product' ); ?>
Therefore you can customize the the content-product.php template in your own theme via template overrides or via hooks/filters. (The content-product.php template is mostly just a few action hooks so it looks a little sparse if you aren't familiar with hooks.)
I would probably go the hooks route. Assuming your attribute is called "pa_size" (WooCommerce always prefaces the attribute with "pa_" so it know that the taxonomy is a "product attribute") I would add the following to my theme's functions.php
function kia_add_size_attr(){
global $product;
$attribute_name = "pa_size";
$results = wc_get_product_terms( $product->id, $attribute_name, array('fields' => 'names') );
if ( $results ) {
echo '<label>' . wc_attribute_label($attribute_name) . ': </label>' . implode( ', ', $results );
}
}
add_action('woocommerce_after_shop_loop_item_title', 'kia_add_size_attr' );
Do keep in mind that this will add the attribute to every "loop" product with a size attribute. If you only want to apply it to the shortcode, then you probably need to write your own shortcode.

Joomla (3.x) custom form field type:text with suggestion dropdown

I'm try'n to create a custom form field of type text (or list) where a user can a) type free text and/or b) select from drop-down. Now I found many posts about autocomplete or auto-fill, but that's not what I'm after.
I followed the example on how to create a 'City' Custom field here http://docs.joomla.org/J2.5:How_to_add_custom_filters_to_components and this is all working.
However, it creates a drop-down only, no option to enter text.
I'm new to the Joomla (3.x) component development, so maybe I am missing something very simple here. With all those field types available, it's hard to belief there is no drop-down with free input.
So
1. can I make the default select/list to accept free text?
2. if not, can I get a pointer on how to get started making one my self?
3. For now, it would be fine to have ~10 city names listed, and free input, no need to filter the city list while typing. But ultimately, I would like to know on how to create a filter while typing Ajax version of this. (Like a suggest input-box)
This is what I use at the moment, the example as linked above
I also tried extending Jformfield, with no luck
class JFormFieldCftCity extends JFormFieldList {
protected $type = 'CftCity';
public function getOptions() {
$options = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('value As value, name As text');
$query->from('#__bitLuCity AS a');
$query->order('a.sortOrder');
$query->where('isEnabled = 1');
$db->setQuery($query);
$options = $db->loadObjectList();
return $options;
}
}
Thanks
Regards
Andreas
For anyone in the same position and future reference I'm going to post my own solution here as I ended up not using #isherwood's suggestion. I have not yet figured out completely how to integrate 'select2' into my component, nor is it needed for simple 'combobox' behavior.
This is HTML5 only, no additional script's, extends a plain JFormField.
It will allow free input, as well as select from the static list and filters while typing.
class JFormFieldCftCity extends JFormField {
protected $type = 'CftCity';
public function getInput() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('value As value, name As text');
....
$db->setQuery($query);
$rows = $db->loadObjectList();
$control= '<input id="' . $this->id . '" name="' . $this->name
. '" list="dataSrc-'. $this->id .'" value="' . $this->value
. '" type="text" class="inputbox" />'
. '<datalist id="dataSrc-'. $this->id .'">';
for ($i = 0; $i < count($rows); $i++) {
$control .= "<option value='{$rows[$i]->text}'>{$rows[$i]->text}/option>";
}
$control .= '</datalist>';
return $control;
}
It did not work in Safari, but I finally found a solution that works in all browsers, until datalist HTML5 markup is supported by all browsers:
<input type="text" class="span6 " style="margin: 0 auto;" data-provide="typeahead" data-items="4" data-source="["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]" />
You can of course fill "data-source" with data from your query.

woocommerce get attribute terms

In Woocommerce you can add global product attributes and terms. So for instance:
Size (attribute)
small (term)
medium (term)
large (term)
This is product independent. You can then select from the pre defined attributes on a product.
I need to get all of the terms in an attribute with php. So select the attribute required, eg size, and then return an array including [small,medium,large].
Seems simple enough but I can't find any help on doing this.
Slightly confusing, especially when looking through the WooCommerce Docs since there is absolutely no mention of getting a list of the terms/attributes.
The Attributes are saved as a custom taxonomy, and the terms are taxonomy terms. That means you can use native Wordpress functions: Wordpress get_terms() Function Reference
By clicking on an attribute in WooCommerce, you can look in the URL and you can see they are all prepended with 'pa_'
This is likely what you need:
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}
I wanted to be able to get all the different attributes from the backend that were set, and get them in an array for me to work with, I took some code from the class-wc-admin-attributes.php file and modified it for my needs:
<?php
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ($attribute_taxonomies) :
foreach ($attribute_taxonomies as $tax) :
if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
$taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0');
endif;
endforeach;
endif;
var_dump($taxonomy_terms);
exit;
This will loop through all the attribute taxonomies, retrieve the terms for each, leaving you with an array of term objects to work with for each taxonomy.
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'pa_taxonyname',
'hide_empty' => false,
) );
For example, if the taxonomy slug is 'date', so the taxonomy will be 'pa_date'.
You can also mouse over the attribute name and see the taxonomy name at the bottom of the browser.
I hope it helps!
I use this:
echo '<h1>variations</h1>';
mario( $product->get_available_variations());
echo '<h1>Atributos</h1>';
mario($product->get_attributes());
echo '<h1>Poste Terms</h1>';
mario(wp_get_post_terms( $post->ID, 'pa_color'));
function mario($texto){
echo '<pre>';var_dump($texto);echo '</pre>';
};
Really with: "wp_get_post_terms( $post->ID, 'pa_color')" i search only one term, but the idea is to loop for the key ['name'] that return that function.
Get all attributes with attribute terms in woocommerce.
$attributes = wc_get_attribute_taxonomies();
foreach ($attributes as $attribute) {
$attribute->attribute_terms = get_terms(array(
'taxonomy' => 'pa_'.$attribute->attribute_name,
'hide_empty' => false,
));
}

Order posts by date or keyword

I want to enable website visitors to order WordPress posts on the category page by date posted or by search keyword, similarly to how it's done on this page:
http://www.steinwaymusical.com/news.php
I'd appreciate a plugin recommendation or any other suggestion from knowledgeable people.
Thank you in advance!
WordPress has order and orderby options you can use in the query.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
<?php
$args = array('order' => 'ASC', 'orderby' => 'name');
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>
Your example page uses GET variables.
?selSort=name_asc&txtKeyword=sdfsdf
So you need to create a form with method="GET" that submit GET data to the current page. Then, using PHP, you can check if any GET data is set (in this case, selSort and txtKeyword). If either of those are set, put them into your query. Then you can modify the query to resemble this:
<?php
$args = array('order' => $_GET['selSort'], 'orderby' => $_GET['txtKeyword']);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>

Search and filter page using code igniter

Started using code igniter recently and been trying to make a page that filters events by certain parameters .I have the view page that contains an input type search (searches keywords in database)and a drop down filter list that filters by name of event,location and pricing.however I need help in querying the search results from database and displaying it on my view page .Anyone with an idea? thanks in advance
when you are submitting a form submit it to the controller method and get the post value in controller method
function search()
{
$keyword = $this->input->post('keyword');
$this->load->model('mymodel');
$result = $this->mymodel->getSearchResults($keyword);
$data['results'] = $result;
$this->load->view('search_results',$data);
}
And in your model
function getSearchResults($keyword){
$this->db->like('column_name',$keyword,'after');
return $this->db->get('tablename')->result();
// or you can write query simple way instead of Active Record
// $query = "SELECT BLah blah FROM mytable WHERE column_name like '$keyword%'";
// return $this->db->query($query)->result();
}

Resources