how do I edit the output of fgetcsv - php

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>

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]);

Sort movies with dropdown

I'm new to PHP and I'm having some trouble with a sort by with a dropdown.
I have a page of movies already sorted by genre and within that page I want to be able to sort by title, rating and year and I don't know how to do it.
In the index page I was able to sort the movies easily because they weren't already sorted by genre, so I just had to put ORDER BY something in the query and it returned what I wanted.
The problem now is that I have to check which genre is chosen at that moment and associate it with what I choose from the dropdown list and I dont know how to do it...
I don't know if this is explicit enough, but if you have any doubt please ask me...
I have the following code which I copied from an existing question here:
<form class="sortby_form" action="">
<div class="sortby" method="POST">
<select name="sort_by" class="drop-box">
<option selected hidden <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'option') echo 'selected="selected"'; ?> value="option">Sort by</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'title') echo 'selected="selected"'; ?> value="title">Title</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'year') echo 'selected="selected"'; ?> value="year">Year</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'rating') echo 'selected="selected"'; ?> value="rating">Rating</option>
</select>
</div>
<form>
This is the code that I have which returns the movies ordered by genre:
<?php
if (isset($_REQUEST["g"])) {
$g = $_REQUEST["g"];
} else {
$g=0;}
$q_sortg = "SELECT id_movie, title, image, plot, rating, id_genre, genre
FROM movie_genre AS mg
INNER JOIN movie AS m ON mg.movie_id_movie = m.id_movie
INNER JOIN genre AS g ON mg.genre_id_genre = g.id_genre
WHERE id_genre = '$g'";
$r_sortg = mysqli_query($dbc, $q_sortg);
$i=0;
echo '<div class="row row-eq-height">';
while ($row_sortg = mysqli_fetch_array($r_sortg)) {
if($row_sortg['id_genre'] == $g) {
echo '<div class="col-sm-3 col-lg-3 col-md-3 column">';
echo '<div class="thumbnail movie-box">';
echo '<img src="data:image/jpg;base64,'.base64_encode($row_sortg['image']).'" width="250px" height="300px" />';
echo '<div class="caption">';
echo "<h4 class=\"title-subs\"><a href='movies_info.php?m=".$row_sortg['id_movie']."'>".$row_sortg['title']."</a></h4>";
echo "<p class=\"title-subs\">".$row_sortg['plot']."</p>";
echo "</div>";
echo '<div class="ratings align-center">';
echo '<p>';
$ratings = $row_sortg['rating'];
$rates = array('$row_sortg' => $ratings);
foreach ($rates as $r => $value) {
if($value <= 1.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 2.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 3.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 4.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 5.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 6.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 7.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 8.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 9.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
}
else if($value <= 10.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
}
}//fecha o foreach
echo '</p>';
echo '</div>';
echo "</div>";
echo "</div>";
$i++;
if ($i%4 == 0){
echo '</div><div class="row row-eq-height">';
} //fecha o if
} //fecha o While
}
?>
Here's the link to the current website:
http://www.zoomlab.pt/2016-web2/pub/grupo4/index.php
In the first page that appears its possible to sort by with no problem.
The problem is when you click in one of the genres in the left column. They appear ordered by genre and then I cant get the sort by to work...
I was initially trying to use bootstrap dropdown but it uses <li><a> instead of <select> and <option>, so I didn't know if it was possible.
Thanks in advance for any help possible.
It looks like you just need to resubmit the form once a user changes the sort option? If so, you'd need to use JavaScript to add an onchange event on your <select>, which will resubmit the form:
<script>
window.addEventListener('DOMContentLoaded', function () {
var form = document.querySelector('form.sortby_form'),
select = form.querySelector('select[name="sort_by"]');
select.addEventListener('change', function () {
form.submit();
});
});
</script>
Also, if this is for a production web site, you really should not use raw user input as part of a SQL query. See: http://php.net/manual/en/mysqli-stmt.bind-param.php

If statement inside echoed html not showing inside the html

I want to shorten a string when it's longer than a certain length for example 20.
But when I add the if statement to my echoed html it outputs the result outside of the echoed html. Any idea why this happens?
My code:
foreach($contentcr as $content)
{
$contentje .= '<li class="job_listing">
<a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'">
<div class="location job_img">
<img src="images/samples/person1.jpg" alt="" class="company_logo">
</div>
<div class="location boldfont">
'.$content['title'].'
</div>
<div class="location">
'.$content['fulltext'].'';
if (strlen($content['fulltext']) >= 20){
echo 'blala';
}
$contentje .='</div>
<div class="rating location">
<div class="rating-stars">
<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>
</div>
<div class="location">
'.$content['metakey'].'
</div>
<div class="location">
<button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button>
</div>
</a>
</li>'; // Here
}
This is a table like structure, but the output of the if statement is echoed outside the html markup.
You are echoing the string inside if statement rather than appending that to your $contentje variable. See if statement below:
foreach($contentcr as $content)
{
$contentje .= '<li class="job_listing">
<a href="http://www.website.nl/_extern/website/content.php?alias='.$content['alias'].'">
<div class="location job_img">
<img src="images/samples/person1.jpg" alt="" class="company_logo">
</div>
<div class="location boldfont">
'.$content['title'].'
</div>
<div class="location">
'.$content['fulltext'].'';
if (strlen($content['fulltext']) >= 20){
$contentje .= 'blala';
}
$contentje .='</div>
<div class="rating location">
<div class="rating-stars">
<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>
</div>
<div class="location"> '.$content['metakey'].'</div>
<div class="location">
<button type="submit" class="btn btn-primary buttonblock" onClick="location.href="http://www.website.nl/_extern/website/content.php?page="'.$content['alias'].'">Nu huren</button>
</div>
</a></li>'; // Here
}

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