-2

How to get each value unique id from array implode when passing value through multiple selection php? When I pass single data, I can get the id; while when I pass multiple data it won't shown any id.

I have some ideas, is it the problem when select more that 2 values while post to next page; it will combine the data "ROBERT ALVIN" so the database could not catch the data; How do I split the data to "ROBERT" "ALVIN", so I would able to get the id "1" "2".

when select single data!

http://b62i.imgup.net/33ba8c.png

when select two or more data!

http://v58i.imgup.net/1232cde.png

Sample coding.

$myselected     =   $_POST["auditor"];
$auditor = implode (" ",$myselected);

$query10 = "SELECT * FROM auditor WHERE auditor_name = '$auditor' ";
$result10 = $db->query($query10);
$row10 = $result10->fetch_array();

<tr>
<td><b>Auditor:</b></td>

<td colspan='5'>
<?php 
echo'<input type="text" name="form_details_id" value="'.$row10["id"].'">';

    foreach ($myselected as $auditor){ 
    echo $auditor."<br>\n"; 
    }
?>
</td>
  • not quite getting it `var_dump($_POST["auditor"])` for us –  Sep 29 '15 at 00:48
  • It's not clear what you want to do. Please edit your question explaining better what you're trying to achieve. – Gustavo Straube Sep 29 '15 at 01:00
  • @GustavoStraube actually it is a very simple question. I cannot get id when post multiple selection value, while if I select single data from the selection box, everything works fine. – user3551936 Sep 29 '15 at 01:05
  • 1
    well if it's so simple you don't need our help :p –  Sep 29 '15 at 01:12
  • How does your form looks like? That with the `auditor` input. Also, as @Dagon asked, please show the contents from your `$_POST` array using `var_dump`. – Gustavo Straube Sep 29 '15 at 01:13
  • @GustavoStraube I just edit the post and provide the output; and I tried var_dump. Output is like: array(4) { [0]=> string(11) "Robert (RO)" [1]=> string(12) "Michiyo (WY)" [2]=> string(16) "Shiow Yong (BUN)" [3]=> string(14) "Seok Hoon (SH)" } – user3551936 Sep 29 '15 at 01:16

1 Answers1

0

to match a against multiple values you can use the mysql IN clause. you create a string of the values in quotes useing implode

$myselected     =   $_POST["auditor"];

if(count($myselected)>1){
$auditor = implode ("','",$myselected);
}else
$auditor =$myselected;
}

$query10 = "SELECT * FROM auditor WHERE auditor_name IN ('$auditor') ";

the rest is as you have above. You need to add user input sanitation to the above

  • it's works! But I only get the first item ID, the others item ID not appear. – user3551936 Sep 29 '15 at 04:37
  • do you mean auditor id? –  Sep 29 '15 at 04:41
  • oh- not answered - um sorry thought it was .. you should be looping through `$row10` if you want to see all the results of the query –  Sep 30 '15 at 00:38
  • Dagon, Thanks alot. Thank you for you kind advice :) – user3551936 Oct 01 '15 at 01:11
  • Dagon, one more question abot Select SUM** I SUM all mark using this method in query. Everything works well. $total=0; $sql = "SELECT form_details_section_id,SUM(mark) FROM audit_section_markrecord WHERE audit_section_id = '$audit_no' GROUP BY form_details_section_id "; $mark = $row['SUM(mark)']; $final_mark = $row1['final_mark']; $subtotal = round($mark/$final_mark); $total += $subtotal; but, how can I SUM again with all the SUM(mark) and set it as $total? is that possible to do that? – user3551936 Oct 01 '15 at 08:58