Jump to content
php.lv forumi

treeview


Klez

Recommended Posts

ir tabula: ID | parent | name

function treeview($pos = false,$atdala = '')
{
if (!$pos) $pos = 0;
$all = db_query("SELECT id,name FROM category WHERE parent=$pos ORDER BY sort ASC");
$count = 0;
while ($one = mysql_fetch_row($all))
{
	echo $atdala.$one[1] . '<br>';
	$count++;
	$has_kids = mysql_fetch_row(db_query("SELECT id,name FROM category WHERE parent=$one[0] ORDER BY sort ASC"));
	if ($has_kids) treeview($one[0],str_repeat('--',$count));
}
}

 

lieta taada ka vinsh laagaa nezimee to koku. divos liimenjos ir ok, bet tālāk nezīmē.

kļūda gan jau ka maziņa, bet nevaru atrast ...

Edited by Klez
Link to comment
Share on other sites

nu tur bija domaats priekshaa atdaliitaajs, lai redz kaa pa liimenjiem kustas. ir izlabots

 

tur izvadaas shitaa:

Mēbeles

--vel kresls

--kresls

----kresla kaja

--krēsla kājas puļķis

--zzzzzz

--sssss

--krēsla atzveltne

Santehnika

Sīkumi

 

db ir:

id 	parent 	name 	sort
1 	0 	Mēbeles 	1 	
2 	1 	kresls 	2 	
3 	2 	kresla kaja 	1 	
4 	1 	vel kresls 	1 	
5 	0 	Santehnika 	2 	
6 	0 	Sīkumi 	3 	
7 	3 	krēsla kājas puļķis 	1 	
8 	3 	krēsla atzveltne 	2 	
9 	7 	zzzzzz 	1 	
10 	7 	sssss 	1

Edited by Klez
Link to comment
Share on other sites

Manuprāt, Tev tas $count arī ir jāpadod. Tobiš pirmo reizi jau izsauc ar count parametru 0.

function treeview($pos = false,$count)
{
if (!$pos) $pos = 0;
$all = db_query("SELECT id,name FROM category WHERE parent=$pos ORDER BY sort ASC");
$atdala = str_repeat('--',$count);
while ($one = mysql_fetch_row($all))
{
	echo $atdala.$one[1] . '<br>';
	$has_kids = mysql_fetch_row(db_query("SELECT id,name FROM category WHERE parent=$one[0] ORDER BY sort ASC"));
	if ($has_kids) treeview($one[0],$count+1);
}
}

Edited by Aleksejs
Link to comment
Share on other sites

Varbūt tad parādi izlaboto, jo meibī vaina ir tieši tur.

 

Moka šitā labāk:

 

<?php
function treeview($pos = false, $level = 0)
{
if (!$pos) $pos = 0;
$all = db_query("SELECT id,name FROM category WHERE parent=$pos ORDER BY sort ASC");
$level ++;
while ($one = mysql_fetch_row($all))
{
	echo str_repeat('--', $level).$one[1] . '<br>';
	$has_kids = mysql_fetch_row(db_query("SELECT id,name FROM category WHERE parent=$one[0] ORDER BY sort ASC"));
	if ($has_kids) treeview($one[0], $level);
}
};
?>

Edited by andrisp
Link to comment
Share on other sites

andrisp tavs variants straadaa:

--Mēbeles

----vel kresls

----kresls

------kresla kaja

--------krēsla kājas puļķis

----------zzzzzz

----------sssss

--------krēsla atzveltne

--Santehnika

--Sīkumi

 

vieniigi palaboju sho rindinju: echo str_repeat('--', $level-1).$one[1] . '<br>';

un sanaak:

Mēbeles

--vel kresls

--kresls

----kresla kaja

------krēsla kājas puļķis

--------zzzzzz

--------sssss

------krēsla atzveltne

Santehnika

Sīkumi

Edited by Klez
Link to comment
Share on other sites

×
×
  • Create New...