从ci中的数据库中获取和查看数据

时间:2016-06-23 06:02:19

标签: mysql codeigniter

我有 String is immutable test::3556498 testtre::-1422435371 testtretre::-1624680014 testtretretre::-855723339 testtretretretre::2071992018 testtretretretretre::-555654763 testtretretretretretre::-706970638 testtretretretretretretre::1157458037 testtretretretretretretretre::1835043090 testtretretretretretretretretre::1425065813 testtretretretretretretretretretre::-1615970766 String Buffer is mutable test::28117098 testtre::28117098 testtretre::28117098 testtretretre::28117098 testtretretretre::28117098 testtretretretretre::28117098 testtretretretretretre::28117098 testtretretretretretretre::28117098 testtretretretretretretretre::28117098 testtretretretretretretretretre::28117098 testtretretretretretretretretretre::28117098 这样的表

subjects

如何显示那些id parent_id subject_title 1 0 maths 2 1 algebra 3 0 English 4 3 essay 作为标题的科目和其他科目,如

id=0

2 个答案:

答案 0 :(得分:0)

由您决定如何获得记录。如果它是一个数组,你应该使用if数组索引。像那样的东西

for($i=0;$i<$count;$i++ ) {
 if($data[$i][0] ){
 echo "heading : {$data[$i][0]}";
 }else{
 echo "sub-heading   {$data[$i]}"
 }
}

这只是一个想法,可能无法使用您的代码所以请在使用之前进行修改,但主要想法是,如果它符合某些标准,它将以不同的方式显示 希望这会帮助你 。谢谢

答案 1 :(得分:0)

以下是基于您的表格和问题的示例代码

<?php
// Connecting to and selecting a MySQL database
$mysqli = new mysqli('host_name', 'your_user', 'your_pass', 'db_name');

// A connect_errno exists so the connection attempt failed!
if ($mysqli->connect_errno) {
    echo "Sorry, this website is experiencing problems."; 
    exit;
}

// Perform an SQL query
$sql = "SELECT * FROM subjects";

if (!$result = $mysqli->query($sql)) {
    // The query failed.    
    echo "Sorry, the website is experiencing problems.";
    exit;
}

// Fetch result
while ($subjects = $result->fetch_assoc()) {
    if($subjects['parent_id '] == 0){
        echo "<b>Heading:</b> ".$subjects['subject_title ']."<br>";
    }
    else{
        echo "<b>Sub-heading:</b> ".$subjects['subject_title ']."<br>";
    }
}
?>