indenting output from foreach<

liunx

Guest
I have a foreach loop operating on a multi dimensional array( ie: $var[0]["key"] )...thats all well and good...the code works..but I can't figure out how to indent the subcats under their parent cat's heading(excuse the escaping..the code doesn't belong on the same page as the rest of it)


<?
$link->exec("SELECT n1.name AS pname, n2.name AS cname, n1.id AS pid, n2.id AS cid FROM news_categories n1, news_categories n2 WHERE n1.id = n2.pid ORDER BY pid");
$a = $link->get_arr_assoc(); /* returns the multi dimensional array which resembles $var[0]["pname"] $var[0]["cname"] $var[0]["pid"] $var[0]["cid"] ...etc */

?>
<?foreach($a as $v):?>
<?if(!isset($curcat))
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo '<b>'.$curcat.'</b><br>';
}
elseif(isset($curcat) && $curcat != $v["pid"])
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo '<b>'.$curcat.'</b><br>';
}
?>

<b><?=$v["pname"]?>(<?=$v["pid"]?>)</b> -> <b><?=$v["cname"]?></b> <br>
<?endforeach;?>



any ideas?well I am going to get real technical here. :P use &nbsp;

there isn't really a way to do it and if anybody else has a better idea I am all ears.

str_repeat("&nbsp;&nbsp;",'4')


<?
$link->exec("SELECT n1.name AS pname, n2.name AS cname, n1.id AS pid, n2.id AS cid FROM news_categories n1, news_categories n2 WHERE n1.id = n2.pid ORDER BY pid");
$a = $link->get_arr_assoc(); /* returns the multi dimensional array which resembles $var[0]["pname"] $var[0]["cname"] $var[0]["pid"] $var[0]["cid"] ...etc */

?>
<?foreach($a as $v):?>
<?if(!isset($curcat))
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo '<b>'.$curcat.'</b><br>';
}
elseif(isset($curcat) && $curcat != $v["pid"])
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo str_repeat("&nbsp;&nbsp;",'4')'<b>'.$curcat.'</b><br>';
}
?>

<b><?=$v["pname"]?>(<?=$v["pid"]?> )</b> -> <b><?=$v["cname"]?></b> <br>
<?endforeach;?>Originally posted by scoutt
well I am going to get real technical here. :P use &nbsp;

there isn't really a way to do it and if anybody else has a better idea I am all ears.

str_repeat("&nbsp;&nbsp;",'4')


<?
$link->exec("SELECT n1.name AS pname, n2.name AS cname, n1.id AS pid, n2.id AS cid FROM news_categories n1, news_categories n2 WHERE n1.id = n2.pid ORDER BY pid");
$a = $link->get_arr_assoc(); /* returns the multi dimensional array which resembles $var[0]["pname"] $var[0]["cname"] $var[0]["pid"] $var[0]["cid"] ...etc */

?>
<?foreach($a as $v):?>
<?if(!isset($curcat))
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo '<b>'.$curcat.'</b><br>';
}
elseif(isset($curcat) && $curcat != $v["pid"])
{
$curcat = $v["pid"];
$curcatname = $v["pname"];
echo str_repeat("&nbsp;&nbsp;",'4')'<b>'.$curcat.'</b><br>';
}
?>

<b><?=$v["pname"]?>(<?=$v["pid"]?> )</b> -> <b><?=$v["cname"]?></b> <br>
<?endforeach;?>



O_o

interesting..so there really isn't a way to properly indent the subcats hieracrh..gah....screw spelling that word :S..in a tree form? hm....thats quite a pickle.


thanks for the idea tho scoutt ^^;;hmm..just thinking just now..would it make any sense to add a 'level' column to the table and have that set how many categories are above it?that +1 would in theroy give me the right amount to str_repeat....but I'm thinking that'd be a maintainence nightmare....no maintenance I can see. using that makes it like so

main
cat
sub
sub
sub

that is what it does for me so it doesn't matter how many are under it it just keeps going.

I bet you can use blockquote but not sure if that will endent like you want.Originally posted by scoutt
no maintenance I can see. using that makes it like so

main
cat
sub
sub
sub

that is what it does for me so it doesn't matter how many are under it it just keeps going.

I bet you can use blockquote but not sure if that will endent like you want.


hmmm.....I need to do some testing.....I'll post in a few with results..


I appricate the help in brainstorming this scoutt :)actually I left some out.

I start with this one

str_repeat("&nbsp;&nbsp;",'4')

and then in the same loop I have a variable that increments on each loop.

str_repeat("&nbsp;&nbsp;",$i)

that tells it how many times to repeat the said string.Originally posted by scoutt
actually I left some out.

I start with this one

str_repeat("&nbsp;&nbsp;",'4')

and then in the same loop I have a variable that increments on each loop.

str_repeat("&nbsp;&nbsp;",$i)

that tells it how many times to repeat the said string.

now THAT makes more sense :P
altho I'm starting to get it to work...I've also realized due to the self join.....the top level categories don't get pulled from the db(ie: pid = 0 ...therefore the join condition is false on them)

gah...

[edit: bah...I've been working a lot with left joins..this is just a normal self join..it should work if I kill the WHERE]

[edit2: correction : killing the where clause also kills the functionality....]
 
Back
Top