php - Craft calculator loop -
i'm trying craft calculator game.
i have database looks :
crafts :
id item_id item_name amount 3 1895 5 8 2486 c 1
craft_materials :
id craft_id item_id item_name amount 1 3 2486 c 15 2 3 5302 d 23 3 3 5698 e 2 4 8 2014 f 3
and here query retrieve data :
$craftproduct = $db->query("select * crafts item_id=$item"); $craftproduct->setfetchmode(pdo::fetch_obj); $product = $craftproduct->fetch(); $craftid = $product->id; $craftmaterial = $db->query("select * craft_materials craft_id=$craftid"); $craftmaterial->setfetchmode(pdo::fetch_obj); echo '<ul>'; echo '<li>'.$product->item_name.' ('.$product->amount.')</li>'; echo '<ul>'; while($material = $craftmaterial->fetch()){ echo '<li>'.$material->item_name.' ('.$material->amount.')</li>'; } echo '</ul>'; echo '</ul>';
what want take material id , check if corresponds craft. if does, want show material needed craft it.
i want looks :
- (5) - c (15) - f (3) - d (23) - e (2)
however, don't know how loop. can me?
here basic recursion setup:
function render_item($item_id) { $product = fetch_item($item_id); // select crafts item_id = $item_id $children = ""; foreach (fetch_children($product->id) $child_id) { // select craft_mats craft_id = $product->id $children .= render_item($child_id); } echo "<li>" . $product->item_name . "<ul>" . $children . "</ul></li>"; } echo "<ul>" . render_item(1895) . "</ul>"
Comments
Post a Comment