I am relatively new to php. I have a function to get all subjects by tableName. I also have a function where I am trying to get all pages where the subject_id (from pages table) matches the subject tables id.
Here are the error's and output:
PDOStatement Object ( [queryString] => SELECT * FROM subjects ORDER BY id ASC )
Array ( [0] => Array ( [id] => 1 [0] => 1 [menu_name] => About Us [1] => About Us [position] => 1 [2] => 1 [visible] => 1 [3] => 1 ) [1] => Array ( [id] => 2 [0] => 2 [menu_name] => Products [1] => Products [position] => 2 etc etc...
Notice: Undefined index: id in /Users/aaronhappe/Sites/php/CMS/includes/db.php on line 39
Fatal error: Call to a member function rowCount() on a non-object in /Users/aaronhappe/Sites/php/CMS/includes/db.php on line 41
And here is the code:
<?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";
}?>
If I pass $subjects[0] into my getAllLinkedId function, it solves the problem of the Array wrapping other arrays. However, what I am wanting is to spit out, for each subject, the corresponding page. Not just the pages which correspond to one single subject, but which respond to all subjects.
This is my view:
<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>