Page 1 of 1

LOOP PHP

Posted: Wed Feb 11, 2004 5:15 am
by babaloui
Hi all,
How can I write PHP code that shows (MultiLevel) result from MySQL,
I have this PRODUCTS table (MySQL):

categorey ................sub-categorey
=======================
Shampoo.................PertPlus
Shampoo.................SunSilk
Soup........................Lux
ToothPaste..............Creast
ToothPaste..............Signal2
ToothPaste..............Collgate

and I want to display ALL records * like this:

Shampoo
-----------PertPlus
-----------SunSlik
Soup
-----------Lux
ToothPaste
-----------Creast
-----------Signal2
-----------Collgate

Thank you!

Posted: Wed Feb 11, 2004 5:16 am
by malcolmboston
look here

Posted: Wed Feb 11, 2004 6:07 am
by valaridz
Here 2 real functions from my code:)


1.

function _buildTree($parent_id = 0, $level = -1, $tree_str = '') {
global $DB;
if ($level == 0 && $parent_id == 0) return;
$query_str = 'SELECT id, name FROM '.TBL_CATEGORIES.' WHERE parent_id = '.$parent_id.' ORDER BY status DESC';
$res = & $DB->query($query_str);
$nrows = $res->numRows();
if ($nrows > 0) {
$level++;
while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
$tree_str .= '<tr><td>';
for ($n = 0; $n < $level; $n++) $tree_str .= '..';
$tree_str .= '<a href="../product/?a=list&i='.$row['id'].'">'.$row['name'].'</a></td>';
$tree_str .= '<td>';
$tree_str .= '<a href=?a=form&p=add&i='.$row['id'].'>add</a>|';
$tree_str .= '<a href=?a=info&i='.$row['id'].'>info</a>|';
$tree_str .= '<a href=?a=form&p=edit&i='.$row['id'].'>edit</a>|';
$tree_str .= '<a href=?a=del&i='.$row['id'].' onClick="return confirm(''Are you sure?'');">delete</a>';
$tree_str .= '</td>';
$tree_str .= '</tr>';
$this->_buildTree($row['id'], $level, &$tree_str);
}
}
else {
if ($level > 0) $level--;
}
}

2.

function showTree() {
global $DB, $SMARTY;
$tree_str = '';
$tree_str .= '<tr>';
$tree_str .= '<td>Name</td>';
$tree_str .= '<td>Actions</td>';
$tree_str .= '</tr>';
$this->_buildTree(0, -1, &$tree_str);
$SMARTY->assign('category_tree', $tree_str);
return $SMARTY->fetch('objects/category/category_tree.tpl');
}

Posted: Wed Feb 11, 2004 6:27 am
by twigletmac
valaridz - it is much easier for people to read your code if you use tags around it.

Mac

Posted: Wed Feb 11, 2004 6:29 am
by malcolmboston

Code: Select all

<?php
echo "$twigletmac"
?>