Jump to content

PHP Explode


cargautech

Recommended Posts

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 :)

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.