Jump to content

Page titles/URL's and pagination error


fbm

Recommended Posts

Hi

 

I am building my first system with PHP and MySQL. The system is going to help me manage my clients and projects.

 

I have read teh pagination guide found here

 

http://www.phpfreaks.com/tutorial/basic-pagination

 

and the data is being being populated but i am getting problems because of my page title/URL's

 

My system is built in the below structure

 

index.php

includes/

pages/

images/

css/

 

index.php looks like this

 

<?php include "includes/config.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo "$sitetitle";?></title>
<link href="css/master.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="wrap">
<div id="header"><img src="images/banner_1.jpg" width="900" height="130" alt="Fine Box Media - CRM" /></div>
    <div id="menu"><?php include "pages/main_menu.php"; ?></div>
    <div id="page_content">
    <?php
	if (isset($_GET['page'])) {
	$page = $_GET['page'];
	} else {
	$page = "home";
	}
	include "pages/".$page.".php";
?>	
    </div>		
</div>

</body>
</html>

 

includes/config.php looks like this

 

<?php

//DB settings and connection
$dbhost = "localhost"; 
$dbname = "#######"; 
$dbuser = "########"; 
$dbpass = "########"; 

$db = mysql_connect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db); 

?>

<?php 

//Admin site Settings
$sitetitle = "Fine Box Media - CRM";

?>

 

So my config.php file handles the DB connection as it is included at the top of every page and then within index.php i use this to include my menu

 

<?php include "pages/main_menu.php"; ?>

 

main_menu.php is this

 

<ul>
    <li><a href="?page=home">home</a></li>
    <li><a href="?page=clients">clients</a></li>
    <li><a href="?page=invoices">invoices</a></li>
    <li><a href="?page=projects">projects</a></li>
    <li><a href="?page=support">support</a></li>
</ul>

 

and finnally i call each page from the page directory with this

 

<?php
	if (isset($_GET['page'])) {
	$page = $_GET['page'];
	} else {
	$page = "home";
	}
	include "pages/".$page.".php";
?>	

 

So my URL ends up like this when i am on home page for example

 

http://localhost/index.php?page=home

 

Sorry for such a long description of my code but i thought it would be best to show you what i have so far in order to get the best help.

 

So now for the questions.

 

1. Is my code a common way of doing things? I am questioning the way i have learnt to link to other pages as my page URL is not very friendly.

 

2. SEO friendly URL's is it possible with my chosen method to make SEO friendly URL's such as

 

http://localhost/home

 

3. Because of my page URL's the pagination work i have done from the tutorial does not work, the first page of results is populated but then the next and previous buttons have URL's like this

 

http://localhost/index.php?currentpage=2

 

when in fact the code is not just on the index.php page it is on this URL

 

http://localhost/index.php?page=client_list

 

Sorry very long winded way of explaining but hopefully i have given good info for you guys to offer any advice.

 

Thanks in advance.

Link to comment
Share on other sites

1. I think it's an OK way of doing it, I don't create pages like this using includes for the content but I know lots of people do, so it's got to be fairly popular.

 

2. If you have mod_rewrite then you can use it to make SEO friendly URL's.

 

3. You haven't included any pagination code so it's difficult to say why the links for next and previous don't work, but my guess would be that you'll need to use $_SERVER['QUERY_STRING'] or $_SERVER['REQUEST_URI'].

 

When you request index.php?page=home the value of $_SERVER['QUERY_STRING'] should be set to page=home.

 

Pagination links should then look at little like this:

 

$next = 'index.php?' . $SERVER_['QUERY_STRING'] . 'currentpage=' . $page;

 

Click here to find out more about the $_SERVER global.

 

Regards

Rich

Link to comment
Share on other sites

thanks rich,

 

here is the code which is generating the pagination links

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";

 

or the entire script looks like this

 

<?php
// database connection info
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM clients";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 2;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT client_id, client_name FROM clients LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['client_id'] . " : " . $list['client_name'] . "<br />";
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links	
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.