Jump to content

How to get a row from a table and set it as the <title>


Noskiw

Recommended Posts

I'm having trouble with my code which is supposed to define the <title> of the page. This is my class code in which the $row variable is defined.

 

<?php

class page{
    
    var $host;
    var $user;
    var $pass;
    var $db;
    
    function connect(){
       $con = mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error());
       mysql_select_db($this->db, $con) or die(mysql_error()); 
    }
    
    function check_page($id){
        $sql = "SELECT * FROM page WHERE id='".$id."'";
        $res = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $page = $row['page'];
            $title = $row['title'];
        }
    }
}

?>

 

And my index page

 

<?php

include './class/class.php';

$obj = new page();

$obj->host = 'localhost';
$obj->user = 'root';
$obj->pass = '';
$obj->db   = 'p_web';

$obj->connect();

?>

<html>

<head>
<title><?php $obj->check_page(1); ?></title>
<link href="./css/style.css" rel="stylesheet" />
</head>

<body>
    <center>
        <div id="mainC" style="text-align: left;">
            <div id="header">
                header here
            </div>
            <div id="mainC">
                <div id="navbar">
                    <div class="button">
                        <a href="#">Home</a>
                    </div>
                    <div class="button">
                        <a href="#">About</a>
                    </div>
                    <div class="button">
                        <a href="#">Portfolio</a>
                    </div>
                    <div class="button">
                        <a href="#">Tutorials</a>
                    </div>
                    <div class="button">
                        <a href="#">Contact</a>
                    </div>
                </div>
            </div>
        </div>
    </center>
</body>

</html>

 

This is getting increasingly frustrating. If anyone could help me, I would be very appreciative.

You're not returning anything from your function. Since I really don't know classes that well, I could be mistaken. Also, since there's probably only one record in your database for each id, you don't need (and shouldn't use) the while loop.

 

Try this:

<?php
    function check_page($id){
        $sql = "SELECT * FROM page WHERE id='".$id."'";
        $res = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_assoc($res));
        return(array($row['title'],$row['page'],$row['id']));
    }
?>

and in the index.php

<title><?php list($title,$page,$id) = $obj->check_page(1); echo $title; ?></title>

 

Note: I could be way off base here.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.