How can guys insert pictures into this script? I have 6 parent categories and each parent must have its own picture. This is my entire category code, of course without styles.

<?php $result = mysql_query ("SELECT `id`, `parent_id`, `name` FROM `category`"); $cats = array(); while($cat = mysql_fetch_assoc($result)) $cats[$cat['parent_id']][] = $cat; function create_tree ($cats,$parent_id){ if(is_array($cats) and isset($cats[$parent_id])){ $tree = !$parent_id?'':'<ul>'; foreach($cats[$parent_id] as $cat){ $tree .= "<li>"; $tree .= !isset($cats[$cat['id']])?"<a href='view_cat.php?cat=".$cat['id']."'>".$cat['name']."</a>":"<span>".$cat['name']."</span>"; $tree .= create_tree($cats,$cat['id']); $tree .= '</li>'; } $tree .= !$parent_id?'':'</ul>'; }else return null; return !$parent_id?'<ul class="sublist">'.$tree.'</ul>':$tree; } echo create_tree($cats, 0); 
Here is an example of how

categories

  • Tried to add img tag before a and span ? - Arnial
  • @Arnial I have 6 parent categories, for each category has its own picture. And this code prints my entire table. What you suggested to me as I understood, then 1 and the same picture is shown - Eugene
  • Sorry wrong, img needed before ul . In order for them to be different, substitute different src into pictures. - Arnial

1 answer 1

 <?php $result = mysql_query ("SELECT `id`, `parent_id`, `name` FROM `category`"); $cats = array(); while($cat = mysql_fetch_assoc($result)) $cats[$cat['parent_id']][] = $cat; function create_tree ($cats,$parent_id){ if(is_array($cats) and isset($cats[$parent_id])){ $tree = !$parent_id?'':'<ul>'; foreach($cats[$parent_id] as $cat){ $tree .= "<li>"; $img = !$parent_id?'<img src="images/'.$cat['name'].'.png">':''; $tree .= !isset($cats[$cat['id']])?"<a href='view_cat.php?cat=".$cat['id']."'>".$img." ".$cat['name']."</a>":"<span>".$img." ".$cat['name']."</span>"; $tree .= create_tree($cats,$cat['id']); $tree .= '</li>'; } $tree .= !$parent_id?'':'</ul>'; }else return null; return !$parent_id?'<ul class="sublist">'.$tree.'</ul>':$tree; } echo create_tree($cats, 0); ?> 
  • You are helping me out in full thanks to you) but could you tell me how to connect them? I need to add pictures to the database or how? Before that, I did this <img src = "images / things.png" id = "things-images"> Things </a> and applied them to other lists as well, but I don’t know here: ) - Eugene
  • one
    updated code. should work - webDev_
  • one
    thank you)))) + for your work :) - Eugene
  • you could not look at it) ru.stackoverflow.com/questions/570829/… - Eugene