/    Sign up×
Community /Pin to ProfileBookmark

Problem with a function…

Hey look, i have a table named “cats” with 3 columns: “parent”, “ID” and “categ”… This is about categories and sub categories…
the column “parent” refers to the ID of an existent entry…
the column “ID” refers to it’s ID…
and “categ” is its name…
so if i have this
ID – categ – parent
1 – abc – 0
2 – def – 1

the category “abc” will be in the root category and the entry “def” will be a subcategory of the “abc” category

Now… I was makin a function for filling a combobox with all the categories, BUT if theres a subcategory, i’d like to put it below it’s parent category and depending of it’s sublevel, add “-” (i.e. if it’s sublevel is 3 then add “—“)
So this is the code:

[code=php]
<?php
function categorias($parent=0, $sub=0, $exc=array(“0”)) {
global $catID;
$sql = “SELECT * FROM cats WHERE parent = ‘”.$parent.”‘”;
$doSql = @mysql_query($sql);
while ($row_doSql = @mysql_fetch_array($doSql)) {
$result .= ($row_doSql[‘ID’] == $catID) ? ‘<option value=”‘.$row_doSql[‘ID’].'” selected>’ : ‘<option value=”‘.$row_doSql[‘ID’].'”>’;
for ($i=0; $i<$sub; $i++) {
$result .= “-“;
}
$result .= $row_doSql[‘categ’];
$result .= ‘</option>’.”n”;
}

$sql2 = “SELECT * FROM cats WHERE”;
foreach ($exc as $k => $v) {
$sql2 .= ” parent != ‘”.$v.”‘ AND”;
}
$sql2 = substr($sql2,0,strlen($sql2)-4);

$doSql2 = @mysql_query($sql2);
while ($row_doSql2 = @mysql_fetch_array($doSql2)) {
$exc[] = $row_doSql2[‘parent’];
$result .= categorias($row_doSql2[‘parent’], $sub+1, $exc);
$sql3 = “SELECT * FROM cats WHERE ID = ‘”.$row_doSql2[‘parent’].”‘ LIMIT 1″;
$doSql3 = @mysql_query($sql3);
$row_doSql3 = @mysql_fetch_array($doSql3);
$parent = $row_doSql3[‘parent’];
$sub=0;
}
return $result;
}
?>
[/code]

The problem is that the subcategory appears at the bottom not below it’s parent category…
How could i fix this..??
Thanx…

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJan 05.2006 — unfortunately in mysql it's a real pain

first you select all of the parent categories:

"SELECT * FROM your_table WHERE parent=0"

then, in each of the results, you have to issue another query for all the sub categories

so let's say this query returns 1-abc-0 Take the 1 (the ID column)

"SELECT *
FROM your_table WHERE parent=1

and you'd do that for each row in the first query.

in other databases, it's much more streamlined.
Copy linkTweet thisAlerts:
@delr2691authorJan 05.2006 — oh... but that's what i'm doing...

the failure i think is in this part:
[code=php]
$doSql2 = @mysql_query($sql2);
while ($row_doSql2 = @mysql_fetch_array($doSql2)) {

$exc[] = $row_doSql2['parent'];
$result .= categorias($row_doSql2['parent'], $sub+1, $exc);

$sql3 = "SELECT * FROM cats WHERE ID = '".$row_doSql2['parent']."' LIMIT 1";
$doSql3 = @mysql_query($sql3);
$row_doSql3 = @mysql_fetch_array($doSql3);
$parent = $row_doSql3['parent'];
$sub=0;
}

[/code]


In this line exactly:
[code=php]
$result .= categorias($row_doSql2['parent'], $sub+1, $exc);
[/code]


It is calling again the function with the category's ID as parent...

So it will do a cicle until all subcategories are logged and then it'll continue with the next category.........
Copy linkTweet thisAlerts:
@chazzyJan 05.2006 — oh

right

ha ok

first you need to have $result set so add a $result = ""; near the top

however, I don't think there's any real need to use recursion for something like this.

overall after reading it a second time, i feel that you are making the query more complex than necessary

- If a category has no parents then its parent id is 0, there is no reason to keep an array of already checked ID's, it's a waste and makes your function run slower.

- if you wanted to have a function that ran recursively, you could do something like this:
[code=php]
function categorias_s(){//takes no input
$id = 0;
return categorias_r($id);
}
function categorias_r($id){
$sql = "SELECT id,name from your_table where parent_id=".$id;
$result = mysql_query($sql);
$data = "";
if($result){
while($row = mysql_fetch_array($result,MYSQL_NUM)){//i use numbers for the array elements, seems more classical
$data= "<option value="".$row[0]."">".$row[1]."</option>n".categorias_r($row[0]);
}
}
else{
$data .= "";
}
return $data;
}
[/code]


That might work.
Copy linkTweet thisAlerts:
@delr2691authorJan 06.2006 — yep... that works... ? thanx a lot... ?

I just have to implement the sublevels part.....

tnx chazzy
×

Success!

Help @delr2691 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...