Solved

PHP - set page title from Database

Posted on 2010-09-20
16
928 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 82

Expert Comment

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

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
 
LVL 82

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now