savi16;10971171 wrote:Try this now 🙂
I tested it and it's kinda working fine 🙂 I can't test it exactly how you would as I ain't got your course_categories table structure
<?php
$sqla = "SELECT id, name FROM ".$prefix."course_categories WHERE visible = '1' AND parent = '0' ORDER BY name ASC"; //first quering all the parent cats
$mysqla = mysql_query($sqla, $link);
while($parent = mysql_fetch_array($mysqla)) {
echo "<option value=\"".$parent["id"]."\">";
echo $parent["name"];
echo "</option>"; //putting your parent cat as the first position on the list and going through it's children categories
$sqlb="SELECT id, path, name FROM ".$prefix."course_categories WHERE visible = '1' AND path LIKE '/".$parent["id"]."/%' ORDER BY name ASC";
$mysqlb = mysql_query($sqlb, $link);
while($var = mysql_fetch_array($mysqlb)) {
$path = explode("/", $var["path"]);
echo "<option value=\"".$var["id"]."\">";
if(count($path) > 2) {
$temp = array_shift($path); //don't need the parent cat
$temp = array_pop($path); //don't need the current cat either
foreach($path as $ids) {
$nextsql = "SELECT name FROM ".$prefix."course_categories WHERE id = '$ids'";
$name = mysql_fetch_array(mysql_query($nextsql, $link));
echo $name["name"].'/';
}
}
echo $var["name"]."</option>";
}
}
?>
Thank you. You really are very kind for helping me like this. I really appreciate it. The sorting works fine - only problem is that I sometimes need to show subcategories (without a parent) because I'm trying to make a toggle-tool that toggles "visible" between "0" and "1".
This is my code:
<?php
$host = "...";
$username = "...";
$password = "...";
$databasename = "...";
$prefix = "mdl_";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Toggle</title>
<script>
function check_synlige_kategorier() {
if(document.synlige_kategorier["skjul_kategorier[]"].value == "")
{alert('Please pick categories to hide!');
return false; }
return true;
}
function check_skjul_kategorier() {
if(document.skjul_kategorier["vis_kategorier[]"].value == "")
{alert('Please pick categories to show!');
return false; }
return true;
}
</script>
</head>
<body>
<table>
<tr>
<td valign=top>
<b>Visible categories</b>
<form name="synlige_kategorier" action="" method="post" onSubmit="return check_synlige_kategorier();">
<select name="skjul_kategorier[]" multiple="multiple" size=15 style="width: 500px">
<?php
$con = mysql_connect($host, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($databasename, $con);
mysql_query("set names 'utf8';");
// HVIS DER SUBMITTES VIS "VENT VENLIGST"
if ((isset($_POST['submit_skjul_kategorier'])) || (isset($_POST['submit_vis_kategorier'])))
{
?>
<option value="">Please wait...</option>
<?php } else { // ELLERS TRÆK KATEGORIER UD
$sqla = "SELECT id, name FROM ".$prefix."course_categories WHERE visible = '1' AND parent = '0' ORDER BY name ASC"; //first quering all the parent cats
$mysqla = mysql_query($sqla, $con);
while($parent = mysql_fetch_array($mysqla))
{
echo "<option value=\"".$parent["id"]."\">";
echo $parent["name"];
echo "</option>"; //putting your parent cat as the first position on the list and going through it's children categories
$sqlb="SELECT id, path, name FROM ".$prefix."course_categories WHERE visible = '1' AND path LIKE '/".$parent["id"]."/%' ORDER BY name ASC";
$mysqlb = mysql_query($sqlb, $con);
while($var = mysql_fetch_array($mysqlb)) {
$path = explode("/", $var["path"]);
echo "<option value=\"".$var["id"]."\">";
if(count($path) > 2) {
$temp = array_shift($path); //don't need the parent cat
$temp = array_pop($path); //don't need the current cat either
foreach($path as $ids) {
$nextsql = "SELECT name FROM ".$prefix."course_categories WHERE id = '$ids'";
$name = mysql_fetch_array(mysql_query($nextsql, $con));
echo $name["name"].'/';
}
}
echo $var["name"]."</option>";
}
}
} // ELSE END
?>
</select><br>
<input type="submit" value="Hide categories »" name="submit_skjul_kategorier"/> (Subcategories are automatically included)
</form>
<?php
if (isset($_POST['skjul_kategorier']))
{
$skjul_kategorier=$_POST['skjul_kategorier'];
if ($skjul_kategorier){
foreach ($skjul_kategorier as $skjult)
{
mysql_query("UPDATE ".$prefix."course_categories SET visible = '0' WHERE id = '$skjult'");
mysql_query("UPDATE ".$prefix."course_categories SET visible = '0' WHERE path LIKE '%/".$skjult."/%'");
}
}
echo "Making categories hidden. Please wait...";
echo "<meta http-equiv='Refresh' content='1.5; URL=_toggle.php'>";
}
?>
</td>
<td width="20">
</td>
<td valign=top>
<b>Hidden categories</b>
<form action="" name="skjul_kategorier" method="post" onSubmit="return check_skjul_kategorier();">
<select name="vis_kategorier[]" multiple="multiple" size=15 style="width: 500px">
<?php
if ((isset($_POST['submit_skjul_kategorier'])) || (isset($_POST['submit_vis_kategorier'])))
{
?>
<option value="">Please wait...</option>
<?php } else {
$sqlc = "SELECT id, name FROM ".$prefix."course_categories WHERE visible = '0' AND parent = '0' ORDER BY name ASC"; //first quering all the parent cats
$mysqlc = mysql_query($sqlc, $con);
while($parent = mysql_fetch_array($mysqlc))
{
echo "<option value=\"".$parent["id"]."\">";
echo $parent["name"];
echo "</option>"; //putting your parent cat as the first position on the list and going through it's children categories
$sqld="SELECT id, path, name FROM ".$prefix."course_categories WHERE visible = '0' AND path LIKE '/".$parent["id"]."/%' ORDER BY name ASC";
$mysqld = mysql_query($sqld, $con);
while($var = mysql_fetch_array($mysqld)) {
$path = explode("/", $var["path"]);
echo "<option value=\"".$var["id"]."\">";
if(count($path) > 2) {
$temp = array_shift($path); //don't need the parent cat
$temp = array_pop($path); //don't need the current cat either
foreach($path as $ids) {
$nextsql = "SELECT name FROM ".$prefix."course_categories WHERE id = '$ids'";
$name = mysql_fetch_array(mysql_query($nextsql, $con));
echo $name["name"].'/';
}
}
echo $var["name"]."</option>";
}
}
} // ELSE END
?>
</select><br>
<input type="submit" value="« Show categories" name="submit_vis_kategorier"/> (Underkategorier medtages automatisk)
</form>
<?php
if (isset($_POST['vis_kategorier']))
{
$vis_kategorier=$_POST['vis_kategorier'];
if ($vis_kategorier){
foreach ($vis_kategorier as $vist)
{
mysql_query("UPDATE ".$prefix."course_categories SET visible = '1' WHERE id = '$vist'");
mysql_query("UPDATE ".$prefix."course_categories SET visible = '1' WHERE path LIKE '%/".$vist."/%'");
}
}
echo "Making categories visible! Please wait...";
echo "<meta http-equiv='Refresh' content='1.5; URL=_toggle.php'>";
}
mysql_close($con);
?>
</td>
</tr>
</table>
</body>
</html>
This is the database table (SQL):
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
CREATE TABLE IF NOT EXISTS `mdl_course_categories` (
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`description` text COLLATE utf8_unicode_ci,
`parent` bigint(10) unsigned NOT NULL DEFAULT '0',
`sortorder` bigint(10) unsigned NOT NULL DEFAULT '0',
`coursecount` bigint(10) unsigned NOT NULL DEFAULT '0',
`visible` tinyint(1) NOT NULL DEFAULT '1',
`timemodified` bigint(10) unsigned NOT NULL DEFAULT '0',
`depth` bigint(10) unsigned NOT NULL DEFAULT '0',
`path` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`theme` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `mdl_courcate_par_ix` (`parent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Course categories' AUTO_INCREMENT=12 ;
INSERT INTO `mdl_course_categories` (`id`, `name`, `description`, `parent`, `sortorder`, `coursecount`, `visible`, `timemodified`, `depth`, `path`, `theme`) VALUES
(5, '5. semester', '', 0, 5, 1, 1, 0, 1, '/5', NULL),
(2, '1.semester', '', 0, 1, 1, 1, 0, 1, '/2', NULL),
(3, '3. semester', '', 0, 3, 3, 1, 0, 1, '/3', NULL),
(4, 'Underkategori_1_i_3.semester', '', 3, 999, 1, 1, 0, 2, '/3/4', NULL),
(6, 'Underkategori_1.1_i_3.semester', '', 4, 999, 0, 1, 0, 3, '/3/4/6', NULL),
(7, 'Underkategori_1_i_1.semester', '', 2, 999, 0, 1, 0, 2, '/2/7', NULL),
(8, 'Underkategori_2_i_3.semester', '', 3, 999, 0, 1, 0, 2, '/3/8', NULL),
(9, '2.semester', '', 0, 2, 0, 1, 0, 1, '/9', NULL),
(10, '4.semester', '', 0, 4, 0, 1, 0, 1, '/10', NULL);
If you would like to see/test it please do :-)
The thing is - if I move (hide) a subcategory it will not show, because of the "AND parent = '0'" in the query. Is there a way around this? Soooo close to the finish line :-) Crossing fingers, hoping for yet another reply from you :-D