I am using php::pdo as my database table locator. I wanted to show the content of a table from my database but I dont know the right way to get it/ declare.
Here is what I’ve done in my viewuserpost.php
<?php
$uid = Yii::app()->session[‘iduser’];
echo $uid;
?>
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'dataProvider' => new CArrayDataProvider($results),
'columns' => array( 'Title', 'Content'
array( //I think my problem starts here
'name' => 'title',
'type' => 'raw',
'value' => 'CHtml::encode($data->title)'
);
//specify the colums you wanted here
),
));
?>
here is what I’ve done in my Model ContentForm.php
public function getTitle() {
$uid = Yii::app()->session['iduser'];
$db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', 'joseph', 'pcG559Jh');
$sql = "SELECT title FROM tbl_content where userid = '$uid' ";
foreach ($db->query($sql) as $row) {
$this->title = $row['title'];
array_push($this->results,$row['title']);
}
}
public function getResults() {
return $this->results;
}
public function getUserPost(){
$results = array();
$db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', 'joseph', 'pcG559Jh');
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = 'SELECT title FROM tbl_content ORDER BY title';
}
here is my controller SiteController.php
public function actionViewUserPost()
{
$results = array();
$model=new ContentForm('getUserPost');
$db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', '*****', '******');
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = 'SELECT title FROM tbl_content WHERE userid = 1';
$this->render('viewuserpost',array('results'=>$results));
}