简体   繁体   中英

Echo arrays in foreach loop codeigniter

I've been using codeigniter for years but there's a really big gap in between so i always find myself in situations where i forgot how to do things and its almost midnight here so my brain isn't working fast. Can someone show me how to echo the array i have and explain to me how they are processed in the foreach loops?

I have this code in my model to take the rows in 2 tables.

public function tag_genre(){
$result['tag'] = $this->db->get('tags')->result_array();
$result['genre'] = $this->db->get('genre')->result_array();
return $result;
}

And I have this in my controller

public function view_publish_story(){
$data = array('tag_genre' => $this->story_model->tag_genre(), 'title' => "New Story");

$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');   
}

I used print_r in my view and this is the result. Seeing this just confuses me more. It's been atleast 2 years since i even dealt with foreach loops.

Array ( [tag] => Array ( [0] => Array ( [tag_id] => 1 [tag_name] => LitRPG ) [1] => Array ( [tag_id] => 2 [tag_name] => Virtual Reality ) [2] => Array ( [tag_id] => 3 [tag_name] => Cyberpunk ) [3] => Array ( [tag_id] => 4 [tag_name] => Reincarnation ) [4] => Array ( [tag_id] => 5 [tag_name] => Summoned Hero ) [5] => Array ( [tag_id] => 6 [tag_name] => Martial Arts ) [6] => Array ( [tag_id] => 7 [tag_name] => Slice of Life ) [7] => Array ( [tag_id] => 8 [tag_name] => Overpowered ) [8] => Array ( [tag_id] => 9 [tag_name] => Non-Human ) [9] => Array ( [tag_id] => 10 [tag_name] => Anti-hero ) ) [genre] => Array ( [0] => Array ( [genre_id] => 1 [genre_name] => action ) [1] => Array ( [genre_id] => 2 [genre_name] => adventure ) [2] => Array ( [genre_id] => 3 [genre_name] => comedy ) [3] => Array ( [genre_id] => 4 [genre_name] => Drama ) [4] => Array ( [genre_id] => 5 [genre_name] => Fantasy ) [5] => Array ( [genre_id] => 6 [genre_name] => Historical ) [6] => Array ( [genre_id] => 7 [genre_na me] => Horror ) [7] => Array ( [genre_id] => 8 [genre_name] => Psychological ) [8] => Array ( [genre_id] => 9 [genre_name] => Romance ) [9] => Array ( [genre_id] => 10 [genre_name] => Sci-fi ) [10] => Array ( [genre_id] => 11 [genre_name] => Mystery ) [11] => Array ( [genre_id] => 12 [genre_name] => Tragedy ) [12] => Array ( [genre_id] => 13 [genre_name] => Short Story ) [13] => Array ( [genre_id] => 14 [genre_name] => Satire ) ) )

I've been looking at various questions regarding arrays from assoc to multidimensional and other stuff. Then i finally visited php manual for foreach loop and i finally was able to echo the array. The problem now is that although it echoes my array it also has an error for each, i can probably fix the error part by declaring it as a defined variable. My question is, is there any other better way? or any improvement on how i made my array to make it cleaner or easier to print?

foreach($tag_genre as $key => $value){
    foreach($value as $values){
        echo $values['tag_name'];
    }
}

UPDATE: i changed the way i made the array.

This is now my model:

public function get_tags(){
    $query = $this->db->get('tags')->result_array();

    foreach($query as $row){
    $result[$row['tag_id']] = $row['tag_name'];}
    return $result;
}

public function get_genre(){
    $query = $this->db->get('genre')->result_array();

    foreach($query as $row){
    $result[$row['genre_id']] = $row['genre_name'];}
    return $result;
}

and my controller:

public function view_publish_story(){
$data = array('tag' => $this->story_model->get_tags(), 'genre' => $this->story_model->get_genre(), 'title' => "New Story");

$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');   
}

and in my view:

<tr>
<div class="form-group">
    <td><label for="genre">Genre</label></td>
    <?php  
    foreach($genre as $genre_id => $genre_name){
    echo "<td><label class='checkbox-inline'><input type='checkbox' value=''>".$genre_name."</label><td>";
    }
    ?>
</div>
</tr>

My problem now is that, how do i fit all these into my table? I just ruined my table right now since i echoed them all in a single line. How do i do it so that it is printed in 3 columns?

foreach($tag_genre as $key => $value){
    foreach($value as $values){
        echo '<pre>';
        echo $values['tag_name'];
    }
}

this will format your array in a way you can read it and understand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM