Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Foreach with implode comma
#1
Thumbs Up 
(This post was last modified: 08-06-2016, 05:01 PM by waptik. Edit Reason: Added the fix to my problem )

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
#2

Sorry, but I do not understand what you are trying to achieve. Can you explain again? And what is going wrong exactly?
Reply
#3

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. 
Reply
#4

(This post was last modified: 08-06-2016, 04:55 PM by waptik.)

(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 
Be Simple Angel
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB