Note that there are some explanatory texts on larger screens.

plurals
  1. POPagination Filtered XML file
    primarykey
    data
    text
    <p>I want to paginate the following filtered results from xml file:</p> <pre><code>&lt;?php //load up the XML file as the variable $xml (which is now an array) $xml = simplexml_load_file('inventory.xml'); //create the function xmlfilter, and tell it which arguments it will be handling function xmlfilter ($xml, $color, $weight, $maxprice) { $res = array(); foreach ($xml-&gt;widget as $w) { //initially keep all elements in the array by setting keep to 1 $keep = 1; //now start checking to see if these variables have been set if ($color!='') { //if color has been set, and the element's color does not match, don't keep this element if ((string)$w-&gt;color != $color) $keep = 0; } //if the max weight has been set, ensure the elements weight is less, or don't keep this element if ($weight) { if ((int)$w-&gt;weight &gt; $weight) $keep = 0; } //same goes for max price if ($maxprice) { if ((int)$w-&gt;price &gt; $maxprice) $keep = 0; } if ($keep) $res[] = $w; } return $res; } //check to see if the form was submitted (the url will have '?sub=Submit' at the end) if (isset($_GET['sub'])) { //$color will equal whatever value was chosen in the form (url will show '?color=Blue') $color = isset($_GET['color'])? $_GET['color'] : ''; //same goes for these fellas $weight = $_GET['weight']; $price = $_GET['price']; //now pass all the variables through the filter and create a new array called $filtered $filtered = xmlfilter($xml ,$color, $weight, $price); //finally, echo out each element from $filtered, along with its properties in neat little spans foreach ($filtered as $widget) { echo "&lt;div class='widget'&gt;"; echo "&lt;span class='name'&gt;" . $widget-&gt;name . "&lt;/span&gt;"; echo "&lt;span class='color'&gt;" . $widget-&gt;color . "&lt;/span&gt;"; echo "&lt;span class='weight'&gt;" . $widget-&gt;weight . "&lt;/span&gt;"; echo "&lt;span class='price'&gt;" . $widget-&gt;price . "&lt;/span&gt;"; echo "&lt;/div&gt;"; } } </code></pre> <p>Where <code>$xml-&gt;widget</code> represents the following xml:</p> <pre><code>&lt;hotels xmlns=""&gt; &lt;hotels&gt; &lt;hotel&gt; &lt;noofrooms&gt;10&lt;/noofrooms&gt; &lt;website&gt;&lt;/website&gt; &lt;imageref&gt;oias-sunset-2.jpg|villas-agios-nikolaos-1.jpg|villas-agios-nikolaos-24​.jpg|villas-agios-nikolaos-41.jpg&lt;/imageref&gt; &lt;descr&gt;blah blah blah&lt;/descr&gt; &lt;hotelid&gt;119&lt;/hotelid&gt; &lt;/hotel&gt; &lt;/hotels&gt; &lt;/hotels&gt; </code></pre> <p>Any good ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload