June 2, 200818 yr Hi there, I'm starting to make my own CMS, and run into a couple of problems. To start with I have made a navigation bar using data from a MySQL database, as below; <?php $subject_set = get_all_subjects(); while ($subject = mysql_fetch_array($subject_set)) { echo "<li><a href-\"content1.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>"; $page_set = get_pages_for_subject($subject["id"]); echo "<ul>"; while ($page = mysql_fetch_array($page_set)) { echo "<li><a href=\"content1.php?page=" . urlencode($page["id"]) . "\" >{$page["menu_name"]}</a></li>"; } echo "</ul>"; } ?> I then draw back relevent data from the DB to populate the content on the page like so; <?php if (isset($_GET['subj'])) { $sel_subj = $_GET['subj']; $sel_page = ""; } elseif (isset($_GET['page'])) { $sel_page = $_GET['page']; $sel_subj = ""; } else { $sel_subj = ""; $sel_page = ""; } ?> <head></head> <body> <div id="right"> <h1><?php echo ($subject["menu_name"]); ?></h1> <p><?php echo $sel_page; ?></p> </div> </body> The $sel_page works fine, but what I am wanting to do is make the h1 tag populate with the menu name from subjects table. What I currently have in the h1 tags does not seem to work, can anyone advise why? Cheers Jamie
June 2, 200818 yr Have you tried <h1><?php echo $subject["menu_name"]; ?></h1> without the parentheses?
June 3, 200818 yr Author Didnt work. I have added the below to my functions php; 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); // if no rows are returned fetch_arrey will return false if ($subject = mysql_fetch_array($result_set)) { return $subject; } else { return NULL; } } And this to my actual page; <?php $sel_subject = get_subject_by_id($sel_subj); ?> <h1><?php echo $sel_subject['menu_name']; ?></h1> However when I come to open that page I get this error; Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1 Am I missing something really daft here, or is there an issue with the LIMIT function with version 5?
June 3, 200818 yr Try changing this line: $query .= "WHERE id=" . $subject_id; to this: $query .= "WHERE id= $subject_id"; As you're using double quotes, the variable should be read.
June 3, 200818 yr Author Try changing this line:$query .= "WHERE id=" . $subject_id; to this: $query .= "WHERE id= $subject_id"; As you're using double quotes, the variable should be read. Nah still no joy. This one is really frying my brain lol! I think I'm in a little deep over my head now
June 3, 200818 yr Have you tried putting the query into one line: $query = "SELECT * FROM subjects WHERE id=$subject_id LIMIT 1";
June 3, 200818 yr Author Yeah tried both ways, its wierd 'cos if I try to access the page using "content.php?subj=5" the page displays correctly. However if I try using a page= variable or no variables at all that error appears - its baffled me (but thats not hard!!)
Create an account or sign in to comment