Make subcategories from scratch (from database) failed Make subcategories from scratch (from database) failed
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

Make subcategories from scratch (from database) failed

Started by allvip, November 06, 2019, 01:10:41 AM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

allvip

HI.

I made it to make a menu with all categories and subcategorie that works fine but not in theme.php

The file (sub-items.php) is in the root of my localhost and the code is:


<?php
define("DB_SERVER""localhost");
define("DB_USER""root");
define("DB_PASS""pass");
define("DB_NAME""cpg16");
  
// 1. Create a database connection
  
$connection mysqli_connect(DB_SERVERDB_USERDB_PASSDB_NAME);
  
// Test if connection succeeded
  
if(mysqli_connect_errno()) {
    die(
"Database connection failed: " 
         
mysqli_connect_error() . 
         
" (" mysqli_connect_errno() . ")"
    
);
  }
function 
db_query($db_query) {
if (!$db_query) {
die("Database query failed.");
}
}
$query "SELECT * FROM cpg16x_categories WHERE parent = '0'";
$result mysqli_query($connection$query);
db_query($result);
$result mysqli_fetch_all($result,MYSQLI_ASSOC);
foreach(
$result as $row) {
 
$output '';
 
$output .= '
 <ul class="main-category"><li>By <b>'
.$row["cid"].'</b> on <i>'.$row["parent"].'</i>';
 
$output .= get_cats($connection$row["cid"]);
echo 
$output '</li></ul>'
}
function 
get_cats($connection$parent 0$marginleft 0) {
 
$query "SELECT * FROM cpg16x_categories WHERE parent = '".$parent."'";
 
$output '';
 
$result mysqli_query($connection$query);
 
db_query($result);
$result1 mysqli_fetch_all($result);
$count =  mysqli_num_rows($result);
 if(
$count 0) {
  foreach(
$result as $row) {
   
$output .= '<ul class="sub-category"><li>By <b>'.$row["cid"].'</b> on <i>'.$row["parent"].'</i>';
   
$output .= get_cats($connection$row["cid"], $marginleft);
  }
 }
 return 
$output '</li></ul>';
}
echo 
'<style>
.main-category{background-color:green;padding:5px;}.sub-category{background-color:blue;padding:5px;}
</style>'
;
?>



I replaced:

cpg16x_categories with {$CONFIG['TABLE_CATEGORIES']}
mysqli_query with cpg_db_query and others like cpg_db_num_rows

I added in get_cats()

global $CONFIG;

BUT NOTHING.
Not workin in theme.php
Maybe because of $result = mysqli_fetch_all($result,MYSQLI_ASSOC);
Not accepted by coppermine.
Need some help.
I have nested sub-sub categories.

ron4mac

CPG takes care of the database connection, so all you would need is something like:

function myGetSubCats ($parent, $margin=0)
{
    global $CONFIG;

    $result = cpg_db_query('SELECT * FROM '.$CONFIG['TABLE_CATEGORIES'].' WHERE parent='.$parent);

    $output = '';
    while ($row = $result->fetchAssoc()) {
        $output .= '<ul class="sub-category"><li>By <b>'.$row['cid'].'</b> on <i>'.$row['parent'].'</i>';
        $output .= myGetSubCats($row['cid'], $margin + 1);
    }
    $result->free();

    return $output . '</li></ul>';
}

$result = cpg_db_query('SELECT * FROM '.$CONFIG['TABLE_CATEGORIES'].' WHERE parent=0');

while ($row = $result->fetchAssoc()) {
    $output = '<ul class="main-category"><li>By <b>'.$row['cid'].'</b> on <i>'.$row['parent'].'</i>';
    $output .= myGetSubCats($row['cid'], 1);
    echo $output . '</li></ul>';
}
$result->free();


(un-tested)

allvip

Thanks a lot.
You are awesome  :)

There is one more problem: it shows right after the <body>
Can not added to pageheder $template_vars['{CATEGORY_MENU}'] = $myvar;
Is a mess. Not showing or error.
$template_vars['{CATEGORY_MENU}'] in the while loop, still a mess.

allvip

I wrapped all the code in a function and added it as a template var but the fact that is echoing from the while loop makes the code show in the head not in the body.


echo $output . '</li></ul>';


Any ideeas?
Maybe I shoud use EOT but don't really know how.


$output =  <<<EOT 
....
EOT;

ron4mac

The code I provided above used echo because you were working oniy in a stand-alone mode. In a theme.php file you would not use echo but instead collect the output in a variable that afterwards gets applied to a template variable, such as "{CATEGORY_MENU}".

allvip

Quote from: ron4mac on November 17, 2019, 02:02:49 PM
The code I provided above used echo because you were working oniy in a stand-alone mode. In a theme.php file you would not use echo but instead collect the output in a variable that afterwards gets applied to a template variable, such as "{CATEGORY_MENU}".

I did and added the token to template.html but ruins head and body.
Outputs on the top of the page.


function my() {
your code here
}
$var = my();
$template_vars['{CATS}'] = $var;

allvip

Solution:

1) in function pageheader in theme.php (copy the function from themes/sample/theme.php if you don't have it in your_theme_name/theme.php

Before

$template_vars = CPGPluginAPI::filter('theme_pageheader_params', $template_vars);

PASTE


function myGetSubCats ($parent, $margin=0) {
    global $CONFIG;
    $result = cpg_db_query('SELECT * FROM '.$CONFIG['TABLE_CATEGORIES'].' WHERE parent='.$parent);
    $output = '';
    while ($row = $result->fetchAssoc()) {
        $output .= '<ul class="sub-category"><li><a href="index.php?cat='.$row['cid'].'">'.$row['name'].'</a>';
        $output .= myGetSubCats($row['cid'], $margin + 1);
$output .= '</li></ul>';
    }
    return $output;
    $result->free();
}
$result = cpg_db_query('SELECT * FROM '.$CONFIG['TABLE_CATEGORIES'].' WHERE parent=0');
while ($row = $result->fetchAssoc()) {
    $output .= '<ul class="main-category"><li><a href="index.php?cat='.$row['cid'].'">'.$row['name'].'</a>';
    $output .= myGetSubCats($row['cid'], 1);
$output .= '</li></ul>';
}
$template_vars['{CATEGORY_MENU}'] = $output;
$result->free();


2) ADD in your_theme_name/template.html at the desired location


{CATEGORY_MENU}