Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP - set page title from Database

Posted on 2010-09-20
16
Medium Priority
?
957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 84

Expert Comment

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

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 84

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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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