cargautech Posted November 14, 2011 Share Posted November 14, 2011 Hi, I have an array for prices : forfait="158|Group Session,95|Group half session,45|Private Course,90|zumba course" which is used in a dropdown list where it shows as Group Session (158$) Group half session (95$) etc... The prices need to be used as an 'amount' field on the gift certificate as the descriptions must show as a 'label' on the gift certificate. So, I need to ' explode ' both the prices and the descriptions separately. I can 'explode' de prices using $tmp_forfait=explode("|", $value) and I get 158 95 45 and 90 But, how do I get the descriptions separately, without the prices meaning I need to 'explode' the digits and '|' ... Anyone can help me with that cope please? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/ Share on other sites More sharing options...
Psycho Posted November 14, 2011 Share Posted November 14, 2011 I can 'explode' de prices using $tmp_forfait=explode("|", $value) and I get 158 95 45 and 90 Really? Using THAT explode should give you: 158 Group Session,95 Group half session,45 Private Course,90 zumba course This will format the data as you request and you can reformat the output to be a select list option, but I don't know what you would make the value foreach(explode(',', $forfait) as $item) { list($price, $label) = explode('|', $item); echo "{$label} ({$price}$)<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1288158 Share on other sites More sharing options...
cargautech Posted November 15, 2011 Author Share Posted November 15, 2011 Thank you for your quick reply. Much appreciated! And...sry. Since the part I was looking for wasn't to get the price & description, I didn't put the hole code: Here is the code for the dropdown list showing : Group Session (158$) Group half session (95$) and so on. <tr> <td align="right" class="se_label"> <?php echo JText::_('AMOUNT');?> </td> <td><select name="se_amount" id="se_amount"> <?php foreach ($this->card_amount as $value) { $checked = ""; if($value > 0) { $value = trim($value); if ($this->se_amount == $value) { $checked = "selected"; } $tmp_value = explode("|", $value); if(isset($tmp_value[1])) { $clean = $tmp_value[1] ." ( " . sprintf($this->currency, $tmp_value[0]) . " )"; } else { $clean = sprintf($this->currency, $tmp_value[0]); } ?> <option value="<?php echo($value) ?>" <?php echo($checked) ?>><?php echo($clean) ?></option> <?php } } ?> </select></td> </tr> Now, I need to insert on that form another field (label and input field - not drowdown) under that one, that will only show the Description without the price for the item selected above. I would name that field se_forfait That is where I don't know how to code so I extract only the description. Somewhere else in the coding they managed to pull out only the prices so it shows on the Gift Certificate as 158$ but they omitted to have the description of the item bought on the Gift Certificate. Here is the code for the price shown on the Gift Certificate: /// PRICE $titleSize = imagettfbbox($params['price_value']['size'], 0, $rootPath.$params['price_value']['font'], $amount); $rgb = $this->hex2rgb($params['price_value']['color']); $colorLabel = imagecolorallocate($container, $rgb[0], $rgb[1], $rgb[2]); imagefttext($container, $params['price_value']['size'], 0, $params['price_value']['left'] - $titleSize[2], $params['price_value']['top'], $colorLabel, $rootPath.$params['price_value']['font'], $amount); So, I will also need to be able to use that new se_forfait field to have it shown on the Gift Certificate somehow. Thank again! Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1288176 Share on other sites More sharing options...
Psycho Posted November 15, 2011 Share Posted November 15, 2011 So, use the code I gave you but only use the $label or only use the $price or use both. Personally, I dump the hole thing into an array using the name as the key and the price as the value. Then you can loop over that array as needed $temp_array = array(); foreach(explode(',', $forfait) as $item) { list($price, $label) = explode('|', $item); $temp_array[$label] = $price; } //Now use a foreach() loop on $temp_array as needed Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1288184 Share on other sites More sharing options...
cargautech Posted November 15, 2011 Author Share Posted November 15, 2011 Thank you again mjdamato. I will try this tomorrow and will let you know how I managed! Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1288187 Share on other sites More sharing options...
cargautech Posted November 16, 2011 Author Share Posted November 16, 2011 I'm quite new at php. So far, from the code of the 1st field which is the drowdown list showing: Group session (158$) Group half session (95$) <tr> <td align="center"> <select name="se_amount" id="se_amount" onChange="forfait_copy()"> <?php foreach ($this->card_amount as $value) { $checked = ""; if($value > 0) { $value = trim($value); if ($this->se_amount == $value) { $checked = "selected"; } $tmp_value = explode("|", $value); if(isset($tmp_value[1])) { $clean = $tmp_value[1] ." ( " . sprintf($this->currency, $tmp_value[0]) . " )"; } else { $clean = sprintf($this->currency, $tmp_value[0]); } ?> <option value="<?php echo($value) ?>" <?php echo($checked) ?>><?php echo($clean) ?></option> <?php } } ?> </select> </td> </tr> All I managed to do for the 2nd field which must be an input field that takes the value selected in the dropdown and inputs only the description part (not the price) is to created another dropdown field same as 1st one... <tr> <td align="right" class="se_label"> <?php echo JText::_('FORFAIT');?> </td> <td><select name="se_forfait" id="se_forfait" onChange="amount_copy()"> <?php foreach ($this->card_amount as $value) { $checked = ""; if($value > 0) { $value = trim($value); if ($this->se_forfait == $value) { $checked = "selected"; } $tmp_value = explode("|", $value); if(isset($tmp_value[1])) { $clean = $tmp_value[1]; } else { $clean = sprintf($this->currency, $tmp_value[0]); } ?> <option value="<?php echo($value) ?>" <?php echo($checked) ?>><?php echo($clean) ?></option> <?php } } ?> </select></td> </tr> But where I managed to get only the description and no price. To get the selected option copied into the input field, I used functions forfait_copy() from the se_amount to se_forfait field and amount_copy() from se_forfait to se_amount field. Functions: for(i=document.se_cgc.se_amount.options.length-1;i>=0;i--) { if(document.se_cgc.se_amount.options[i].selected) document.se_cgc.se_forfait.options[i].selected=true; } and reverse for the 2nd function. But I still end up with a 'select' field instead of an 'input' field which is why I have to use functions to ensure that same items is in both fields. I tried your codes mjdamato, but I'm stuck as how to implement the php in between <td> and </td> to get the input field with copied value of the selected option of dropdown list. I'm pretty sure there is a nicest and easiest way to code this but I can't find the answer. The best would be the 2nd field as INPUT with copied description of selected item from dropdown list of 1st field. Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1288817 Share on other sites More sharing options...
cargautech Posted November 19, 2011 Author Share Posted November 19, 2011 I found the answers on how to manage the php code of the exploded description and the <td></td> tags. Also figured out how to have a selected option from one drop down field copied to an input text field. Thank you for all the help on the explode part Quote Link to comment https://forums.phpfreaks.com/topic/251147-php-explode/#findComment-1289517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.