tcorbeil Posted March 16, 2007 Share Posted March 16, 2007 Hello everyone. Is there a command or a procedure to get a page title and assign it to a php variable? Thanks. T. Quote Link to comment Share on other sites More sharing options...
peeps Posted March 16, 2007 Share Posted March 16, 2007 Are you looking for something like this? <title> <?php echo "$title"; ?> </title> If not could you provide a little more info? Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Ok.. I confess that I'm a newbie with respect to PHP.. I your code, let's say I open up the webpage google.ca.. The page title is google... in the code you presented, would the variable $title = google? If this is the case, then yes this is what I'm after.. Thanks. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 16, 2007 Share Posted March 16, 2007 if you want to get what some other page is displaying that will not work... if you are writing a page, and you want google to be your title... the you would assign the string google to the variable $title and echo (or print) that in between your title tags. Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 16, 2007 Share Posted March 16, 2007 Ok.. I confess that I'm a newbie with respect to PHP.. I your code, let's say I open up the webpage google.ca.. The page title is google... in the code you presented, would the variable $title = google? If this is the case, then yes this is what I'm after.. Thanks. no, understand this, PHP is not like html it is known for doin the background work. HTML is still used to edit <title> and make tables etc. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Thanks for your replies. Actually I do get the PHP doing the work concept.. I have many pages for my own website and a database storage for each page.. I was hoping to use the page title to store into a php variable so then I could retrieve the information required from the appropriate database (given name by the page title..) There might be another way of doing this but so I welcome any other suggestions to the matter. Basically something like this in english: The setup: Database has several table aaaa, bbbb, cccc. 1) PHP script: get page title and store in a variable called $pageID (page title is cccc) therefore $pageID = cccc 2) php script to get all information in mySQL under cccc and return to print on website Note: Because all pages are going to be generic and reading into an external php script, I don't want to assign a specific variable to a page (too many pages to edit) I want to be able to get the page ID and run with that.. Is this doable or is there a better way? Thanks T. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 16, 2007 Share Posted March 16, 2007 open database and connect... then, query getting the data you want... ie, SELECT * FROM main WHERE page_title='$page_title' then based off of that, you can set your variables for title, content, etc... and echo those out to the page. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Ok so let's say I have this: my SQL table called 'main'. In main I have colums such as: index page name content 1 content 2 ----------------------------------------------------- 1 aaaa Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 sorry.. hit the wrong button and submitted too soon.. here is the full message..: Ok so let's say I have this: my SQL table called 'main'. In main I have colums such as: index page name content 1 content 2 ----------------------------------------------------- 1 aaaa blahblah blahblah 2 bbbb blahblah blahblah 3 cccc blahblah blahblah 4 aaaa blahblah blahblah 5 cccc blahblah blahblah 6 aaaa blahblah blahblah Ok, so I want to retrieve the page name such as for example, 'aaaa' and use the select command to query mySQL and pull all the information on the rows listed aaaa.. I tried your suggestion but i may need more info to implement the code.. right now I have: $query="SELECT * FROM `Entertainment` WHERE 1"; and it works.. only thing is, I need a table for every page.. Thanks again for the help. T. Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 actually sorry, where it is listed 'Entertainment', it should read 'main' Quote Link to comment Share on other sites More sharing options...
interpim Posted March 16, 2007 Share Posted March 16, 2007 <?php $index_id=$_GET['id']; //however your retrieving the data... be it GET or POST //connect to your DB... then query it with the $index_id variable $query="SELECT * FROM main WHERE index='$index_id'"; $result=mysql_query($query); //assign the content of the DB to variables $page_name=mysql_result($result,0,'page_name'); $content_1=mysql_result($result,0,'content_1'); $content_2=mysql_result($result,0,'content_2'); //print the variables and any html formatting you want to the screen. echo "<title>$page_name</title>"; echo "More code etc...."; echo "more stuff... "; ?> Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Here is a piece of code interpim: $query="SELECT * FROM `Entertainment` WHERE Page ='$index_id'"; $result=mysql_query($query); This works good if i declare the variable prior: $index_id = "xbox.php" (where xbox.php = to the name of the current page I'm viewing in a browser and is also a value in mySQL under 'page' column in a table called Entertainment) I tried doing $index_id = $_GET['ID'] as you mentioned but it didn't work.. Aside from me declaring the the $index_id variable should be equal to the page name I am on, how do I make $index_id variable get the page name automatically? (ex. if I'm in IE and I'm viewing a site called www.xxxx.com/xbox.php, i would want $index_id to be assigned the value xbox.php) I'm still stuck.. Thanks for your patience. T. Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted March 16, 2007 Share Posted March 16, 2007 I am still unsure as to what you want to do, what are you wanting to grab from the database? actual page content, or variables such as page title etc? Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 I am trying to grab content relevant to the page I'm viewing.. The page title would determine what content to grab from the database.. T. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 16, 2007 Share Posted March 16, 2007 you would have to set an id variable based on that page... usually this is done with http://domain.com?page=123 you would then use the $_GET['page'] global array to grab the variable page. then you would use that to get the rest of the page from your DB. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 Alright, what you want to do is webfetching basically. You want to read in the contents of an external website and put the title into a string. Here is one way to do it: <?php $file = file_get_contents('http://www.google.com/'); list(,$title) = spliti('</title>', $file); list($title) = spliti('<title>', $file); print $title; //should print "Google" ?> Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Interpim, could the variable not equal the value of the page that is loaded? rather than give each page a ID variable? Quote Link to comment Share on other sites More sharing options...
interpim Posted March 16, 2007 Share Posted March 16, 2007 you could give it a name... as long as it's unique you can search for it in the database via your query Quote Link to comment Share on other sites More sharing options...
tcorbeil Posted March 16, 2007 Author Share Posted March 16, 2007 Interpim, I think i got it.. see the thread Page Title that i started.. Thanks for your help! T. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.