Solved

PHP - set page title from Database

Posted on 2010-09-20
16
934 Views
Last Modified: 2013-12-12
Hi,

I am using CodeCharge to create a web application.  This application is being used by several users.  What I would like to do is to set the page title from a name in the database.  For instance, the name now is Customers.php.  I would like it to say "Company Name" Customers.php.  Does anyone know how to do this?
0
Comment
Question by:NerishaB
  • 7
  • 6
  • 3
16 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33714295
<title><?php echo $title?></title> where $title is fetched from the database.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33714299
Forgot a ';'.

<title><?php echo $title;?></title> where $title is fetched from the database.
0
 

Author Comment

by:NerishaB
ID: 33714309
Thanks, I am not too sure how to fetch the title from the database...  The <title> tag is found in the HTML tag of Codecharge.  How would I fetch data from the database in HTML?  Can you give me an excample?  I have a MySQL database.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33714333
HTML can't fetch from the database, that requires a server side program in a language like PHP.  Your Codecharge program probably has a feature somewhere to do that.  But I'm not familiar with Codecharge.
0
 

Author Comment

by:NerishaB
ID: 33714350
Yes, it does use PHP.  Would I then use php to set the title?

Something like:
<?php
  $title = $DBTitle;
where $DBTitle is feteched from the DB?
0
 
LVL 7

Expert Comment

by:haijerome
ID: 33714387
Hi,

    Its pretty simple...Follow the steps below :

   1. Open your PHP File..

   2. Kindly find your title tag and replace it with the code attached below :

   3. Save your PHP file and refresh your browser to see the new result....


  If it helps then its cool else let me know..

Regards,
Jerome Dennis D

         

 

     
<title><?php echo $DBTitle;?></title>

Open in new window

0
 

Author Comment

by:NerishaB
ID: 33715679
I tried that, but it does not work.

Here is my code:

//Custom Code @55-2A29BDB7
                     $username = "oztech";
      $password = "11111";
      $database = "oztest";
                     mysql_connect('localhost', $username, $password);
                     $query="Select OrgShortName from Globals WHERE GlobalID = 1";
      $DBTitle=mysql_query($query);    
      mysql_close();

//End Custom Code

Then I found the title tag and placed this there:
<title><?php echo $DBTitle;?></title>
0
 
LVL 7

Expert Comment

by:haijerome
ID: 33715956
Hi ,

   From your code i found some important things are missing in your code and i have listed it below. Kindly correct these things in your code:
 
          1. There is no connection variable : $cn =   mysql_connect('localhost', $username, $password);        
 
          2. I didn't find any Database select statement....Eg : mysql_select_db(oztest);
 
           3. You 're trying to fetch the title directly from the result set variable which in turn is an array...

      It should be like as follows
   
     $DBTitle=mysql_query($query,$cn) or die("Query Failed !!! Find the errors below : <br/>".mysql_error());

      $rowset = mysql_fetch_array($DBTitle);

      $title = $rowset['OrgShortName'];

   
  No problem  dude. I find that you are very new to PHP. In the beginning it all happens. But you should learn from your mistakes very importantly from others mistakes....      
   

Ok I've rewritten your queries with some changes find it below just replace your older one with mine. Before using it Kindly make sure the following things..

    1. Your username, Password and database name are the right ones...
   
    2. Place the code i've written even before the <html> tag ...(i.e.) that should be the first lines of the code file....

   
 Note  : Kindly remove the functions mysql_error() inside die statement once if you are moving to production environment....


If still you haven't get that feel free  to get back to me. I am here to help you always...  :-)
 



Regards,Jerome Dennis D

 
<?php
//Custom Code @55-2A29BDB7
      $username = "oztech";
      $password = "11111";
      $database = "oztest";
      $cn = mysql_connect('localhost', $username, $password) or die("CAnnot Connect to Databse Server !!! Find the errors below : <br/>".mysql_error());
     
      mysql_select_db($database,$cn) or die("CAnnot Connect to Databse !!! Find the errors below : <br/>".mysql_error());
      
      $query =                "SELECT OrgShortName
                               FROM Globals
                               WHERE GlobalID = 1";


      $DBTitle=mysql_query($query,$cn) or die("Query Failed !!! Find the errors below : <br/>".mysql_error());

      $rowset = mysql_fetch_array($DBTitle);

      $title = $rowset['OrgShortName'];

      mysql_close();

?>

<html>
    <head>
        <title>
           <?php echo $title;?>
        </title>
    </head>
    <body>
        
        <?php /*your stuff here*/ ?>
        
    </body>
</html>

Open in new window

0
 

Author Comment

by:NerishaB
ID: 33716076
I've added your code, and I dont get any errors, but instead of the correct title, I get the following:
<?php echo $title;?>
0
 
LVL 7

Expert Comment

by:haijerome
ID: 33716378
Hi
 
     1. At first confirm that your query is right by just running it in the mysql / phpmyadmin

       SELECT OrgShortName                                FROM Globals                                WHERE GlobalID = 1

   2. If it returns one or more rows then its fine ...we have check whether $title variable is holding the title by echoing it

    echo $title = $rowset['OrgShortName'];
 
If it does what is required then get back to me with in half an hour .. Bcoz i'll be offline after half an hour...

Regards,Jerome Dennis D

 
0
 

Author Comment

by:NerishaB
ID: 33716544
I tried both the SQL query, and the echo, both return the result I am looking for.  
0
 
LVL 7

Expert Comment

by:haijerome
ID: 33716727
Ok so the code and the query works fine the problem is with your comments ...


    Remove all your comments in the file whether its html comments / PHP comments remove it ...

  and try with this it will surely work....

Regards,
Jerome Dennis D
 
0
 
LVL 7

Expert Comment

by:haijerome
ID: 33716771
and also you commented some thing like this before this line

  instead of commenting like this

 <?php // echo $title;?>

you commented like this i assumed / suspect

 // </php echo $title;?>
0
 
LVL 7

Accepted Solution

by:
haijerome earned 500 total points
ID: 33716874
Hi i got your problem right ...


 Just find the file attached ... you can see exactly what you got right now....

Its all because of irrelevant commenting ...

    Actually thecode given below should display the title as jerome instead of it it will display  echo $title;?>

  But if you remove that ?> in the line <?php   // <?php echo $title ?> i mean the commented one ..... it displays the title as jerome..


And my suggestion / advice is use some good PHP editors with syntax highlighting feature like Netbeans preferrably ..and comment the php code with in the php tags like  <?php /*echo $title*;/?>

   find the wrong code below...and make the changes as i mentioned before to get it display as jerome as title...


Cheers..Jerome Dennis D
 

<?php
$title = 'jerome';

?>
<html>
    <head>
        <title>
            

       <?php   // <?php echo $title ?>
       

       echo $title;?>

        </title>

    </head>
    <body></body>
</html>

Open in new window

0
 

Author Comment

by:NerishaB
ID: 33717132
This is exactly wht I currently have.
<title><?php echo $title;?></title>

The problem is thi, I am using something called CodeCharge.  Codecharge seperates the HTML from the PHP.  Thats why I had to put the PHP code in the "Before Show" method of the page, and then I added the code above in the HTML page.
0
 

Author Closing Comment

by:NerishaB
ID: 33946100
Thanks, was a typo on my behalf.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question