Advanced search  

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Pages: [1]   Go Down

Author Topic: Make subcategories from scratch (from database) failed  (Read 431 times)

0 Members and 1 Guest are viewing this topic.

allvip

  • Global Moderator
  • Coppermine addict
  • *****
  • Country: 00
  • Offline Offline
  • Posts: 1326
Make subcategories from scratch (from database) failed
« 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.
Logged

ron4mac

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: us
  • Offline Offline
  • Posts: 1344
Re: Make subcategories from scratch (from database) failed
« Reply #1 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)
Logged

allvip

  • Global Moderator
  • Coppermine addict
  • *****
  • Country: 00
  • Offline Offline
  • Posts: 1326
Re: Make subcategories from scratch (from database) failed
« Reply #2 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.
Logged

allvip

  • Global Moderator
  • Coppermine addict
  • *****
  • Country: 00
  • Offline Offline
  • Posts: 1326
Re: Make subcategories from scratch (from database) failed
« Reply #3 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;
Logged

ron4mac

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: us
  • Offline Offline
  • Posts: 1344
Re: Make subcategories from scratch (from database) failed
« Reply #4 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}".
Logged

allvip

  • Global Moderator
  • Coppermine addict
  • *****
  • Country: 00
  • Offline Offline
  • Posts: 1326
Re: Make subcategories from scratch (from database) failed
« Reply #5 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;
Logged
Pages: [1]   Go Up
 

Page created in 0.018 seconds with 20 queries.