html - Trying to Categorize XML data in a PHP Shopping Cart -


ok, i'm attempting create php based shopping cart reading xml file catalogue. problem when print out information onto website, prints out in xml file. need put them categories (i.e. shoes, apparel, etc.) , print out called category.

the xml file structured such (extra spaces added organizational purposes):

<items>     <product>         <id>           tshirt01                        </id>         <title>        red t-shirt                     </title>         <category>     apparel                         </category>         <description>  t-shirt designed sassafrass  </description>         <img>          ../images/apparel1.jpg          </img>         <price>        5.99                            </price>     </product> </items> 

i print out information onto website using following code:

<?php echo render_products_from_xml(); ?> 

and here's function php command sets structure being output onto website itself:

function render_products_from_xml(){ $counter=0; $output = '<table class="products"> <tr>'; foreach(get_xml_catalog() $product) {     $counter++;     $output .='                  <td>                     <div class="title">                     <h2> '.$product->title.' </h2>                     </div>                     <div class="cells">                         <img src="'.$product->img.'" height="220" width="170" />                     </div>                     <div class="description">                     <span>                         '.$product->description.'                     </span>                     </div>                     <div class="price">                         $'.$product->price.'                     </div>                     <div class="addtocart">                         <a href="addtocart.php?id='.$product->id.'">add cart</a>                     </div>                 </td>';     if($counter%4 == 0)     {         $output .='<tr>';     } } $output .='</tr></table>'; return $output;} 

i hoping have php function end looking (changes made in caps):

<?php echo render_products_from_xml($category=='apparel'); ?> 

or along these lines:

<?php echo render_apparel_products_from_xml(); ?> 

just need tips on how can add functions categorize information read xml file. also, don't want create new xml files each category because i'll need duplicate codes pull information separate xml files , consolidate products 1 shopping cart. i'm looking lot easier manage.

and on final note, have lot of background functions working in grabbing information , setting actual shopping cart itself, if feel need me give more code, ask! also, if vague anything, don't hesitate telling me can (hopefully) correct problem or answer question.

thanks in advance can give! appreciate it. :)

your code doesn't show function get_xml_catalog(), apparently fetching xml.

so code you've given, make relatively small changes function render_products_from_xml():

function render_products_from_xml($category) {     $counter=0;     $output = '<table class="products"> <tr>';      foreach (get_xml_catalog() $product) {          if ((string)$product->category == $category || $category == '') {              $counter++;             $output .= 'all stuff';               if ($counter % 4 == 0) $output .= '<tr>';         } // if     } // foreach      $output .='</tr></table>';     return $output; } 

comments:

(1) call function passing parameter $category:

echo render_products_from_xml('apparel'); 

(2) within foreach loop, <product> category == $category added $output.

(3) if $category empty string, every <product> added $output.

alternative:

change function get_xml_catalog($category) selection @ place. done best xpath.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -