error foreach codeigniter

1

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

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

If the ad has 5 photos it is displayed 5 times, if it has an exhibit once. What can I do to print only the first item in the 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 looks 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 pullAThumb ($ id) {         $ this-> db-> where ("idPost", $ id);         return $ this-> db-> get ("snapshots") -> row_array ();

}
    
asked by anonymous 08.11.2016 / 19:27

1 answer

3

Bruno, what is happening is that by giving the join with the 'fotosununcios' table the main registry, which is the car's registry, ends up being replicated by the amount of records in the 'fotosununcios' table.

SUGGESTION # 1

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

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

SUGGESTION # 2

Create a model for 'snapshots' (if you do not have one) and add a method that returns an array with the images for each car. Call this method in the foreach of suggestion # 1, instead of the explicit query. So you do not need to query another table in the 'CarsHome' controller, and keep your code more organized.

I hope I have helped.

    
08.11.2016 / 19:56
___ ___ erkimt Sharing the variable $ scope from one controller to another with parameters in AngularJS ______ qstntxt ___

I have this:

%pre%

I wanted my second controller to get the $ scope.getIdJobs variable and save it to a variable again. How can I do this?

%pre%

app.js

%pre%     
______ azszpr165042 ___

A simple solution is to have a factory that returns the value that you need (an object for example): JS:

%pre%

HTML:

%pre%

Demo: link

When applications get larger, more complex, and harder to test, you may not want to expose the entire object of a factory in this way, but instead give limited access for example via getters and setters:

%pre%

With this approach controllers consume the service with new values and use $ watch to observe the changes:

%pre%

HTML:

%pre%

Demo: link

    
______ azszpr164875 ___

I'm a beginner too but I'll try to help you by giving two tips:

  • There is %code% that holds information and when loaded can be passed from controllers to controllers, much used for IDs.

  • Another way would be to store this %code% in a service and then use it in other controllers by calling it: %code%

  • ______ azszpr164891 ___

    You can put the products variable in $ rootScope, so you can access it for $ rootScope anywhere. But I advise you to use the ui-router to make the parameters pass, it gets more cohesive and with less chances of you having undesirable variable value substitution problems, or something like that.

    You should set the parameter in your state:

    %pre%

    And it will pass it this way:

    %pre%     
    ______ azszpr165044 ___

    Good,

    In my opinion it is advisable to create a %code% for the date, which will be %code% , where you can encapsulate the date and its %code% . Then just call the %code% you need in the controllers.

    Here's the example in this jsfiddle

    %pre%

    In the controller that accesses %code% you call the %code% method.

    %pre%

    In the second controller, just declare %code% or if necessary you can filter again with a new id.

    %pre%

    In this way, you can clear the %code% , and everything relative to that date can be reached in %code% and %code% easily.

    Or you will have the option of %code% , which also works for what you want.

    About routers, I advise you to take a look at ui-router , a far superior tool in my understanding.

        
    ___ Confirmation of registration data E-mail Password