Jump to content

[SOLVED] Dynamic page titles


FridayRain

Recommended Posts

This is really irking me.

 

I have poetry.php that allows users to select a title of a poem, which passes a variable through the URL and the poem is then displayed on the same page. I figured I should have the page title change to correspond with the poem selected. If no piece is selected, then no special title text is added to the page title.

 

The code at the end of this post works if a poem is selected, but when a clean poetry.php is loaded, I get the following error:

 

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in C:\Web\XXXX\db\closedb.php on line 2

 

<?php
mysql_close($conn);
?>

 

 

Dynamic page title code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
import_request_variables('G', 'url_');
if ((isset($_GET[piece])) && ($_GET[piece] != "")) {
require("db/config.php");
require("db/opendb.php");
    $query = "SELECT text, title, year FROM poems WHERE id=". mysql_escape_string($_GET[piece]);
    $result = mysql_query($query);
    $content = mysql_fetch_assoc($result);

    $title = "> $content[title]";
} else {
    $title = "";
  }
require("db/closedb.php");
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title><?php echo "$title"; ?></title>
<link rel="shortcut icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="css.css" media="Screen,Projection,TV" />
</head>

 

The code is mostly identical to the code I use later in the page to display the piece selected, and it works fine. If no piece is selected, a message is displayed asking the user to select one. If a piece is selected, it displays.

 

Since the code works when a piece is selected, I'm thinking there's an issue with the isset function. I'm not really sure, but I'm betting it's something stupid that I should know.

Link to comment
Share on other sites

revised

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
import_request_variables('G', 'url_');
$ID = (int)$_GET['piece'];
if ($ID > 0)
{
require("db/config.php");
require("db/opendb.php");

    $query = "SELECT text, title, year FROM poems WHERE id=$ID";
    $result = mysql_query($query);
    $content = mysql_fetch_assoc($result);

    $title = $content['title'];
} else {
    $title = "";
  }
require("db/closedb.php");
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title><?php echo $title; ?></title>
<link rel="shortcut icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="css.css" media="Screen,Projection,TV" />
</head>

Link to comment
Share on other sites

Also instead of having a file for closing the db why not have a functions.php file that has all stuff like that in...

 

<?php
function dbConnect() {
    $dbhost = 'localhost';
    $dbuser  = '';
    $dbpass  = '';
    $dbname = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $selectdb = mysql_select_db($dbname);

    if($conn) {
        if(!$selectdb) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return false;
    }
}

function dbClose() {
    mysql_close($conn);
}

function freeResult($result) {
    mysql_free_result($result);
}
?>

 

I find it's an easier way of doing things.

 

Link to comment
Share on other sites

It's weird that the same config files work fine in every other MySQL query.

 

<?php
$dbhost = 'localhost';
$dbuser = 'XXXXX';
$dbpass = 'XXXXX';
$dbname = 'writing';
?>

 

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
?>

 

<?php
mysql_close($conn);
?>

Link to comment
Share on other sites

I don't believe i missed that!!!

 

your always attempting to close it..

just move it up, in the if block

 

FIXED

 

revised

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
import_request_variables('G', 'url_');
$ID = (int)$_GET['piece'];
if ($ID > 0)
{
require("db/config.php");
require("db/opendb.php");

    $query = "SELECT text, title, year FROM poems WHERE id=$ID";
    $result = mysql_query($query);
    $content = mysql_fetch_assoc($result);

    $title = $content['title'];
    require("db/closedb.php");
} else {
    $title = "";
  }

?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title><?php echo $title; ?></title>
<link rel="shortcut icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="css.css" media="Screen,Projection,TV" />
</head>

 

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.