Support > cpg1.6 themes (visuals)

Make subcategories from scratch (from database) failed

(1/2) > >>

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:


--- Code: ---<?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_SERVER, DB_USER, DB_PASS, DB_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>';
?>

--- End code ---

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:


--- Code: ---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();


--- End code ---
(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.


--- Code: ---echo $output . '</li></ul>';

--- End code ---

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


--- Code: ---$output =  <<<EOT 
....
EOT;

--- End code ---

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}".

Navigation

[0] Message Index

[#] Next page

Go to full version