0

So I have a mysql table for the charges of a hospital. My program currently only gets the price of the checked procedure. But now, I also want to get the procedure name when it is checked.

transaction.php

while($row = mysql_fetch_array($result))
{
     echo ' <tr> <td>'.$row[0].'</td> <td>'.$row[1].'</td><td>'.$row[2].'</td>';
     $procedure=$row['procedure'];
     echo '<td><input type="checkbox" name="er[]" value="$price."|".$procedure"></td>';
     echo "</tr>";
}
echo '</table>';

computation.php

  <?php
      if(isset($_POST['er']))
      {
        $ercharge=$_POST['er'];
        $totalofer = array_sum($ercharge);  
      }
      if(isset($_POST['ultrasound']))
      {
        $x=$_POST['ultrasound'];
        $totalofultrasound =  array_sum($x);
      }
      if(isset($_POST['confinement']))
      {
        $y=$_POST['confinement'];
        $totalofconfinement =  array_sum($y);
      }
      $total = $totalofer + $totalofultrasound + $totalofconfinement;
      $p = explode("|", $ercharge);
      echo $p;
      echo  $total;
?>

It only gets the row for price. Can the value attribute have two values? I can't just make another checkbox cause that would be inappropriate.

edit: the explode function doesnt work. It says: Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\computation.php on line 18

8
  • 1
    It's an array because of how it is being submitted: <input type="checkbox" name="er[]" value="$price."|".$procedure"> - Are there multiple checkboxes named "er[]"? If there is only one, change it to just "er", otherwise you will need to step through the array and process each individually. Commented Dec 15, 2012 at 7:06
  • Passing two values under the same parameter is a bad practice, if you want to do something like that your design is probably not good and most chances are that it will cause maintenance issues in the future. Commented Dec 15, 2012 at 7:07
  • Does the price vary? If not, it would be simpler to just pass in the procedure's ID, and then retrieve the procedure and price info on the computation.php page. Also, explode returns an array. Commented Dec 15, 2012 at 7:11
  • @EricBrandel Nope. Just that one since its on a loop. If I change it to just "er", you can only just check one procedure. It wouldnt compute multiple checks. Commented Dec 15, 2012 at 7:13
  • @EricBrandel Yes. The prices vary. Commented Dec 15, 2012 at 7:14

2 Answers 2

2

You should split your parameters in the HTML:

    echo '<td><input type="checkbox" name="checked[$row_index][]" value="1">';
    echo '<input type="hidden" name="prices[$row_index][]" value="$price">';
    echo '<input type="hidden" name="procedures[$row_index][]" value="$procedure"></td>';

where $row_index is incremented on each row (tr tag)

By the way, explode will work on the items of the er array, not on the array itself. Try:

foreach ($er as $item) {
  var_dump( explode( "|", $item ) );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok i changed it to: echo '<td><input type="checkbox" name="er[]" value="'.$price.'">'; echo '<input type="hidden" name="procedures[]" value="'.$procedure.'"></td>'; echo "</tr>"; the total is computed now. So the price works fine. The problem is the procedure name. It only shows: "Array".
1

I'm not sure I understand your question but couldn't you set the name attrtibute for your checkbox to the name of the procedure? It looks like you are setting the name to the er[] array but you never reference that later.

1 Comment

I can't do that since its on a loop and is retrieved from a mysql table. I referenced er[] to the computation.php so it could output multiple values if there are many checks in the form.

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.