MySQL Forums
Forum List  »  PHP

PLEASE HELP! lynda.com php with mysql essential training
Posted by: ryan shannon
Date: January 19, 2013 03:53AM

Hi All,

I am extremely new to PHP and have been taking the PHP Essentials training course at lynda.com. I have run into a problem on chapter 13.2. I have all the code correct according to the instructor but my code does not work as it should. For every subject the <h2> should update to the menu_name in the database and it should do the same for each page. Mine however, does not update the page; only the subject. This is very frustrating as I am trying to learn and follow along but I keep running into these problems although my code is exactly the same as the instructors. Please, I am pulling my hair out here because I was steaming along and now I have been stumped for a week now. Any help will be greatly appreciated. The code is as follows:

FUNCTIONS:
<?php
//This file is the place to store all basic functions

function confirm_query($result_set){
if (!$result_set){
die("Database query failed: " . mysql_error());
}
}
function get_all_subjects(){
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}

function get_pages_for_subject($subject_id){
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id={$subject_id}
ORDER BY position ASC";

//pages
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id='" . "$subject_id" . "' ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);

//REMEMBER:
//if no rows are returned, fetch array will return false
if ($subject = mysql_fetch_array($result_set)){
return $subject;
} else {
return NULL;
}
}

function get_page_by_id($page_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id='" . "$page_id" . "' ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);

//REMEMBER:
//if no rows are returned, fetch array will return false
if ($page = mysql_fetch_array($result_set)){
return $page;
} else {
return NULL;
}
}
?>

CONTENT PAGE CODE:
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_GET['subj'])){
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
} elseif (isset($_GET['page'])){
$sel_subject = NULL;
$sel_page = get_page_by_id($_GET['page']);
} else {
$sel_subject = NULL;
$sel_page = NULL;
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subject['id']) { echo " class=\"selected\""; }
echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page['id']) { echo " class=\"selected\""; }
echo "><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if (!is_null($sel_subject)) { // subject selected ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif (!is_null($sel_page)) { // page selected ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or page to edit</h2>
<?php } ?>
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>

Thanks for the help and advice.

Options: ReplyQuote


Subject
Written By
Posted
PLEASE HELP! lynda.com php with mysql essential training
January 19, 2013 03:53AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.