Jump to content

PHP trying to retieve a tricky value using XQuery with wildcard


max_maggot
Go to solution Solved by Ch0cu3r,

Recommended Posts

I am trying to retrieve a really tricky array of values. The value I am trying to retrieve are the

-price values and the text within the price tag

-color values which can be more than one option (black and white in this case)

<dd>
<div class="input-box">
  <select name="options[1216]" id="select_1216" 
   class="product-custom-option" title=""  onchange="opConfig.reloadPrice()">
   <option value="" >-- Please Select --</option>
   <option value="4185"  price="4.99" >4GB +$4.99</option>
   <option value="4186"  price="5.99" >8GB +$5.99</option>
   <option value="4187"  price="9.99" >16GB +$9.99</option>
   <option value="4188"  price="16.99" >32GB +$16.99</option>
   <option value="4189"  price="29.99" >64GB +$29.99</option>
  </select>            
</div>
</dd>
            
<dt>
<label class="required"><em>*</em>Color</label></dt>
<dd class="last">
    <div class="input-box">
        <ul id="options-1215-list" class="options-list">
          <li><input type="radio" class="radio  
               validate-one-required-by-name product-custom-option" 
               onclick="opConfig.reloadPrice()" name="options[1215]" 
               id="options_1215_2" value="4183"  price="0" />

               <span class="label"><label for="options_1215_2">Black </label></span>
               <script type="text/javascript">
          </li>
          <li>
              <input type="radio" class="radio  
               validate-one-required-by-name product-custom-option" 
               onclick="opConfig.reloadPrice()" name="options[1215]" 
               id="options_1215_3" value="4184"  price="0" />
              <span class="label"><label for="options_1215_3">White </label></span>              
          </li>
        </ul>                                    
</div>

I have wrtten the following code to try and retrieve the values of the price but to no avail. I'm trying to use a wildcard character in the xquery to try because other pages will have different option names. Here is what I came up with so far. There won't be more than 20 options on a page. Any help would be much appreciated. Thanks in advance.

$option_array = array();
      for($k = 1; $k <= 20; $k++) {
           $option_array[$k] = $xpath->query("//div[@class='input-box']//select/*/option[$k]/@price")->item(0)->textContent;
           //If we have captured all the images then we need to exit the for loop
           if($option_array[$k] == null){
                 break;
           }
      }
Link to comment
Share on other sites

  • Solution

I wouldn't use a for loop. Better to foreach loop over the elements returned from the xpath query, example

$option_array = array();
// finds all <option price="xxx">zzz</option> tags in <div class="input-box">
foreach($xpath->query("//div[@class='input-box']//option[@price]") as $option)
{
    // add price attribute value and and option tag text items to array
    $option_array[] = array($option->getAttribute('price'), $option->nodeValue);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.