forum.coppermine-gallery.net

Support => cpg1.6.x Support => cpg1.6 themes (visuals) => Topic started by: allvip on November 06, 2019, 01:10:41 am

Title: Make subcategories from scratch (from database) failed
Post by: allvip on November 06, 2019, 01:10:41 am
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:

Code: [Select]
<?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.
Title: Re: Make subcategories from scratch (from database) failed
Post by: ron4mac on November 07, 2019, 01:08:40 pm
CPG takes care of the database connection, so all you would need is something like:

Code: [Select]
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)
Title: Re: Make subcategories from scratch (from database) failed
Post by: allvip on November 07, 2019, 02:33:06 pm
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.
Title: Re: Make subcategories from scratch (from database) failed
Post by: allvip on November 17, 2019, 08:33:11 am
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.

Code: [Select]
echo $output . '</li></ul>';

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

Code: [Select]
$output =  <<<EOT 
....
EOT;
Title: Re: Make subcategories from scratch (from database) failed
Post by: 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}".
Title: Re: Make subcategories from scratch (from database) failed
Post by: allvip on November 17, 2019, 07:59:37 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.

Code: [Select]
function my() {
 your code here
}
$var = my();
$template_vars['{CATS}'] = $var;
Title: Re: Make subcategories from scratch (from database) failed
Post by: allvip on September 24, 2020, 01:52:39 pm
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

Code: [Select]
$template_vars = CPGPluginAPI::filter('theme_pageheader_params', $template_vars);
PASTE

Code: [Select]
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

Code: [Select]
{CATEGORY_MENU}