Simplify star rating code - php

I'm currently working on a rating system for a website but I'm stuck with the conditional statements in RAIN Language. I have 2 questions:
Is there a way to make this piece of code more efficient?
Is it a problem to leave it like this or will it slow down the loading speed of a website?
<div class="product-rating">
{% if (product.score * 5) < '1.3' %}
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '1.3' and (product.score * 5) < '1.7' %}
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '1.7' and (product.score * 5) < '2.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '2.2' and (product.score * 5) < '2.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '2.7' and (product.score * 5) < '3.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '3.2' and (product.score * 5) < '3.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '3.7' and (product.score * 5) < '4.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '4.2' and (product.score * 5) < '4.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
{% endif %}
{% if (product.score * 5) > '4.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
{% endif %}
</div>

It's better to implement a function to draw the score, something like this without templating :
function drawScore($score){
$solid = floor($score);
$empty = 4 - $solid;
$half = $score - $solid;
for($i = 1; $i <= $solid; $i++){
echo '<i class="fa fa-star"></i>';
}
if($half > 0){
echo '<i class="fa fa-star-half-o"></i>';
}
for($j = 0; $j < $empty; $j++){
echo '<i class="fa fa-star-o"></i>';
}
}
Then all you need inside <div class="product-rating"> is to call the function like:
$pScore = $product.score * 5;
drawScore($pScore);

Try this (not sure if there are syntax problems as I wrote it here..
The thing I change is that numbers are now not strings and I don't skip all numbers which you have set for ranges.
Using elseif confirms that the calculation is bigger than the previous IF. So there shouldn't be any problem.
+ This calculation score*5 can be made before the how IF statement as somebody purposed.
<div class="product-rating">
{% if (product.score * 5) < 1.3 %}
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 1.7 %}
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 2.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 2.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 3.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 3.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 4.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 4.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
{% else %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
{% endif %}
</div>

Here is a method using a for loop.
<div class="product-rating">
{% if (product.score * 5) < 1.3 %}
<i class="fa fa-star"></i>
{% endif %}
# array = [1.3,2.2,3.2,4.2]
{% for item in array %}
{% if if (product.score * 5) > item %}
<i class="fa fa-star"></i>
{% elseif (product.score * 5) < (item + 0.5) %}
<i class="fa fa-star-half-o"></i>
{% endif %}
{% endfor %}
{% if (product.score * 5) > 4.7 %}
<i class="fa fa-star"></i>
{% endif %}
</div>

Related

Showing Star Rating based on text file number in array [duplicate]

This question already has answers here:
Displaying star rating with Font Awesome icons
(4 answers)
Laravel - Star rating - Optimization?
(5 answers)
Closed last month.
I have a script which reads an array from a text file and grabs each line and produces star rating images based on number in array. The script is kind of big and was wondering if there was a better way of doing this.
<?php if ($row[2] == '5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
<?php } elseif ($row[2] == '4.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
<?php } elseif ($row[2] == '4') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1') { ?>
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '.5') { ?>
<i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '0') { ?>
<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } ?>
Yes, better ways... A loop will help, maybe something like this (PHP demo at tio.run).
function star_printer($num)
{
$star_class = [
'blank' => 'far fa-star',
'half' => 'fas fa-star-half',
'full' => 'fas fa-star'];
$star_html = '<i class="%s"></i>';
$star_str = "";
for($i=1; $i<=5; $i++)
{
$is_half = $i==ceil($num) && ceil($num)!=$num;
$star = $is_half ? "half" : ($i>$num ? "blank" : "full");
$star_str .= sprintf($star_html, $star_class[$star]);
}
return $star_str;
}
echo star_printer(3.5);
You can work off this function (doesn't include the half stars, shows you how to do the full star rating - I just noticed you run halfs as well);
function getStars($rating){
$stars = "";
$x = 0;
while($x < $rating) {
$stars = $stars.'<i class="fa fa-star"></i>';
$x++;
}
return $stars;
}
echo getStars(3);
in your case -
echo getStars($row[2]);
You can tweak this that if the number contains a decimal you can append to the string '' for example; you get the gist :P
-------------- Edit
Tweaked it and added the half star rating :)
function getStars($rating){
$stars = "";
$x = 1;
while($x <= $rating) {
$stars = $stars.'<i class="fa fa-star"></i>';
$x++;
}
if (strpos($rating, '.') !== false) {
$stars = $stars . '<i class="fa fa-star-half">';
}
return $stars;
}
echo getStars(3.5);
function getStar($rating) {
$wholeStar = "<i class="fa fa-star"></i>";
$halfStar = "<i class="fa fa-star-half-o"></i>";
// round the rating for whole stars
$roundedRating = round($rating);
// Add the whole stars using str_repeat
$starString = str_repeat($wholeStar, round($rating));
// Add the half star if the rating is bigger than it's rounded value
if ($roundedRating < $rating) {
$starString .= $halfStar;
}
return $starString
}
echo getStar($row[2]);

How to hide the a tag all rows when table field in database has true

How to hide the < a # check > all replies when correct field in database has true?
just one true in same post_id
Database - Replies
...
{
user_id: 1,
post_id: 1,
body: "Lorem ipsum",
correct: // true or false
}
...
PHP Blade
#foreach ($replies as $reply)
<li class="item">
<a href="#" id="check">
<i class="fa fa-check fa-fw"></i>
</a>
</li>
#endforeach
I'm sorry if this question is unclear. I'm English not strong. Thanks for every ideas and answers
#foreach ($replies as $reply)
<li class="item">
<a href="#" id="check">
#if($reply->correct)
<i class="fa fa-check fa-fw"></i>
#else
<i class="fa fa-times-circle-o fa-fw"></i>
#endif
</a>
</li>
#endforeach
Try this
Do you only want to link to correct results ($reply->correct == true)?
#foreach ($replies as $reply)
<li class="item">
#if($reply->correct)
<i class="fa fa-check fa-fw"><a href="#" id="check"></i>
#else
<i class="fa fa-check fa-fw"></i>
#endif
</li>
#endforeach

how do I edit the output of fgetcsv

I'm using eKomi and want to pull in reviews to my website. I've managed to get this working but there's a slight problem. Currently every single review is getting added, and in reverse order.
You'll see from the code below that I am using fgetcsv.
Is it possible to limit to the 12 latest reviews? and then order them from newest to oldest?
Here's the code I've put together.
<div class="row">
<div class="row__colspaced">
<?php
$row = 1;
if (($handle = fopen("http://api.ekomi.de/get_feedback.php?interface_id=66630&interface_pw=f2d097e83db1880e85e2f77aa&range=3m&type=csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<div class='colspan12-4 colspan6-3 as-grid with-gutters'><div class='content-module--review-list__item item' data-mh='review-item'>";
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if($c == 0){
echo gmdate('Y-m-d H:i:s',$data[$c]);
}
else if($c == 2){
if($data[$c] == 1){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 2){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 3){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 4){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 5){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
</div>';
}
}
else {
echo '<div class="inner-item item'.$c.'">' . $data[$c] . "</div>";
}
}
echo "</div></div>";
}
fclose($handle);
}
?>
</div>
</div>

Trying to retrieve data from a string in database

I have the following output in one field in Joomla ($item->attribs):
{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"ja_teline_v:p4p50","content_type":"p4p","ads":"1","ctm_boxer":{"name":["Floyd Mayweather","Manny Pacquiao","Wladimir Klitschko","Miguel Cotto","Gennady Golovkin","Sergey Kovalev","Guillermo Rigondeaux","Juan Manuel Marquez","Carl Froch","Saul Alvarez","Danny Garcia","Roman Gonzalez","Amir Khan","Mikey Garcia","Jhonny Gonzalez","Leo Santa Cruz","Adonis Stevenson","Abner Mares","Terence Crawford","Adrien Broner","Timothy Bradley","Juan Francisco Estrada","Robert Guerrero","Bernard Hopkins","Marco Huck","Nicholas Walters","Sergio Martinez","Julio Cesar Chavez Jr","Arthur Abraham","Nonito Donaire","Orlando Salido","Fernando Montiel","Humberto Soto","Marcos Maidana","Naoya Inoue","Donnie Nietes","Alexander Povetkin","Juan Carlos Reveco","Peter Quillin","Lucas Matthysse","Brian Viloria","Jamie McDonnell","Juergen Braehmer","Kell Brook","Deontay Wilder","Erislandy Lara","Jean Pascal","Carl Frampton","Omar Narvaez","Tomoki Kameda"],"rating":["5","5","4","4","3","3","3","4","3","3","3","3","3","3","3","2","3","2","2","3","3","2","3","5","2","2","3","2","3","4","2","3","2","3","3","2","2","2","2","2","2","2","2","1","1","1","2","1","2","1"],"movement":["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],"record":["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0, 1 KO","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0","0","0","0","0","0"],"ranking":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50"],"highlight":["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]},"ctm_topic_id":""}
How do I retrieve every value from ctm_boxer -> name in a list?
I've tried the below, but to no avail!
<?php $boxers = $item->attribs->get('ctm_boxer', array()); ?>
<?php foreach($boxers['name'] as $index => $boxer_type): ?>
<?php echo $boxer_type; ?>
but it's returning 'Fatal error: Call to a member function get() on a non-object'
Can anyone help as to why please?
Thanks,
Matt
As you can see from var_dump inside $item->attrib is a string shown as string(2494). Not an object/PHP class. That's why you're getting Fatal Error: Call to a member function on a non-object. Because again, $item->attrib is a normal string.
The -> dereference operator can only be used on PHP Objects/Classes.
More on how to use the object operator.
That variable seems to contain a string in JSON format.
Try the following:
$BoxerCollectionObject = json_decode($item->attribs);
$boxerArray = $BoxerCollectionObject->ctm_boxer->name;
foreach ($boxerArray as $boxer) {
var_dump($boxer); //Should output the name of your boxers.
}
DEMO
That's great - thanks - works perfectly... I expanded it a bit with the following to bring in some of the other parts of the string:
<?php
$BoxerCollectionObject = json_decode($item->attribs);
$boxerArray = $BoxerCollectionObject->ctm_boxer->name;
$boxerRating = $BoxerCollectionObject->ctm_boxer->rating;
?>
<table class="table">
<tbody>
<?php $i = 1;?>
<?php $ic = 1;?>
<?php foreach ($boxerArray as $index => $boxer) : ?>
<tr itemprop="boxer" class="boxer<?php echo $ic++; ?>">
<td><strong><?php echo $i++; ?></strong></td>
<td><span><?php echo $boxer; ?></span></td>
<td itemprop="rating"><?php if ($boxerRating[$index] <= 0): ?>
<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 1): ?>
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 2): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 3): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 4): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] >= 5): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
<?php endif;?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
Thanks again, Matt.

Get child items recursively with PHP

I have a table in MySQL that holds Menu Items, and each has a parent, that can be either 0 (Top Menu) or the ID of another Item.
I'm working on a controller within CodeIgniter that was started by another programmer, and here's the brilliant way he found to sweep through the items of each item, recursively.
// I know there's a better way to do this, but as time is short we have to run
$data['aplicacoes_list'] = '';
$aplicacoes = $this->aplicacoes_model->get_aplicacoes_no_parent();
foreach($aplicacoes as $apl){
$data['aplicacoes_list'] .= '<li class="dd-item" data-id="3">
<div class="dd-handle">'.$apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$sub_aplicacoes = $this->aplicacoes_model->get_aplicacoes_by_parent_id($apl['id']);
foreach($sub_aplicacoes as $sub_apl){
$data['aplicacoes_list'] .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$sub_aplicacoes2 = $this->aplicacoes_model->get_aplicacoes_by_parent_id($sub_apl['id']);
foreach($sub_aplicacoes2 as $sub_apl2){
$data['aplicacoes_list'] .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl2['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
}
}
}
So, as you can see, the list goes on until $sub_aplicacoes7, which is just moronic.
Can you guys think of a better way to accomplish this?
Here's a screenshot of the database, as it is.
Here's a recursive method for you:
private function recursive_aplicacoes( $parent = 0 )
{
$sub_aplicacoes = $this->aplicacoes_model->get_aplicacoes_by_parent_id($parent);
if(!count($sub_aplicacoes)) return '';
$s = '<ol class="dd-list">';
foreach($sub_aplicacoes as $sub_apl)
{
$s .= '<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub_apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>';
$s .= $this->recursive_aplicacoes($sub_apl['id']);
}
$s .= '</ol>';
return $s;
}
Put it in same class.
Here's how to use:
$data['aplicacoes_list'] = $this->recursive_aplicacoes();
Should work :)
You could move the code for generating a submenu into it's own function and use recursion. While this will solve your problem may I recommend that, once you've built the menu for a user that you cache it until s/he logs out as your menu system can quickly become quite complex and slow to load the more embedded menus you add (you end up with an recursive N + 1 problem).
$data['aplicacoes_list'] = '';
$aplicacoes = $this->aplicacoes_model->get_aplicacoes_no_parent();
foreach($aplicacoes as $apl){
$data['aplicacoes_list'] .= '<li class="dd-item" data-id="3">
<div class="dd-handle">'.$apl['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>' . build_submenu($this->aplicacoes_model, $apl['id']);
}
function build_submenu($model, $id) {
$aplicacoes = $model->get_aplicacoes_by_parent_id($id);
if (empty($aplicacoes)) {
return '';
}
$submenu = '';
foreach($aplicacoes as $sub){
$submenu .= '<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">'.$sub['nome'].'
<div class="pull-right"><i class="fa fa-edit"></i><span class="hidden-block hidden-xs hidden-sm"> Editar</span> <i class="fa fa-plus"></i> <span class="hidden-block hidden-xs hidden-sm">Adicionar subcategoria</span> <i class="fa fa-trash-o"></i><span class="hidden-block hidden-xs hidden-sm"> Remover</span></div>
</div>
</li>' . build_submenu($model, $sub['id']);
}
return $submenu;
}
You may need to fiddle with this to get the correct HTML tags/CSS that you need.

Categories