• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Foreach with implode comma

#1
Thumbs Up 
Please i'm stucked with this problem for an hour now(yeah i'm newbie in this).

this is a piece of code in my controller


PHP Code:
$tags $this->input->post('check_list');
 
           
            
            foreach
($tags as $t){
 
               
                $q 
$this->db->get_where('info', array('text_id' => $t))->result()->text_name;
 
               //$data['q'] = $this->db->get_where('info', array('text_id' => $t))->result()->text_name;
 
               
                $data
['qe'] = $q.' ,';
 
           

and this is the view(index.php). i also wanted to see the query i did,so tried to output it with last_query() :
PHP Code:
<?php
    
echo form_open();
    
?>

<input type="checkbox" name="check_list[]" value="1">love<br>
<input type="checkbox" name="check_list[]" value="2">heart<br>
<input type="checkbox" name="check_list[]" value="3">heaven<br>
<input type="checkbox" name="check_list[]" value="4">sun<br>
<input type="checkbox" name="check_list[]" value="5">earth<br>
    
        <?php
    
echo form_submit('''Save');
    echo 
form_close();
    
?>
    
    <div>The Checked boxes names were: 

<?php echo $qe;
    
?></div>
     

and the printed result is:
Code:
,string(2) " ," string(4) "SELECT * FROM `info` WHERE `text_id` = '4'"

what i want to achieve is something like this depending on the selected checbox by their id:
The Checked boxes names were: sun, heaven, earth

UPDATE(8/6/2016 11:57pm)
Here is the fix:
PHP Code:
//in controller
$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;
 
               
            

}

$data['qe'] = immplode(', '$q);

//inside view

 
echo $qe// prints Love, Sun, Heaven (when their ids are being selected) 
Thanks y'all for your consern

Heart Cool Big Grin
Be Simple Angel
Reply


Messages In This Thread
[SOLVED] Foreach with implode comma - by waptik - 08-01-2016, 10:21 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.