Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: JmGarciaYT en 23 Julio 2019, 20:54 pm



Título: Crear categorias en un XML
Publicado por: JmGarciaYT en 23 Julio 2019, 20:54 pm
Buenas familia

He creado un carrito de compra, donde todo los productos estan en un XML
Código
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <listings>
  3. <listing>
  4. <title>Art. 1.1 - Uso excesivo del claxon</title>
  5. <description>Utilizar de forma repetida el claxon</description>
  6. <price>250</price>
  7. <carcel>2</carcel>
  8. <images/>
  9. <url>0</url>
  10. </listing>
  11. <listing>
  12. <title>Art. 1.2 - Giro indebido</title>
  13. <description>Hacer un cambio de sentido pisando la linea continua o en zonas no habilitadas</description>
  14. <price>250</price>
  15. <carcel>2</carcel>
  16. <images/>
  17. <url>1</url>
  18. </listing>
  19. <listing>
  20. <title>Art 1.3 - Circular en sentido contrario</title>
  21. <description>Conductor que cons u vehiculo circula o invade el sentido contrario</description>
  22. <price>500</price>
  23. <carcel>2</carcel>
  24. <images/>
  25. <url>2</url>
  26. </listing>
  27. <listing>
  28. <title>Art 1.4 - Estacionar en zonas no habilitadas y obstruir la cinculacion</title>
  29. <description>Abandonar el vehiculo en medio de la calzada o en zonas en la cueles no proceda</description>
  30. <price>250</price>
  31. <carcel>2</carcel>
  32. <images/>
  33. <url>3</url>
  34. </listing>
  35. <listing>
  36. <title>Art 1.5 - Ignorar las señales de transito</title>
  37. <description>Hcaer caso omiso de las señales verticales</description>
  38. <price>250</price>
  39. <carcel>2</carcel>
  40. <images/>
  41. <url>4</url>
  42. </listing>
  43. <listing>
  44. <title>Art 1.6 - Saltarse un semaforo</title>
  45. <description>Saltarse o no realizar los 3 segundos de parada en un semaforo</description>
  46. <price>400</price>
  47. <carcel>2</carcel>
  48. <images/>
  49. <url>5</url>
  50. </listing>
  51. </listings>

Para ver los productos en el index he creado un PHP que visualice el xml
Código
  1. <?php
  2. if(!defined('IN_SCRIPT')) die("");
  3. ?>
  4. <br/>
  5. <br/>
  6. <div id="resultswrapper">
  7. <h2 class="pull-left no-margin">
  8. <?php
  9. if(isset($_REQUEST["keyword_search"]))
  10. {
  11. echo $this->texts["search_results"];
  12. }
  13. else
  14. {
  15. echo $this->texts["our_ads"];
  16. }
  17. ?>
  18. </h2>
  19.  
  20. <div class="clearfix"></div>
  21.  
  22.  
  23. <hr class="no-margin"/>
  24. <br/>
  25. <script src="js/results.js"></script>
  26.  
  27. <div class="clearfix"></div>
  28. <div class="results-container">
  29.  
  30. <?php
  31. $PageSize = intval($this->settings["website"]["results_per_page"]);
  32.  
  33. if(!isset($_REQUEST["num"]))
  34. {
  35. $num=1;
  36. }
  37. else
  38. {
  39. $num=$_REQUEST["num"];
  40. $this->ms_i($num);
  41. }
  42.  
  43.  
  44. $listing_counter = -1;
  45.  
  46. $listings = simplexml_load_file($this->data_file);
  47.  
  48. $price_from = 0;
  49. $price_to = 0;
  50. $min_price = 0;
  51. $max_price = 0;
  52. $iTotResults = 0;
  53.  
  54.  
  55. if(isset($_REQUEST["amount"])&&trim($_REQUEST["amount"])!="")
  56. {
  57. $_REQUEST["amount"]=preg_replace("/[^\-0-9]/","",$_REQUEST["amount"]);
  58.  
  59.  
  60. $amount_items=explode("-",$_REQUEST["amount"]);
  61.  
  62. if(sizeof($amount_items)==2)
  63. {
  64. $price_from=$amount_items[0];
  65. $price_to=$amount_items[1];
  66. }
  67.  
  68. }
  69.  
  70. $script_products="";
  71. $script_products_carcel="";
  72. $script_product_prices="";
  73.  
  74. foreach ($listings->listing as $listing)
  75. {
  76. $listing_counter++;
  77.  
  78. $script_products.="products[".$listing_counter."]=\"".trim($listing->title)."\";\n";
  79. $script_products_carcel.="carcel[".$listing_counter."]=\"".trim($listing->carcel)."\";\n";
  80. $script_product_prices.="product_prices[".$listing_counter."]=\"".trim($listing->price)."\";\n";
  81.  
  82.  
  83.  
  84.  
  85. $current_price = floatval($listing->price);
  86.  
  87.  
  88. //refine search
  89. if(isset($_REQUEST["only_picture"])&&$_REQUEST["only_picture"]==1)
  90. {
  91. if(trim($listing->images)=="") continue;
  92. }
  93.  
  94. if(isset($_REQUEST["keyword_search"])&&trim($_REQUEST["keyword_search"])!="")
  95. {
  96. if
  97. (
  98. stripos($listing->title, $_REQUEST["keyword_search"])===false
  99. &&
  100. stripos($listing->description, $_REQUEST["keyword_search"])===false
  101. )
  102. {
  103. continue;
  104. }
  105. }
  106.  
  107. if($price_from!=0&&$price_to!=0)
  108. {
  109. if($current_price<$price_from) continue;
  110. if($current_price>$price_to) continue;
  111. }
  112. //end refine search
  113.  
  114.  
  115.  
  116.  
  117.  
  118. if($current_price>$max_price) $max_price=$current_price;
  119.  
  120. if($min_price==0)
  121. {
  122. $min_price=$current_price;
  123. }
  124. else
  125. if($min_price>$current_price)
  126. {
  127. $min_price=$current_price;
  128. }
  129.  
  130.  
  131. if($iTotResults>=($num-1)*$PageSize&&$iTotResults<$num*$PageSize)
  132. {
  133.  
  134. $images=explode(",",$listing->images);
  135.  
  136. if($this->settings["website"]["seo_urls"]==1)
  137. {
  138. $strLink = "product-".$this->format_str(strip_tags(stripslashes($listing->title)))."-".$listing_counter.".html";
  139. }
  140. else
  141. {
  142. $strLink = "index.php?page=details&id=".$listing_counter;
  143. }
  144. ?>
  145.  
  146. <div class="panel panel-default search-result">
  147. <div class="panel-heading">
  148. <h3 class="panel-title">
  149.  
  150. <a href="<?php echo $strLink;?>" class="search-result-title"><?php echo $listing->title;?></a>
  151.  
  152. </h3>
  153. </div>
  154. <div class="panel-body">
  155. <div class="row">
  156. <div class="col-sm-8 col-xs-12">
  157. <div class="details">
  158.  
  159. <p class="description">
  160. <?php echo $this->text_words(strip_tags($listing->description),80);?>
  161. </p>
  162.  
  163. <?php
  164. if(trim($listing->price)!="")
  165. {
  166. ?>
  167.  
  168. <span class="listing-price"><?php echo $this->texts["price"];?>: <strong><?php echo $this->settings["website"]["currency"].number_format(floatval($listing->price), 0);?></strong></span>
  169.  
  170. <?php
  171. }
  172. ?>
  173. <?php
  174. if(trim($listing->carcel)!="")
  175. {
  176. ?>
  177.  
  178. <span class="listing-price"><?php echo $this->texts["carcel"];?>: <strong><?php echo $this->settings[""]["currency"].number_format(floatval($listing->carcel), 0);?> meses</strong></span>
  179.  
  180. <?php
  181. }
  182. ?>
  183.  
  184. <span class="is_r_featured"></span>
  185. </div>
  186. </div>
  187. </div>
  188. <div class="row">
  189. <div class="col-xs-6">
  190.  
  191. </div>
  192. <div class="col-xs-6">
  193. <div class="text-right">
  194. <a class="btn btn-md btn-info" href="javascript:AddToCart(<?php echo $listing_counter;?>)"><?php echo $this->texts["add_to_cart"];?></a>
  195. </div>
  196. </div>
  197. </div>
  198. </div>
  199. </div>
  200. <?php
  201.  
  202.  
  203. }
  204.  
  205. $iTotResults++;
  206. }
  207. ?>
  208. </div>
  209. <div class="clearfix"></div>
  210. </div>
  211.  
  212. <script>
  213. var currency_symbol="<?php echo $this->settings["website"]["currency"];?>";
  214. var currency_code="<?php echo $this->settings["website"]["currency_code"];?>";
  215. var pp_address="<?php echo $this->SimpleEncrypt("123", $this->settings["website"]["admin_email"]);?>";
  216. var products=Array();
  217. <?php echo $script_products;?>
  218. var product_prices=Array();
  219. <?php echo $script_product_prices;?>
  220. </script>
  221.  
  222. <script>
  223. var min_price=<?php echo $min_price;?>;
  224. var max_price=<?php echo $max_price;?>;
  225. </script>
  226.  
  227. <?php
  228. $this->Title($this->texts["our_ads"]);
  229. $this->MetaDescription("");
  230. ?>
  231.  
  232.  
  233.  
  234. <div id="loadmoreajaxloader" style="display:none;"><center><img src="images/loading.gif" /></center></div>
  235.  
  236.  
  237. <script type="text/javascript">
  238. var page_num=2;
  239. $(window).scroll(function()
  240. {
  241.    if($(window).scrollTop() == $(document).height() - $(window).height())
  242.    {
  243.        $('div#loadmoreajaxloader').show();
  244.        $.ajax({
  245.        url: "load_products.php?num="+page_num+"&view="+current_view,
  246.        success: function(html)
  247.        {
  248.            if(html)
  249.            {
  250.                $("#resultswrapper").append(html);
  251.                $('div#loadmoreajaxloader').hide();
  252. page_num++;
  253.            }else
  254.            {
  255.                $('div#loadmoreajaxloader').html('');
  256.            }
  257.        }
  258.        });
  259.    }
  260. });
  261.  
  262. $(document).ready(function()
  263. {
  264. InitCart();
  265. });
  266. </script>

No se mucho de PHP, mi preguntas es, se puede agregar algo en el XML como <categoria>1</categoria>
<categoria>2</categoria>
 y que en el PHP poner que si la categoria es 1 que muestre ese producto y si es 2 que muestre solo los producto 2?


Título: Re: Crear categorias en un XML
Publicado por: mchojrin en 25 Julio 2019, 15:21 pm
Se puede hacer lo que buscas con la librería SimpleXML (https://academy.leewayweb.com/como-recorrer-un-archivo-xml-usando-php/), aunque honestamente, te recomiendo no usar XML como medio de almacenamiento persistente...

Más vale usa una base de datos (MySQL por ejemplo) y usa XML para comunicarte con otros sistemas.