Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Foreach with implode comma
#5

(08-06-2016, 04:53 PM)waptik Wrote:
(08-04-2016, 12:50 PM)PaulD Wrote: Sorry, but I do not understand what you are trying to achieve. Can you explain again? And what is going wrong exactly?
Thanks for your reply @PaulD. well what i wanted to achieve was to retrieve the selected checkboxes id(1,2 0r 4) then print their values from db using foreach and implode so it may look like: Love, Heaven, Sun.
(08-05-2016, 01:47 AM)Wouter60 Wrote: If it's only about the text value, why don't you assign that as the value attribute of the checkbox?
PHP Code:
<input type="checkbox" name="check_list[]" value="love">love<br

If you need both the number and the text, I would suggest getting the id's and names from your database and put them into an array (in your controller):
PHP Code:
$info = array();
$query $this->db->query("SELECT id,text_name FROM info");
foreach (
$query->result() as $i) {
 
  $id $i->id;
 
  $info[$id] = $i->text_name;
}
// result of this is: $info[1] = 'love', $info[2] = 'heart' etc.

$tags $this->input->post('check_list');
$selected = array();
foreach (
$tags as $t) {
 
   $selected[] = $info[$t];
}
$endresult implode(',' $selected);
// result of this: the "names" of the selected textboxes, seperated by comma's, so no comma after the last one. 

@Wouter60 your comment was good(the second php code section,that was what i wanted) but it came late as i already had a friend who showed me where i was wrong. here is my fix:
PHP Code:
$tags $this->input->post('check_list');
 
      if (isset($tags)){
 
       $q = [];
 
            
            
            foreach
($tags as $t){
 
               
                $q
[] = $this->db->get_where('info', array('text_id' => $t))->result()->text_name;
 
               
            

}
 echo 
implode(", "$q); // prints Love, Sun, Heaven 

Not good. I can change the content from check_list. I can spam the log or other funny stuff Smile
Reply


Messages In This Thread
[SOLVED] Foreach with implode comma - by waptik - 08-01-2016, 10:21 PM
RE: struggling with foreach using checkbox values - by Paradinight - 08-07-2016, 04:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB