How do I write a codeigniter badge? - codeigniter

How do I count all unread email for a specific user and put it on a badge:
The files are here.
Location code/function:
crud_model
// message number apper count per user // START
header
// message number apper count per user // START
Example: The Admin have 10 message total (8 read message + 2 unread message). The badge show 2 unread message.
<?php
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('sender', $current_user);
$this->db->or_where('reciever', $current_user);
$message_threads = $this->db->get('message_thread')->result_array();
$unread_message_number = count($message_threads);
?>
<a href="<?php echo base_url();?>index.php?teacher/message">
<i class="entypo-mail"></i>
Message
<span class="badge badge-secondary"><?php echo $unread_message_number; ?></span>
</a>
</li>
<?php endif;?>
The existent controller:
function count($message_thread_code) {
$unread_message_counter = 0;
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$messages = $this->db->get_where('message', array('message_thread_code' => $message_thread_code))->result_array();
foreach ($messages as $row) {
if ($row['sender'] != $current_user && $row['read_status'] == '0')
$unread_message_counter++;
}
return $unread_message_counter;
}
I want to count the total number of unread messages from the logged user.

You should not name a method or function the same as a native PHP function like count(). Remove the method, and instead just use Codeigniters count_all_results()
Try this:
<?php
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('message_thread.reciever', $current_user);
$this->db->where('message.read_status', 0);
$this->db->from('message_thread');
$this->db->join('message', 'message.message_thread_code = message_thread.message_thread_code');
$unread_message_number = $this->db->count_all_results();
?>
Since you do not have a reciever id in your table message, we are making a join to be able to query the current user. In my example I assume that you have the column message_thread_code as the foreign key between the messages and the tread?
If this example works, you do not need the custom count() method in your controller.
Alternative 2: To make the code cleaner, you could put the logic inside a method in the controller:
function count_unread(){
$current_user = $this->session->userdata('login_type') . '-' . $this->session->userdata('login_user_id');
$this->db->where('message_thread.reciever', $current_user);
$this->db->where('message.read_status', 0);
$this->db->from('message_thread');
$this->db->join('message', 'message.message_thread_code = message_thread.message_thread_code');
return $this->db->count_all_results();
}
And then remove the PHP-code and only use this in your header:
<?php if($account_type == 'teacher'):?>
<li>
<a href="<?php echo base_url();?>index.php?teacher/message">
<i class="entypo-mail"></i>
Message
<span class="badge badge-secondary"><?php echo count_unread(); ?></span>
</a>
</li>
<?php endif;?>

All you need to do is count the number of $message_threads, right?
...
$message_threads = $this->db->get('message_thread')->result_array();
$unread_message_number = count($message_threads);
And then send $unread_message_number to your view. You put everything together in your example, I only imagine your model, view, and controller are separate in your actual app though.

Related

Activate default tabs based on condition

I have the following code a page that holds 3 tabs as follow:
<div id="tab-container">
<button class="tablink" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The idea is to make whatever tab that's called on from a controller the default and land on it. I try passing $data to the page but it's telling me it's undefined. I also think maybe storing the info in a session and calling it on the page where the tabs are, but I'm not that flexible yet with the coding.
function index ($page='wall') {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
I know the codes are not all there right now, but hopefully you get the idea. I will probably need to use an IF statement either in php/js and I was hoping someone might give me some feedback on that. Thanks in advance for all input.
controller :
public function index ($page = 'wall') {
$this->load->helper('url'); // only if you haven't load helper in autoload.
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
view :
<?php $active_tab = end($this->uri->segment_array());?>
<button class="tablink<?php echo ($active_tab == 'wall')? ' active':''; ?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink<?php echo ($active_tab == 'profile')? ' active':''; ?>" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink<?php echo ($active_tab == 'gallery')? ' active':''; ?>" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The first method is to use parameters in the URL like http://localhost/example.com/home?tab=wall
Then use this URL parameter to active your tab
$tab = $this->input->get('tab'); //wall
<button class="tablink <?php if($tab == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Apply same for other tabs as well
The second method is to pas variable to view. Use variable not a parameter in a function
function index() {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
Use $defaultOpen like this
<button class="tablink <?php if($defaultOpen == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Try something like this
// controller
public function index ($page = 'wall') {
// pass the selected data to view
$data['selectedPage'] = $page;
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
// view
// use ternary operator to set active class to tab
// example:
// <?php echo $selectedPage== 'wall' ? 'active' : '' ?>
// code above means if $selectedPage equals 'wall', set class to active,
// if not do nothing
<button class="tablink <?php echo $selectedPage== 'wall' ? 'active' : '' ?>"
onclick="openPage('Wall', this, '#F06078')"id="defaultOpen">Wall</button>
<button class="tablink <?php echo $selectedPage== 'profile' ? 'active' : '' ?>"
onclick="openPage('Profile', this,
'#F06078')">Profile</button>
<button class="tablink <?php echo $selectedPage== 'gallery' ? 'active' : '' ?>"
onclick="openPage('Gallery', this,
'#F06078')">Gallery</button>

add blog picture to user profile page in joomla contact page

I want to add the article's pictures before the articles titles in the contact (user profile page ) like so:
Also, I have created an ovveride into a template html/com_contact/contact
and I have added this code but it gives a warning:
"Notice: Undefined property: stdClass::$images"
and no image.
Code:
/ Create a shortcut for params.
$images = json_decode($this->item->images);
$introImage = $images->image_intro;
?>
<?php if ($this->params->get('show_articles')) : ?>
<div class="contact-articles">
<ul class="nav nav-tabs nav-stacked">
<?php foreach ($this->item->articles as $article) : ?>
<li>
<?php echo JHtml::_('link',
JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article-
>catid, $article->language)), htmlspecialchars($article->title, ENT_COMPAT,
'UTF-8')); ?>
<?php echo $introImage; ?>
</li>
How can I get this fixed?
i have fixed it by using this cod ;
// Create a shortcut for params.
$article_id = $article->id; // get article id
//aticle_id = JFactory::getApplication()->input->get('id'); // get article
id
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('images'))
->from($db->quoteName('#__content'))
->where('id = '. $db->Quote($article_id));
$db->setQuery($query);
$result = $db->loadResult();
$intro_image = json_decode($result)->image_intro;
//var_dump($intro_image);
//echo $intro_image;
echo "<img src=\"".$intro_image."\" alt=\"icon\" >";

Add 'active' class to K2 Content Module Item

I have searched to the end of the internet and cannot find an answer to this and my limited php knowledge is making this seemingly easy task very difficult.
The file is modules/mod_k2_content/templates/default/default.php around LINE 22
Here is the code:
<li id="" class="<?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1) echo ' lastItem'; ?>">
I simply need to add an 'active' to the class area IF the li is the page I am currently viewing in order to highlight it with CSS.
You should be able to check the standard joomla routing variables to do some checks. I don't use K2 much, so you may have to play with the values to get this working in your case:
$jinput = JFactory::getApplication()->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
$id = $jinput->get('id');
I would then compare those values to the items in the link that are likely in the code directly after the code that you have included. If all three match, you are on that page!
David's answer is correct you need to check for option,view and id and than add the class to li here is rest of the code -
<?php
$jinput = JFactory::getApplication()->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
$id = $jinput->getInt('id'); ?>
<?php foreach ($items as $key=>$item):
$liclass = '';
if(($option=='com_k2') && ($view=='item') && ($id==$item->id)){
$liclass = 'active ';
});
?>
<li class="<?php echo $liclass?><?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1) echo ' lastItem'; ?>">
Hope this will help.
Here is the correct code:
<?php $id = JRequest::getVar('id'); ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; if(count($items)==$key+1) echo ' lastItem'; echo ($id == $item->id)?" active":""; ?>">

Wordpress The Events Plugin wp_query not paginating

I've got an issue with Modern Tribe's "The Events Calendar" plugin not paginating properly. It displays the first page fine, but refuses to go to any secondary pages (using wp-pagenavi). Ideally, I'm trying to have it load via AJAX, but at this point pagination would be a miracle. I've exhausted an entire day here. Basically, this is the query:
<?php
$wp_query = new WP_Query();
$wp_query->query( array(
'post_type'=> 'tribe_events',
'eventDisplay' => 'upcoming',
'posts_per_page' => 5)
);
$max = $wp_query->max_num_pages;
$paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<li><!-- do stuff --> </li>
<?php
endwhile;?>
</ul>
<?php
endif;
wp_reset_query(); // important to reset the query
?>
There are quite a number of issues with your loop:
You say you're using wp_page_navi, however I'm not seeing any references to the plugin in any of your code. Also, you have list items being generated in your loop along with a closing ul tag, but I don't see any opening ul tag anywhere, which could also be contributing to some of your problems.
I'm also noticing in your list of arguments that you're trying to set 'eventDisplay' to 'upcoming'. 'eventDisplay' is not a valid WP_Query parameter. I'm guessing you probably have a registered Taxonomy of eventDisplay? If so, you will need to use Tax Query instead. I've removed this parameter in the example, but feel free to replace it when you're comfortable with setting the parameters you need.
Lastly, query arguments should be made when you call WP_Query, not through $query->query.
Here's something I came up with using standard Wordpress Paging and the arguments you have in your code. I'm not familiar with wp_page_navi, but this should help get you started on the right track:
<?php
global $paged;
$curpage = $paged ? $paged : 1;
$query = new WP_Query(array(
'post_type'=> 'tribe_events',
'paged' => $paged,
'posts_per_page' => 5
));
if ($query->have_posts()) :
?>
<ul>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
echo '<div id="wp_pagination">';
echo '<a class="first page button" href="'.get_pagenum_link(1).'">«</a>';
echo '<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
{
echo '<a class="'.($active = $i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
}
echo '<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>';
echo '<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>';
echo '</div>';
endif;
wp_reset_query();
?>
This will set your loop to display 5 post titles in your list. Below it will be a series of numbered links based on how many posts you have. When you click on a number, it reloads the page with its corresponding titles.
Let me know if this helps.

Sorting categories in Magento according to the position in admin

I would like to know how to sort this list of categories (I followed this tutorial here http://www.devinrolsen.com/magento-custom-category-listing-block/) in magento by position in the admin panel? Currently it is sorted by id
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo '<a href="' . $category->getUrl() . '">';
echo $category->getName() . '</a>';
?>
</li>
<?php endforeach; ?>
</ul>
You're making way too much work for yourself trying to deal with IDs and stuff. The following is already sorted by position as standard.
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<?php echo $category->getName() ?>
</li>
<?php endforeach; ?>
</ul>
If you want to sort the categories by the position created in adminhtml you can then, since catalog/category is an instance of Mage_Catalog_Model_Resource_Category_Collection, make a query where you specify what you want to select, filter and/or sort.
The case here is getting categories from catalog_category_entity select only the name, filtering after the id and sort the query on the position.
<?php
$subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>
This is what I did:
$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds =
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();
after to retrieve the categories:
$categoryChildren = array();
if ($categoriesIds) {
foreach ($categoriesIds as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryChildren[] = $category;
}
}
and then:
// Sort by position
function comparePosition($a, $b) {
if ($a->position == $b->position)
return 0;
return ($a->position > $b->position) ? 1 : -1;
}
usort($categoryChildren, 'comparePosition');
Inverting the return (1 and -1) would obviously change the order.
It worked just fine for me.
Hope it helps someone.
I strongly suggest to lok here first http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections and also other articles in knowledge base are a must read for any magento dev.
<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>
<?php
$model_category = Mage::getModel('catalog/category')->load($_category->getEntityId());
$sub_categories = $model_category->getCollection();
$sub_categories -> addAttributeToSelect('url_key')
-> addAttributeToSelect('name')
-> addAttributeToFilter('is_active',1)
-> addIdFilter($model_category->getChildren())
-> setOrder('position', 'ASC')
-> load();
?>
<?php foreach($sub_categories->getData() as $each_subcat): ?>
<?php $model_subcat = Mage::getModel('catalog/category')->load($each_subcat['entity_id']); ?>
<?php endforeach; ?>

Resources