Можно ли использовать foreach в foreach в codeigniter? - PullRequest
0 голосов
/ 17 марта 2020

Вот моя функция - я получаю ошибку.

В переменной $data['stars'] Я получил максимальное количество звездочек с недопустимым

Я хочу правильные звезды из базы данных, потому что на самом деле таблицы звезд разные, вот почему.

Возможно ли получить звезды из другой таблицы в этом foreach для получения точного значения?

public function dbmovies()
{
    $this->db->order_by("videos_id", "desc");
    $this->db->where(array('publication' => 1, 'is_tvseries' => 0));
    $query = $this->db->get('videos');

    //get stars
    foreach ($query->result() as $key => $value) {
        $this->db->where_in('star_id', $value->stars);

        $queryStars = $this->db->get('star');
            $stars=array();

        foreach ($queryStars->result() as $star) {
            $stars[] = $star->star_name;
            $starone = implode(", ", $stars);

        }

        $data['videoId'] = $value->videos_id;
        $data['imdbid'] = $value->imdbid;
        $data['title'] = $value->title;
        $data['description'] = $value->description;
        $data['duration'] = $value->runtime;
        $data['views'] = $value->total_view;
        $data['stars'] = $starone;

        $alldata[] = $data;
    }

    $get['data'] = $alldata;
    echo json_encode($get, JSON_PRETTY_PRINT);
}

И вывод tge у меня получается вот так

{
    "data": [
        {
            "videoId": "47",
            "imdbid": "tt2935510",
            "title": "Ad Astra",
            "description": "<p>The near future, a time when both hope and hardships drive humanity to look to the stars and beyond. While a mysterious phenomenon menaces to destroy life on planet Earth, astronaut Roy McBride undertakes a mission across the immensity of space and its many perils to uncover the truth about a lost expedition that decades before boldly faced emptiness and silence in search of the unknown.<\/p>",
            "duration": "123 Min",
            "views": "2",
            "stars": "Brad Pitt"
        },
        {
            "videoId": "45",
            "imdbid": "tt8243160",
            "title": "Hacker",
            "description": "<p>13-year-old Benjamin discovers that his mother didn\u2019t die in an accident as he was led to believe. The trail leads to high-ranking officials in the Danish Secret Service. \"Trust no one!\", he is told.<\/p>",
            "duration": "96 Min",
            "views": "93",
            "stars": "Brad Pitt, Rumle Krs"
        },
        {
            "videoId": "44",
            "imdbid": "tt7131622",
            "title": "Once Upon a Time... in Hollywood",
            "description": "<p>A faded television actor and his stunt double strive to achieve fame and success in the film industry during the final years of Hollywood's Golden Age in 1969 Los Angeles.<\/p>",
            "duration": "161 Min",
            "views": "71",
            "stars": "Brad Pitt, Rumle Krs, Leonardo DiCaprio"
        },

Может кто-нибудь, пожалуйста, помогите мне?

Ответы [ 2 ]

0 голосов
/ 17 марта 2020

Вы должны переместить переменную $starone за пределы foreach l oop, чтобы она не перезаписывалась внутри l oop:

        foreach ($queryStars->result() as $star) {
            $stars[] = $star->star_name;
        }
        $starone = implode(", ", $stars);
0 голосов
/ 17 марта 2020

Добавить пустой массив перед foreach $ stars = array ();

           $stars=array();
        foreach ($queryStars->result() as $star) {
            $stars[] = $star->star_name;
            $starone = implode(", ", $stars);
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...