Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored rendering of ingredients lists into Item class

  • Loading branch information...
commit 93840716042c2f4f6651655969d37b754c169f35 1 parent f6a5072
@mattrubin authored
Showing with 44 additions and 70 deletions.
  1. +34 −6 common/classes/Item.php
  2. +10 −64 item.php
View
40 common/classes/Item.php
@@ -12,7 +12,7 @@ class Item extends DataObject
private $buff_object = NULL;
- public function render(){
+ public function render_li(){
echo '<li>';
echo '<a href="item.php?id='.$this->id.'" >';
echo '<img class="item-image" src="'.$this->thumb_url.'" height="40" width="40" alt="'.$this->name.'"/>';
@@ -27,13 +27,41 @@ public static function renderList($items){
echo '<ul class="items-list">';
foreach($items as $index=>$item)
{
- $item->render();
+ $item->render_li();
}
echo "</ul>\n";
- }/* else {
- if(method_exists($items, "render"))
- $items->render();
- }*/
+ }
+ }
+
+ public function renderAsIngredient(){
+ $value = isset($this->quantity)?$this->quantity:'';
+
+ echo "<td ".((intVal($value)==0)?'style="opacity:.5;"':'')." id='ingredient-".$this->id."'>
+ <input type='text' size='2'
+ id='quantity-".$this->id."'
+ name='quantity-".$this->id."'
+ value='".$value."' onkeyup='checkVal(this.value, \"ingredient-".$this->id."\")'/>
+ <img src='".$this->thumb_url."' height='30' width='30'/>";
+ echo " ".$this->name."</td>";
+ }
+
+ public static function renderIngredientsList($items){
+ if(is_array($items)){
+ echo '<table style="font-size:75%;">';
+
+ $i=0;
+ $step = 5;
+ foreach($items as $index=>$item)
+ {
+ if($i%$step==0) echo "<tr>";
+
+ $item->renderAsIngredient();
+
+ if($i%$step==$step-1)echo "</tr>\n";
+ $i++;
+ }
+ echo "</table>\n";
+ }
}
public function render_info(){
View
74 item.php
@@ -167,70 +167,16 @@ function render_cost($mood, $energy, $xp, $currants=0){
<?php
- if(!$recipe['harvest']) {
-
-
- $query = sprintf("SELECT * FROM `ingredients` LEFT JOIN `items` ON ingredients.item_id=items.id WHERE recipe_id=%d", $recipe['id']);
- $result = execute($query);
-
- $in_use = array();
-
- if(mysql_num_rows($result)>0){
- echo '<table style="font-size:75%;">';
-
-
- $i=0;
- $step = 5;
- while ($row = mysql_fetch_array($result))
- {
- array_push($in_use, $row['item_id']);
- if($i%$step==0) echo "<tr>";
-
- echo "<td id='ingredient-".$row['item_id']."'>
- <input type='text' size='2'
- id='quantity-".$row['item_id']."'
- name='quantity-".$row['item_id']."'
- value='".$row['quantity']."' onkeyup='checkVal(this.value, \"ingredient-".$row['item_id']."\")'/>
- <img src='".$row['thumb_url']."' height='30' width='30'/>";
- echo " ".$row['name']."</td>";
-
-
- if($i%$step==$step-1)echo "</tr>\n";
- $i++;
-
- }
-
- echo '</table><hr />';
- }
-
- $query = "SELECT * FROM items";
- $result = execute($query);
-
- echo '<table style="font-size:75%;">';
- $i=0;
- $step = 5;
- while ($row = mysql_fetch_array($result))
- {
- if(in_array($row['id'], $in_use)) continue;
- if($i%$step==0) echo "<tr>";
-
- echo "<td style='opacity:.5' id='ingredient-".$row['id']."'>
- <input type='text' size='2'
- id='quantity-".$row['id']."'
- name='quantity-".$row['id']."'
- onkeyup='checkVal(this.value, \"ingredient-".$row['id']."\")'/>
- <img src='".$row['thumb_url']."' height='30' width='30'/>";
- echo " ".$row['name']."</td>";
-
-
- if($i%$step==$step-1)echo "</tr>\n";
- $i++;
-
- }
- echo '</table>';
- }
-
- ?>
+ if(!$recipe['harvest']) {
+ $ingredients_in_use = Item::all(sprintf("RIGHT JOIN `ingredients` ON ingredients.item_id=items.id WHERE recipe_id=%d", $recipe['id']));
+ Item::renderIngredientsList($ingredients_in_use);
+
+ echo '</table><hr />';
+
+ $ingredients_in_use = Item::all(sprintf("WHERE id NOT IN (SELECT item_id FROM `ingredients` WHERE recipe_id=%d)", $recipe['id']));
+ Item::renderIngredientsList($ingredients_in_use);
+ }
+ ?>
</form>
</div>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.