gegevens uit database halen
Heb onderstaande 2 functies om gegevens uit de database te halen. Functie 1 is makkelijk en werkt prima. Hier hoeft hij alleen maar categorieen uit de database te halen.
Functie 2 werkt niet. Hij moet eerst de categorieen uit de database halen en daarna de subcategorieen. En die onder elkaar zetten. Dus hoofdcategorie en dan de bijbehorende subcategorieen.
Hieronder 1:
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
function loadmainCat($link,$table_cat) {
$query = "SELECT * FROM $table_cat WHERE subid =0";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
return arrayMaker($result,MYSQL_ASSOC);
}
?>
function loadmainCat($link,$table_cat) {
$query = "SELECT * FROM $table_cat WHERE subid =0";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
return arrayMaker($result,MYSQL_ASSOC);
}
?>
Hieronder 2:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
function loadCat($link,$table_cat) {
// let's first get the list of main categories from the categories table.
$query = "SELECT * FROM zomplog_cat";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
// make an array of the main categories
$cat = arrayMaker($result,MYSQL_ASSOC);
$data = mysql_query("SELECT ".$query);
// now let's start looping through the subcategories
foreach($cat as $thecat){
echo $thecat['name'];
// get the subcategories based on the id of the main-categories
// in your case the query would be: $query = "SELECT * FROM zomplog_subcat WHERE subid = $thecat[id]";
// ok, let's go
$query = "SELECT * FROM zomplog_subcat";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
$subcat = arrayMaker($result,MYSQL_ASSOC);
// echo the main category
echo $thecat['name'];
foreach ($subcat as $subcat){
// echo the subcategories
echo $subcat['name'];
}
}
}
?>
function loadCat($link,$table_cat) {
// let's first get the list of main categories from the categories table.
$query = "SELECT * FROM zomplog_cat";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
// make an array of the main categories
$cat = arrayMaker($result,MYSQL_ASSOC);
$data = mysql_query("SELECT ".$query);
// now let's start looping through the subcategories
foreach($cat as $thecat){
echo $thecat['name'];
// get the subcategories based on the id of the main-categories
// in your case the query would be: $query = "SELECT * FROM zomplog_subcat WHERE subid = $thecat[id]";
// ok, let's go
$query = "SELECT * FROM zomplog_subcat";
$result = mysql_query ($query, $link) or die("Died getting info from db. Error returned if any: ".mysql_error());
$subcat = arrayMaker($result,MYSQL_ASSOC);
// echo the main category
echo $thecat['name'];
foreach ($subcat as $subcat){
// echo the subcategories
echo $subcat['name'];
}
}
}
?>
Er zijn nog geen reacties op dit bericht.