1


I have a value from mysql which I want to split into multiple input fields in a form.

// This is the value from mysql...
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

// This is how I want to split it....
$amt_1 = "10";
$desc_1 = "Coles";
$amt_2 = "15";
$desc_2 = "Aldi";
$amt_3 = "5";
$desc_3 = "Target";
$amt_4 = "7.77";
$desc_4 = "Kmart";


//So that it can be displayed in input boxes...
<input type="text" value="<?php echo $amt_1; ?>" />
<input type="text" value="<?php echo $desc_1; ?>" />

<input type="text" value="<?php echo $amt_2; ?>" />
<input type="text" value="<?php echo $desc_2; ?>" />

<input type="text" value="<?php echo $amt_3; ?>" />
<input type="text" value="<?php echo $desc_3; ?>" />

<input type="text" value="<?php echo $amt_4; ?>" />
<input type="text" value="<?php echo $desc_4; ?>" />

Now, there is a condition.

The $value can be empty or it may not enough to be able to split into 4 parts, means it can be $value = "10 - Coles, 15 - Aldi"; to be able to fit into $amt_1, $desc_1 and $amt_2, $desc_2. or any set of 1, 2, 3 or 4 splits.

1
  • Let me know if my answer worked for you. It's pretty clean and also takes care of the situation if $value is empty Commented Jun 1, 2016 at 4:19

3 Answers 3

1

Here is my approach if you want exactly 4 pairs:

$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$valArr = explode(', ', $value);

for ($i=0; $i<4; $i++) {
  if(isset($valArr[$i])) {
     $currentVal = explode(' - ',$valArr[$i]);
     echo '<input type="text" name="amt_'.($i+1).'" value="'.$currentVal[0].'" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="'.$currentVal[1].'" />';
  } else {
     echo '<input type="text" name="amt_'.($i+1).'" value="NO VALUE" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="NO VALUE" />';
  }
}

But to me more proper approach is:

$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$valArr = explode(', ', $value);

foreach ($valArr as $pair) {
     $currentVal = explode(' - ',$pair);
     echo '<input type="text" name="amt_'.($i+1).'" value="'.$currentVal[0].'" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="'.$currentVal[1].'" />';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Using explode in an efficient matter inside a foreach loop:

// This is the value from mysql...
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$myArray = explode(",", $value);//separating the parts

foreach ($myArray as $data)
{
  $buffer = explode("-", $data);
  $first = trim($buffer[0]);
  $second = trim($buffer[1]);
  if ($first != "")
  {
    echo "<input type='text' value='{$first}' />";
    echo "<input type='text' value='{$second}' />";
  }
}

Comments

0

Use following code

<?php
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$arrayValue = explode(', ', $value);
$i=0;
foreach ($arrayValue as $new) {
    $i++;
     $exactValue = explode(' - ',$new);
     echo '<input type="text" name="amt_'.($i).'" value="'.$exactValue[0].'" />';
     echo '<input type="text" name="desc_'.($i).'" value="'.$exactValue[1].'" /><br>';
}
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.