Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Foreach in function DB class
  • Opa tranquilo?

    I have an appointment and need of the fields the result return me "modified". In this case, the field "descricao" return only 10 characters, using the truncate func. However, does not show me any error, only it's not working too ... suggestions?

    https://gist.github.com/YuriFontella/e6afae3caf1cfe7e87d5
  • You are changing the loop variable of the foreach, which is always a copy, unless you use the & (by reference).

    If you do that, you will get an exception, since database results are read-only and can not be modified.

    Instead, I would have your RDBMS handle this using something like this:

        $query = DB::select('*')
          ->select(array(DB::expr('LEFT(`descricao`,10)'), 'descricao'))
          ->from('publics')
          ->limit(4)
          ->execute();

    It saves you having to loop over the result. If you want the loop, do this:

    public static function publics_slide()
    {
        $query = DB::select()
            ->from('publics')
            ->limit(4)
            ->execute();

        $result = array();

        if ($query)
        {
            foreach($query as $row)
            {
                $row['descricao'] = Str::truncate($row['descricao'], 10);
                $result[] = $row;
            }
        }
       
        return $result;  
    }
  • Mas que barbaridade!

    Harro Verton, thank you very much, was all day puzzling over it the two modes have worked. Thanks again for the explanation, solved: D

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion