How do i handle this dividing modulus error? - php

I have a function that generates a category menu.
What i'm trying to do is to group two category in one ul.
The problem is almos fixed but i'm getting an empty ul ( see the pic).
How can I fix this ?
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}

Try this:
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0 && !empty($i)){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}

just start counting from -1
$i = -1;

Related

youtube class video limit not working

I want to change the video limit on my home page (getpopularvideos), but when I change the limit, it always only shows 20 videos.
What am I doing wrong?
protected function getPopularVideos($limit = NULL){
// Set Limit
if(!is_null($limit)) $this->limit = $limit;
// Construct URL
$part = "snippet,contentDetails,statistics";
$this->url = "https://www.googleapis.com/youtube/v3/videos?key={$this->key}&part=$part&chart=mostPopular&maxResults={$this->limit}";
if(!is_null($this->page)) $this->url = $this->url."&pageToken={$this->page}";
// Get Data
$data = Main::cache_get("popular_{$this->page}");
if($data == NULL){
$data = $this->http(TRUE);
Main::cache_set("popular_{$this->page}", $data); $limit = 24;
}
$i = 1;
$html = "<ul class='media'>";
foreach ($data->items as $yt) {
$html .= $this->listVideo($yt);
if($i%24 == 0) {
$html .= "</ul>{$this->ads(728)}<ul class='media'>";
}
$i ++;
}
$html .= "</ul>";
$this->pagination = "<ul class='list-inline'>";
if(isset($data->prevPageToken)){
$this->pagination .= "<li ><a href='{$this->config["url"]}/popular/{$data->prevPageToken}' class='btn btn-ghost'><i class='btn-icon btn-icon--left ninety9lives-youtube-play'></i> Previous</a></li>";
}
if(isset($data->nextPageToken)){
$this->pagination .= "<li ><a href='{$this->config["url"]}/popular/{$data->nextPageToken}' class='btn btn-ghost'><i class='btn-icon btn-icon--left ninety9lives-youtube-play'></i> Next</a></li>";
}
$this->pagination .= "</ul>";
return $html;
}
protected function getUserVideos4Home($q, $limit = NULL){
if(!is_null($limit)) $this->limit = $limit;
$part = "snippet";
$this->url = "https://www.googleapis.com/youtube/v3/search?key={$this->key}&part=$part&order=date&type=video&channelId={$q}&maxResults={$this->limit}";
if(!is_null($this->page)) $this->url = $this->url."&pageToken={$this->page}";
$data = $this->http(TRUE);
$i = 1;
$return = new stdClass();
$return->title = $data->items[0]->snippet->channelTitle;
$html .= "";
$vids = array();
foreach ($data->items as $yt){
$vids[] = $yt->id->videoId;
}
$videos2 = implode(",", $vids);
$part = "snippet,contentDetails,statistics";
$this->url = "https://www.googleapis.com/youtube/v3/videos?key={$this->key}&part={$part}&id={$videos2}";
$data2 = $this->http(TRUE);
$durations = Main::durations($vids);
foreach ($data->items as $yt) {
$html .= $this->listSearchVideo12($yt,$durations[$yt->id->videoId]);
if($i%6 == 0) {
$html .= "</div><div class='adblock'><div class='img'>Google AdSense<br>336 x 280</div></div><div class='list'>";
}
$i ++;
}
$html .= "";
return $html;
}

menu only showing the first child php

i have this code:
$Maxcount = 10;$count = 1;$query = "SELECT pages.paid,
pages.pag_desc,
pages.isActive,
pages.pag_pag_id,
pages.pag_type,
parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) { if ($count === 1) {
$query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
$query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid " }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id
INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid
WHERE uid = '$uid' AND $where AND pages.isActive=1";
$result = $db->query($query);
if (mysqli_num_rows($result)) {
$menu = array();
while ($row = $result->fetch_assoc()) {
$menu[] = $row;
}
}
and for showing the menu i used this recursive function :
foreach($menu as $row){
$x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc'];
}function recursive($x, $out) { if (is_array($x)){
//$out .= "<ul>";
foreach($x as $i => $data) {
if ($i === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">';
}
else
{
$out .= '<a href="#">';
}
$out .= $i;
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
the result is that its showing only the parent and the first child but in database the child have another child etc...
for example:
parent user have a child admin and admin have another child called editor
but its only showing user as a parent and admin as a child
any help please? thank u in advance

Php Array list items with different class

This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }

Why the format via php is not preserved in a selectbox PHP

Hello im trying to achieve this
http://phpfiddle.org/main/code/jw3-s1j
Actually i think its working fine.. But you CANT difference between a category and a subcategory like in the example before..
So how can i have a select box with a tree inside???
<?PHP
$result = mysql_query("SELECT id, parent_id, name FROM category")or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);; // This line executes the MySQL query that you typed above
$datas = array(); // make a new array to hold all your data
while($row = mysql_fetch_assoc($result)) {
$datas[] = $row;
}
function generatePageTree($datas, $depth = 0, $parent = 0){
if($depth > 1000) return ''; // Make sure not to have an endless recursion
$tree = '';
for($i=0, $ni=count($datas); $i < $ni; $i++){
if($datas[$i]['parent_id'] == $parent){
$tree .= str_repeat('-', $depth);
$tree .= '<option value=\''.$datas[$i]['name'] . '\'>'.$datas[$i]['name'] . '</option><br/>';
$tree .= generatePageTree($datas, $depth+1, $datas[$i]['id']);
}
}
return $tree;
}
echo '<select>';
echo(generatePageTree($datas));
echo '</select>';
?>
Solved!!
Fixed Code:
function generatePageTree($datas, $depth = 0, $parent = 0){
if($depth > 1000) return ''; // Make sure not to have an endless recursion
$tree = '';
for($i=0, $ni=count($datas); $i < $ni; $i++){
if($datas[$i]['parent_id'] == $parent){
$tree .= str_repeat('-', $depth);
$tree .= '<option value=\''.$datas[$i]['name'] . '\'>'.str_repeat('-', $depth).$datas[$i]['name'] . '</option><br/>';
$tree .= generatePageTree($datas, $depth+1, $datas[$i]['id']);
}
Solved!!
Fixed Code:
function generatePageTree($datas, $depth = 0, $parent = 0){
if($depth > 1000) return ''; // Make sure not to have an endless recursion
$tree = '';
for($i=0, $ni=count($datas); $i < $ni; $i++){
if($datas[$i]['parent_id'] == $parent){
$tree .= str_repeat('-', $depth);
$tree .= '<option value=\''.$datas[$i]['name'] . '\'>'.str_repeat('-', $depth).$datas[$i]['name'] . '</option><br/>';
$tree .= generatePageTree($datas, $depth+1, $datas[$i]['id']);
}

Convert 3D array into a HTML table

ARRAY
$array['1'] = null;
$array['2']['21'] = null;
$array['3']['31'] = null;
$array['3']['32'] = null;
$array['4']['41'] = null;
$array['4']['42']['421'] = null;
$array['4']['42']['422'] = null;
$array['5']['51'] = null;
$array['5']['52'] = null;
$array['5']['53']['531'] = null;
$array['5']['53']['532'] = null;
$array['5']['53']['533'] = null;
$array['6']['61']['611'] = null;
$array['6']['62'] = null;
$array['6']['63']['631'] = null;
$array['6']['63']['632'] = null;
$array['6']['63']['633'] = null;
$array['6']['63']['634'] = null;
$array['7']['71']['711'] = null;
$array['8']['81']['811'] = null;
$array['8']['81']['812'] = null;
$array['9']['91']['911'] = null;
$array['9']['91']['912'] = null;
$array['9']['92']['921'] = null;
$array['9']['92']['922'] = null;
I'm trying to convert array above into a HTML table. I need to add rowspan to get output like shown as image below but run out of juice.
PHP
//If condition is satistied
if (isset($array) && count($array) > 0)
{
//echo '<pre>'; print_r($array); exit;
//Initiate variables
$row = null;
$level_one_array = $array;
//Iterate through menu level one
foreach ($level_one_array as $level_one_name => $level_two_array)
{
//If level two is an array
if (is_array($level_two_array))
{
//Iterate through menu level two
foreach ($level_two_array as $level_two_name => $level_three_array)
{
//If level two is an array
if (is_array($level_three_array))
{
//Iterate through menu level three
foreach ($level_three_array as $level_three_name => $null)
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data">' . $level_three_name . '</td>';
$row .= '</tr>';
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
$table = '<table>';
$table .= '<tr><td class="td_title">Menus</td><td class="td_title">Submenus/Items</td><td class="td_title">Items</td></tr>';
$table .= $row;
$table .= '</table>';
echo $table;
}
else
{
echo '<center>No record found</center>';
}
WHAT I NEED IS THIS:
I wrote new code that will do the trick. Working with rowspan is very tricky. The key elements are calculate_rowspan() and the flag $on_level_X_row to determine whether or not it is necessary to add <tr>.
It was necessary to detach the output from the most inner foreach because you have to handle multiple rows with it. Also I added "\n" after each </tr> to make the output a little more human readable.
function calculate_rowspan($array)
{
if (!is_array($array) || count($array) == 0)
return 1;
$rowspan = 0;
foreach ($array as $key=>$value)
{
$rowspan++;
if (is_array($value) && count($value) > 0)
{
$rowspan += count($value);
// -1 because first element is one same row
$rowspan--;
}
}
return $rowspan;
}
if (is_array($array) && count($array) > 0)
{
$output = '';
foreach ($array as $level_one_name => $level_two_array)
{
if (!is_array($level_two_array) || count($level_two_array) == 0)
{
$output .= '<tr><td>';
$output .= $level_one_name;
$output .= '</td><td></td><td></td></tr>'."\n";
}
else
{
$output .= '<tr>';
$output .= '<td rowspan="'.calculate_rowspan($level_two_array).'">';
$output .= $level_one_name;
$output .= '</td>';
$on_level_one_row = TRUE;
foreach ($level_two_array as $level_two_name => $level_three_array)
{
if ( ! $on_level_one_row)
$output .= '<tr>';
else
$on_level_one_row = FALSE;
if (!is_array($level_three_array) || count($level_three_array) == 0)
{
$output .= '<td>';
$output .= $level_two_name;
$output .= '</td><td></td></tr>'."\n";
}
else
{
$output .= '<td rowspan="'.calculate_rowspan($level_three_array).'">';
$output .= $level_two_name;
$output .= '</td>';
$on_level_two_row = TRUE;
foreach ($level_three_array as $level_three_name => $null)
{
if ( ! $on_level_two_row)
$output .= '<tr>';
else
$on_level_two_row = FALSE;
$output .= '<td>';
$output .= $level_three_name;
$output .= '</td></tr>'."\n";
}
}
}
}
}
print '<table>'."\n";
print $output;
print '</table>';
}

Categories