0

我能够以数组形式从我的数据库表中获取值。但我不知道如何在我的 <table><tr><td>// values of subjectId comes here </td> <td>//values of subject Name comes here</td></tr></table>. 我想在我的标签中一一迭代所有字段值。

在数组中我得到这样的值:

FIctionNon FIctionArray ( [0] => stdClass Object ( [subject_id] => 1 [subject_name] => FIction [creater_id] => 1 [created_date] => 2012-10-07 16:49:12 [update_id] => 1 [updated_date] => 2012-10-07 16:49:12 ) [1] => stdClass Object ( [subject_id] => 2 [subject_name] => Non FIction [creater_id] => 1 [created_date] => 2012-10-07 16:49:12 [update_id] => 1 [updated_date] => 2012-10-07 16:49:12 ) )

我获取输出的代码如下:


     $con = new connection(); 
     $info = new Subdetails_setup($con); 
     $results = $info->getSubjectInfo(); 
     print_r($results);

我的方法看起来像这样:

 function getSubjectInfo()
 { 
    $sub_info = $this->con->prepare("SELECT * FROM subjectdetails");
    $sub_info->execute();

    $results = $sub_info->fetchAll(PDO::FETCH_OBJ);

    foreach ($results as $key)
        { 
           echo $key->subject_name; 
        }
    // Return the result array
        return $results;
   }  

我的桌子是。“主题详情”

        subject_id int(3) NO PRI NULL auto_increment
        subject_name varchar(150) NO NULL
        creater_id int(5) NO NULL
        created_date datetime NO NULL
        update_id int(5) NO NULL
        updated_date datetime NO NULL

. 请帮助我。

4

1 回答 1

0

To split apart your arrayed values, couldn't you use the explode function? I'm unfamiliar with it so I wouldn't be able to help with that. But, could you go about collecting your values, and echoing them a different way? Like shown below

$query = mysql_query("SELECT * from subjectdetails ORDER BY `subject_id` DESC ");
while($row = mysql_fetch_array($query)){
   $subjectid = $row['subject_id'];
   $subjectname = $row['subject_name'];
   (The rest of your values here)
   echo "
<table><tr><td>$subjectid</td> <td>$subjectname</td></tr></table>
";
}

I did not test that, but it should work.

于 2012-10-15T03:04:55.447 回答