This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

fededark1702
fededark1702

Hi, I was playing with editor and I wanted to count the number of row in a table, so I made this

function onStart(){
$this["invitati"] = DB::table('invitati')
    ->selectRaw('COUNT(*) as presenti')    
    ->get();
}

and I want to print the value of invitati, but if I try to do

<div class="grid container">
<div class="col span-2"></div>
    <div class="col span-10">
        <p>{{ invitati }}</p>
    </div>
</div>

It returns me: An exception has been thrown during the rendering of a template ("Array to string conversion").

The variable should contain the value "2", how can I print it?

Last updated

daftspunky
daftspunky

Try DB::table('invitati')->count()

fededark1702
fededark1702

Thank you :)

Another question: if I recover data from a database using

$res = DB::table('invitati')->get();

it returns me an array of object. How can I do to extract value from it? I tried to do as say in this post but none of the solutions suggested seems to work.

EDIT:

Solved in this way:

DB::setFetchMode(PDO::FETCH_ASSOC);
$res = DB::table('invitati')->get();
DB::setFetchMode(PDO::FETCH_CLASS);

but still receive the same error as before (mysql_fetch_array() expects parameter 1 to be resource, array given)

EDIT 2.0

I solved making

    $nomi = array();
$cognomi = array();
$partec = array();
$regalo = array();

foreach($res as $inv)
{
    $nomi[] = $inv['nome'];
    $cognomi[] = $inv['cognome'];
    $partec[] = $inv['partecipazione'];
    $regalo[] = $inv['regalo'];
}

$this['nomi'] = $nomi;
$this['cognomi'] = $cognomi;
$this['partec'] = $partec;
$this['regalo'] = $regalo;

I know that probably this code sucks but it do what I want and is enought for now

Last updated

daftspunky
daftspunky

This should do what you need

DB::table('invitati')->get()->toArray();
fededark1702
pain-spark50564
pain-spark50564

daftspunk said:

This should do what you need

DB::table('invitati')->get()->toArray();

I am trying learn october too, but i am stuck in this part, i am trying show values like (select *) on a partial but i lost a whole day and not sucess... The doc its complicated for me ...

Last updated

IrreverentMrB
IrreverentMrB

Hi, I'm trying to do something similar except instead of printing I'm trying to push the select results from a table of a specific database to the default database of my plugin through a routes but doing this I'm getting a Undefined index: id_couleur so something must be wrong in how my array is defined

<?php

use Macro\Mes\Models\Couleur;
use Illuminate\Support\Facades\Db;

Route::get('hello', function () {

$fact = Db::connection('fact')
  ->table('glo_couleur')
  ->select(['id_couleur','tag_couleur','desc_couleur_fr','desc_couleur_en','ref_html'])
  ->where('obsolete','=', 0)
  ->orderBy('tag_couleur', 'ASC')
  ->get()
  ->toArray();

Couleur::create([
  'id_couleur' => $fact['id_couleur'],
  'tag_couleur' => $fact['tag_couleur'],
  'desc_couleur_fr'=> $fact['desc_couleur_fr'],
  'desc_couleur_en'=> $fact['desc_couleur_en'],
  'ref_html' => $fact['ref_html'],
]);
  return "Colors created";
});
Flynsarmy
Flynsarmy

It probably means your select statement is returning nothing from from the database.

if (empty($fact)) {
    return "Nothing to do";
}

1-8 of 8

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.