-
Apr 3rd, 2012, 07:19 AM
#1
Thread Starter
New Member
Dynamic CMS problem
I am using PHP and MySQL to build a dynamic CMS. I am really new to this. I set up my SQL database with the following
web_pages
id page_title url content
These files have the following codes:
//header.php
<?php
//Variables for the database
$host = 'localhost';
$username = '###########';
$password = '######';
$dbname = '###########';
?>
//nav.php
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
$sql = "SELECT id, page_title, url FROM web_pages;";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
//loop through the page titles to populate the navigation bar
while ($row = mysqli_fetch_array($result)) {
$page_title = $row['page_title'];
$url = $row['url'];
echo "<ul><li><a href=' " . $row['id'] . " '> " . $row['page_title'] . "</a></li></ul>";
}
mysqli_close($conn);
?>
//default.php
<header>
<?php
require_once("header.php");
require_once("nav.php");
?>
</header>
<!-- +++++++++++++++++++++++ CONTENT +++++++++++++++++++++ -->
<div id="content">
<?php
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
$sql = "SELECT page_title, url, content FROM web_pages WHERE content = '$content';";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$page_title = $row['page_title'];
$url = $_GET['url'];
$content = $row['content'];
}
if(!isset($_GET['id'])) {
echo $row['content'];
}
else
echo "default.php";
mysqli_close($conn);
?>
</div>
Rather than the user click on the navigation and it directing to a new non-existant page I want the content from the database to show up in the content section of the default page.
PLEASE HELP!! I need to get this working by tonight!
-
Apr 3rd, 2012, 11:29 AM
#2
Re: Dynamic CMS problem
In this query:
Code:
$sql = "SELECT page_title, url, content FROM web_pages WHERE content = '$content';";
... $content doesn't appear to have been created beforehand; where do you expect to get this variable from? Seems like a logic problem too: if you've already got the page's content, you wouldn't need to query for it.
A common pattern for this sort of thing is using row IDs to fetch content. For example, you have the url "default.php?page_id=2" and you query for the content where ID is 2.
-
Apr 3rd, 2012, 01:22 PM
#3
Thread Starter
New Member
Re: Dynamic CMS problem
I finally managed to at least stay on the same page at this point. Still don't know how to fetch the content. How do you do it?
Code:
//NAVIGATION
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
//select the url
$sql = "SELECT * FROM web_pages;";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
//loop through the page titles to populate the navigation bar
while ($row = mysqli_fetch_array($result)) {
$page_title = $row['page_title'];
$url = $row['url'];
$content = $row['content'];
//echo "<ul><li><a href=' " . $row['id'] . " '> " . $row['page_title'] . "</a></li></ul>";
echo '<a href="default.php?content='.$row["id"].'">'.$row["page_title"].'</a>';
}
mysqli_close($conn);
//CONTENT
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
//select the url
$sql = "SELECT * FROM web_pages WHERE id = ".$_GET['content'];
$result = mysqli_query($conn, $sql) or die('Error querying database.');
//loop through the page titles to populate the navigation bar
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$page_title = $row['page_title'];
$url = $_GET['url'];
$content = $row['content'];
}
if(!isset($_GET['id'])) {
echo $row['page_title'];
echo $row['content'];
}
else
echo "default.php";
mysqli_close($conn);
-
Apr 3rd, 2012, 04:53 PM
#4
Thread Starter
New Member
Re: Dynamic CMS problem
Here is my revised code:
I can get the default.php template stay put but my content is saying error querying database. I wonder why?
NAVIGATION
Code:
<?php
//Connect to the db
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
//select the url
$sql = "SELECT * FROM web_pages;";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
//loop through the page titles to populate the navigation bar
while ($row = mysqli_fetch_array($result)) {
echo '<ul><li><a href="default.php?id='.$row["id"].'">'.$row["page_title"].'</a></li></ul>';
}
mysqli_close($conn);
?>
CONTENT
Code:
<?php
$conn = mysqli_connect($host, $username, $password, $dbname) or die('Error connecting to MySQL server.');
//select the url
$sql = "SELECT * FROM web_pages WHERE id = $_GET[id]";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
//loop through the page titles to populate the content box
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$page_title = $row['page_title'];
$url = $row['url'];
$content = $row['content'];
}
mysqli_close($conn);
?>
<?php
echo '<tr><td>' . $row['page_title'] . '</td>
<td>' . $row['content'] . '</td>';
?>
-
Apr 3rd, 2012, 10:35 PM
#5
Re: Dynamic CMS problem
Remove the ; after the query.
Code:
$sql = "SELECT * FROM web_pages";
-
Apr 4th, 2012, 03:28 AM
#6
Re: Dynamic CMS problem
Try changing this line:
Code:
$sql = "SELECT * FROM web_pages WHERE id = $_GET[id]";
to:
Code:
$sql = "SELECT * FROM web_pages WHERE id = {$_GET['id']}";
Or to:
Code:
$id = $_GET['id'];
$sql = "SELECT * FROM web_pages WHERE id = $id";
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|