我对php比较新。我有一个函数来通过tableName获取所有主题。我也有一个函数,我试图获取subject_id(来自pages表)匹配主题表id的所有页面。
以下是错误和输出:
PDOStatement对象([queryString] => SELECT * FROM subject ORDER BY id ASC)
数组([0] =>数组([id] => 1 [0] => 1 [menu_name] =>关于我们[1] =>关于我们[position] => 1 [2] => 1 [可见] => 1 [3] => 1)[1] =>数组([id] => 2 [0] => 2 [menu_name] =>产品[1] =>产品[位置] => 2等等......
注意:第39行/Users/aaronhappe/Sites/php/CMS/includes/db.php中的未定义索引:id
致命错误:在第41行的/Users/aaronhappe/Sites/php/CMS/includes/db.php中的非对象上调用成员函数rowCount()
以下是代码:
<?php
// controller file
include '../admin-head-includes.php';
$subjects = getAll('subjects', $conn);
$subjectId = $subjects->fetchAll();
print_r($subjects);
echo "<br>";
echo "<br>";
print_r($subjectId);
$pages = getAllLinkedId('pages', $subjectId, $conn);
view('manage-content', array(
'subjects' => $subjects,
'pages' => $pages,
));
// from my db functions file
function getAllLinkedId($tableName, $tableName2, $conn) {
try {
$result = $conn->query("SELECT * FROM $tableName WHERE subject_id = {$tableName2['id']} ORDER BY id ASC");
return ($result->rowCount() > 0)
? $result
: false;
} catch (Exception $e) {
return false;
}
<!-- standard functions file -->
<?php function view($path, $data = null) {
if ($data) {
extract($data);
}
$path = $path . '.views.php';
include "views/$path";
}?>
如果我将$ subject [0]传递给我的getAllLinkedId函数,它解决了Array包装其他数组的问题。但是,我想要的是为每个主题吐出相应的页面。不仅仅是对应于一个单一主题的页面,而且它对所有主题作出响应。
这是我的观点:
<div class="subjects">
<ul>
<?php
foreach ($subjects as $subject) {?>
<li>
<?=$subject['menu_name'];?>
</li>
<ul>
<?php foreach ($pages as $page) {?>
<?=$page['menu_name']?>
<?php }?>
</ul>
<?php }?>
</ul>
</div>
答案 0 :(得分:0)
第一个问题:
Notice: Undefined index: id in ...
未设置数组$tableName2
中的索引ID,请检查是否已设置
第二个问题:
Fatal error: Call to a member function rowCount() on a non-object ..
$conn->query()
on失败返回非对象,检查$result
是否为对象
在您的代码中:
function getAllLinkedId($tableName, $tableName2, $conn) {
// check if index id is set
if( !isset($tableName2['id']) )
return false;
try {
$result = $conn->query("SELECT * FROM $tableName WHERE subject_id = {$tableName2['id']} ORDER BY id ASC");
// check if $result is object
if( !is_object($result) )
return false;
return ($result->rowCount() > 0) ? $result : false;
} catch (Exception $e) {
return false;
}
}
答案 1 :(得分:0)
我离开了这个基础。但我已经弄清楚了。我发布我的解决方案来关闭它。
<?php
// controller file
include '../admin-head-includes.php';
$subjects = getAll('subjects', $conn);
$pages = getAll('pages', $conn);
$subjects = $subjects->fetchAll();
$pages = $pages->fetchAll();
view('manage-content', array(
'subjects' => $subjects,
'pages' => $pages,
));
<!-- what my view function does -->
<?php function view($path, $data = null) {
if ($data) {
extract($data);
}
$path = $path . '.views.php';
include "views/$path";
}?>
<!-- view -->
<div class="subjects">
<ul>
<?php
foreach ($subjects as $subject) {?>
<li>
<?=$subject['menu_name'];?>
<ul>
<li>
<?php foreach ($pages as $page) {?>
<ul>
<li>
<?php
if ($page['subject_id'] == $subject['id']) {
echo $page['menu_name'];
}
?>
</li>
</ul>
<?php }?>
</li>
</ul>
<?php }?>
</li>
</div>
</ul>