Error foreach codeigniter

Asked

Viewed 147 times

1

I’m in the following problem, I make one select in the database and display the images on the page, is a classified portal where the person makes an ad and can upload up to 10 images.

So far so good, I picked up the ad and it is displayed on the page, the problem that it is displaying the ad the same amount of times that it had image upload.

If the ad has 5 photos it displays 5 times, if it has one it displays once. What can I do for him to print only the first item of array?

class Carroshome extends CI_Model
{
    public function exibeCarrosHome($limit =0,$offset =0){

        if ($limit > 0)  $this->db->limit($limit,$offset);
        $this->db->select("*");
        $this->db->from("anuncios");
        $this->db->join("fotosanuncios", "anuncios.id = fotosanuncios.idPost");
        $this->db->order_by('id', 'DESC');

        return $valida = $this->db->get()->result_array();
    }

    public function totalLinhas(){
        return $this->db->count_all("anuncios");
    }
}

My foreach is like this.

        <div class="col s12 m6 l6 carrosRetangulo">
            <div class="col s12 m12 l12 carrosBanner">
                <div class="fundoFoto">

                    <a href="<?= base_url("car/$dado[id]") ?>" style="padding: 0 !important; margin: 0 !important; border:none !important;background: transparent !important;">
                        <img src="<?= base_url("$dado[caminho]thumb/$dado[thumb]") ?>" class="carrosFotomini" alt="">
                    </a>
                </div>
                <div class="detalhesCarro">
                    <ul class="itensCarroFront">
                    <li>Model : <?= substr($dado['modelo'],0,15) ?></li>
                        <li>State     : <?= $dado['stateAuto'] ?></li>
                        <li>Year      : <?= $dado['year'] ?></li>
                        <li>Miles     : <?= $dado['odometer'] ?></li>
                        <li>Fuel      : <?= $dado['fuel'] ?></li>
                        <li>Transmission : <?= $dado['transmission'] ?></li>
                    </ul>
                </div>
                <div class="precoCarro"><span class="precoVertical"><?=  ($dado['price'])? numeroEmReais($dado['price']) : "check" ?></span></div>
            </div>

        </div>
    <?php endforeach; ?>

Solved with Jonathan’s help

public Function puxaThumb($id){ $this->db->Where("idPost",$id); Return $this->db->get("photofunctions")->row_array();

}

1 answer

3


Bruno, what is happening is that when giving the Join with the table 'fotosanuncios' the main record, which is the car, ends up being replicated by the amount of existing records in the table 'fotosanuncios'.

SUGGESTION #1

Do not Join with the photo table, add a new key in the array returned by the query, for example $valida['photos']. Then feed this key with the result of an independent search in the table 'fotosanuncios'.

Run a foreach to retrieve the images for each main search result and then recover the images from each record.

SUGGESTION #2

Create a model for 'photofunctions' (if not available) and add a method that returns an array of images for each car. Call this method in the foreach of suggestion #1, instead of the explicit query. So you don’t need to run a query to another table in the 'Carroshome' controller, and keep your code more organized.

I hope I’ve helped.

  • Thank you Jonathan, I understood what I should do, I will create a function receiving an id and it makes a selection of the first line you find in the bank. Thank you very much, I don’t know how I didn’t think of it before, I’m working many hrs a day on this project and I think I’m bugging Rs.

  • 1

    Thanks, solved my problem. Follows model code. public Function puxaThumb($id){ $this->db->Where("idPost",$id); Return $this->db->get("fotosanuncios")->row_array(); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.