Generate pages from database content

Hello, this is definitely a beginner question, but I have searched for awhile now but I haven’t been able to find an answer, so here is my dilemma:

I have a sql database setup which contains the title, description, content, etc. of a blog post. I want to run a php script which will load and link to that content dynamically without having to create a separate page for each post.

So far, I can get the link to generate, like so “index.php?article=articlename” using mysql_fetch_assoc, but I don’t know how to get the content to change when the link is clicked.

Here is the code I have so far:
[php]$selectposts = “SELECT postid, postName, postDetail, postDate FROM posts LIMIT 0, 10”;
$result = mysql_query($selectposts);
while ($curr_row = mysql_fetch_assoc($result)) {
echo ‘

’ . $curr_row[‘postName’] . ‘

’;
echo ‘

’ . $result[‘postDetail’] . ‘

’;
}
mysql_free_result($result);[/php]

Thank you

connect_to_mysql.php//DB connection file
[php]<?php

$db_host=‘localhost’;
$db_user=‘user1’;
$db_password=‘password1’;
$db_name=‘mydatabase’;

//connection
$myConnection= mysqli_connect($db_host, $db_user, $db_password,$db_name) or die(mysqli_error() );
?>[/php]

Your index page: index.php

[php]

<?php require_once ("scripts/connect_to_mysql.php");//DB connection on a separate file // DETERMINE which page ID to USE in our query below if (!isset($_GET['postid'])) { $pageid = '1'; //sets first page } else { $pageid = preg_replace('#[^0-9]#i', '', $_GET['postid']); // filters everything to avoid SQL injections } // Query the body section for the proper page content to be displayed $selectposts = "SELECT postid, postName, postDetail, postDate FROM posts LIMIT 0, 10"; $result = mysqli_query($myConnection, $selectposts) or die (mysqli_error()); //print mysqli error screen for debugging while ($curr_row = mysqli_fetch_assoc($result)) { $body = '

'. $curr_row['postid']. ' '.$curr_row['postName'].'

'.$curr_row['postDetail']; } mysqli_free_result($result); //CREATE LINK MENU $selectButtons = "SELECT postID, postName FROM posts LMIT 1,10 ORDER BY id ASC"; $query = mysqli_query($myConnection, $selectButtons) or die (mysqli_error()); $menuDisplay = ''; //sets variable for the links while ($curr_row = mysqli_fetch_array($query)) { $pid = $curr_row["id"]; $postName = $row["postName"];//Use the Post name as the label for the buttons. //Alternatively, you can add a linklabel field in your database. This is good, especially if your $postNames are too long for a button. That you you can have long post titles and short link button labels. //Prepare to Show and Highlight buttons if($pageid==$pid){ $pid = mysql_real_escape_string($pid);//Avoid SQL injections //use a CSS class to stylize the selected menu item $menuDisplay .= '


';} //use a second CSS class to stylize all other buttons of the menu else{ $menuDisplay .= '
';} } } mysqli_free_result($query); ?>

[/php]

Now, all you have to do is echo the $menu and $body variables where you want them to appear in your page. All this code is in your index page, except for the connection file.

This is how I’ve done it in my page: http://www.javierbooks.com

***********************
For simplicity, you can break this into two functions and then you call them in your index page. For instance, you create a separate file called functions. php and add it as an include in your index.php page. Here are the two functions for my page. I will not bother to adjust it to your code, as I think you can figure it out from here:

functions.php[/b]
[php]<?php
include(‘scripts/connect_to_mysql.php’);

//CREATE LINKS MENU function
function menu(){
$query = mysql_query(“SELECT id, linklabel FROM mybooks WHERE showing =‘1’ ORDER BY id ASC”) or die(mysql_error());

//DETERMINE which page ID to USE in our query below

if (!isset($_GET['bookid'])) {
	$pageid = '1';
} else {
	$pageid = preg_replace('#[^0-9]#i', '', $_GET['bookid']); // filter everything but numbers for security(new)
	
} 
	$menuDisplay = '';
	while ($row = mysql_fetch_assoc($query)) { 
	$bookid = $row['id'];
	$postLinkLabel = strtolower($row['linklabel']); //strtolower() lowercase buttons

	//Prepare to Show and Highlight buttons

	if($pageid == $bookid){
	$bookid = mysql_real_escape_string($bookid);//Avoid SQL injections
	//$menuDisplay .= '<a href="spanish.php?bookid=' . $bookid . '">' . $linklabel . '</a><br />';//original link

	//SHOW NORMAL STATE BUTTON
	$menuDisplay .= '<ul class="fontUL"> <li class="fontLI_selected"><a href="index.php?bookid='.$bookid. '">' . $postLinkLabel.'' . '</a></li></ul><br />';}

	//SHOW NORMAL STATE BUTTON
	else{$menuDisplay .= '<ul class="fontUL"><li class="fontLI"><a href="index.php?bookid='.$bookid. '">' . $postLinkLabel.'' . '</a></ul><br />';}

		}
		
		echo $menuDisplay;
	}

//AND HERE’S THE CONTENT FUNCTION

function bookContent(){

// DETERMINE which page ID to USE in our query below ********************************************************************
if (!isset($_GET['bookid'])) {
	$pageid = '1';
	
} else {
	
	$pageid = preg_replace('#[^0-9]#i', '', $_GET['bookid']);} // filter everything but numbers for security(new)
	//preg_replace($pattern, $replacement, $string);//preg_replace() Function structure

// Query the body section for the proper page
$query = mysql_query ("SELECT content,title,author FROM mybooks WHERE id = '$pageid' LIMIT 1 ") or die (mysql_error());  

while ($row = mysql_fetch_array($query)) { 

echo ucwords($row['title']).' por ';
echo '<b>'.$row['author']. '</b><br>';
	
/*++++++++++++++++++++++++++++++++++++++++++*/

//ADD LOAD javascript for FlipBook
//if( $pageid == 3)		
if (ucwords($row['title'])=='Libros')

echo '<div id="slick-show"> <a href="#">Haz click para leer ALGUNAS PAGINAS</a>  </div> 
	  		  
	  <div id="DISPLAYlibros">
	 
	  		<!--FLIPBOOK LOADS HERE-->
			
	  </div> '; 

	
//echo $row['content'];


echo $row["content"];

//ADD LOAD javascript for BLOG
//if( $pageid == 4) //use page id
	
if (ucwords($row['title'])=='Discusi&oacute;n') //use row title -- Capitalized word
echo '<div id="BLOG_show"><a href="#">Haz click para OPINAR</a></div> 
	  		  
	  <div id="DISPLAYblog">
	  
	  		<!--FLIPBOOK LOADS HERE-->
			
	  </div> '; 

//ADD CONTACT FORM
if (ucwords($row['title'])=='Contacto') //use row title -- Capitalized word
echo '<DI
	 <iframe src="contact/contact.html" width="445px" height="260" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=NO>
	  Your browser does not support iframes.
      </iframe>';
	
}

}?>[/php]

So your index page would look like something like this:
index.php

[php]My Home Page

<?php include ('myfunctions/functions.php');?>
<?php menu();?>
<?php bookContent();?>
?>[/php]

That’s it. There ARE some predefined php functions that use to handle text, like capitalization and lowercasing of the words. for Instance, I wanteD my menu texT to be lower case no matter how is typed in the database. For this I used strtolower($row[‘linklabel’]); // Notice that I have a link label field in my databAse. I used this, instead of using postTitle field in MySQL.

I learned this menu technique following this tutorial in YouTube. For whatever reason I cannot ost the link here. Just go to YouTube search flashbuilding How to Build Custom PHP and MySQL CMS Website Software.

I had to figure out how to break it in to two functions as it is not part of the tutorial. It’s always a good idea to have your page code i a different file. This is especially true with the database connection files.

I know this is a mouthful. But think of it as two different approaches to display your content. I personally prefer the later. I am almost sure there might be a typo here or there as I did a lot of copying pasting to help you out. but I think you can take it from here.

Good Luck!

Wow, +Karma ( not sure how to give that out on this forum) that was very helpful, I was definitely using a dated method, I like your approach much better.

Thanks a ton!

I am glad I was able to help.

Sponsor our Newsletter | Privacy Policy | Terms of Service