PDA

View Full Version : [PHP] OOP - Ritorno di array di stringhe + smarty


x3d0
14-06-2008, 22:59
Ciao,
devo estrarre i commenti che gli utenti hanno mandati in merito ad una certa domanda/testo.
Per farlo utilizzo il metodo $answers->grab($id);
class answers{

function grab($qid){ //Stampa tutte le risposte di una certa domanda

$result = $this->query("SELECT * FROM answers WHERE qid='$qid'");



while ($row = mysql_fetch_array($result)) {


$this->row['user_id'];
$this->row['text'];
}
}

}
Ora però non so come estrarre i dati da questo metodo. Perchè se faccio:
$answers->row['user_id'] ottengo solamente l'ultimo record del database. Come faccio ad ottenerli tutti insieme.

Poi dovrei stamparli con smarty. Ma una volta che sono riuscito a farlo con il metodo, smarty non è un problema.

Quello che vorrei è che se faccessi foreach($answers as $answer){

echo $answer->text;
echo $answer->user_id;


}

insomma un array che contiene a sua volta gli array con il testo e l'user_id. Non so' se è l'array multidimensionale, ma non so' come strutturarlo?


grazie mille

kk3z
15-06-2008, 09:47
$this->row['user_id'];
Con questo in realtà non ottieni niente, forse intendevi
$this->row['user_id'] = $row['user_id'];

Io farei così:
class answers
{
var $rows = array()
....
while ($row = mysql_fetch_ASSOC($result))
{
$this->rows[] = $row;
}
}

Quindi:
foreach($answers->rows as $row)
{
echo $row['user_id'];
echo $row['text'];
}

x3d0
21-06-2008, 17:32
ok, ora xò non riesco a stamparlo con smarty. come fare?