Noskiw Posted June 1, 2010 Share Posted June 1, 2010 I would like to know how to create a function in which I can check what page it is on so I can change the "<title>" tag according to the page. It would be complicated and padantic yes... But I have no clue in where to start. This is my code so far <?php class page{ function check_page($page){ } } ?> I'm completely stuck. And yes, I have purposely done this function in a class. Putting it in a class makes it a whole lot simpler for me. Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/ Share on other sites More sharing options...
mrMarcus Posted June 1, 2010 Share Posted June 1, 2010 Well, your method lacks scalability. To achieve this, create a template/database driven site. It's a lot easier than one might think. You create a template/page as you normally would, calling it index.php, let's say. You'll store different information within a database table, ie. meta information (page title, description, keywords), and other content for your page (body), etc. You will call these records like so: http://www.example.com/index.php?id=x, where x equals a record id in your database. Then, the contents from that record in the db table are populated into the designated areas within your template (index.php). Boom. Done. Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066364 Share on other sites More sharing options...
Noskiw Posted June 1, 2010 Author Share Posted June 1, 2010 OK.... This is my code now... <?php function check_page($page){ $sql = "SELECT * FROM page"; $res = mysql_query($sql) or die(mysql_error()); } ?> I DO have the connection variable in there but it isn't relevant. I'm not sure where to go from here... And I do have some rows inserted Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066379 Share on other sites More sharing options...
Noskiw Posted June 1, 2010 Author Share Posted June 1, 2010 <title><?php $obj->check_page($row['title']); ?></title> Quick Edit: This is what I used ^ This I have no idea whether it will work or not... The function is also different now... function check_page(){ $sql = "SELECT * FROM page"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $title = $row['title']; $page = $row['page']; } } Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066407 Share on other sites More sharing options...
mrMarcus Posted June 1, 2010 Share Posted June 1, 2010 Let's assume you have a record in the db with a id of 1, your url would look like this: http://www.example.com/index.php?id=1 index.php <?php if (isset($_GET['id'])) { include 'db_connection.php'; $sql = " select * from `pages` where `id` = %d limit 1 ", mysql_real_escape_string($_GET['id']); if ($result = @mysql_query($sql)) { if (mysql_num_rows($result) == 1) { $res = mysql_fetch_array($result); //display your html below: ?> <html> <head> <title><?php echo $res['title']; ?></title> <meta name="description" content="<?php echo $res['description']; ?>" /> <meta name="keywords" content="<?php echo $res['keywords']; ?>" /> </head> <body> <php echo $res['body']; ?> </body> </html> <?php } else { echo 'Sorry, there is no record with that ID.'; } } else { //line below is for development only; //comment it out or remove it completely when you go to production //and add handling for failed query; trigger_error(mysql_error()); } } else { //no id in url, do something; echo 'quit messing around with my script.'; } ?> It's that easy. Of course this is a very, very basic example, but it's just to illustrate how simple it can be. No need for your/a function, just have a table (pages) in your db with the following fields: id, title, description, keywords, body .. add any more as needed. Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066421 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 Can you possibly make it so it isn't like "?id". Sorry And so it goes in my function... Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066423 Share on other sites More sharing options...
mrMarcus Posted June 2, 2010 Share Posted June 2, 2010 Can you possibly make it so it isn't like "?id". Sorry of course it's possible, but I don't read minds. instead of ?id, what is it you're looking to do. And so it goes in my function... What's with the function? Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066427 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 Can you possibly make it so it isn't like "?id". Sorry of course it's possible, but I don't read minds. instead of ?id, what is it you're looking to do. I'm looking for something like ?p= And so it goes in my function... What's with the function? I want it so it goes into the check_page function because it's in a class which I have related to my index page and all of my other pages. I want to be able to define the page by a function that will thereby define the title from the database. I'll show you 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($row['title']); ?></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> And my class file <?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){ $page = $_GET['page']; $sql = "SELECT * FROM page WHERE id='".$id."'"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $page = $row['page']; $title = $row['title']; } } } ?> This makes it a lot easier than writing all the code on one page and having it look messy. This keeps it organised. (From my perspective at least) Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066430 Share on other sites More sharing options...
mrMarcus Posted June 2, 2010 Share Posted June 2, 2010 there's no difference between my code and yours. Just because yours is in a class, doesn't make it "less messy". I can't actually add on to your code 'cause it's lacking. You're best to take the code i provided as a - well - template, and go from there. To do what you're doing, classes are not necessary, and I've noticed your understanding of functions is not up to par, so you're better off learning simple function handling before jumping into classes. I know that classes might look cleaner, but trust me, they can end up being just as ugly as anything else. Remember, code is only as messy as you it. PS. Next time, show your code up front as that will most likely save everybody a lot of time in most cases. Link to comment https://forums.phpfreaks.com/topic/203573-function-to-check-the-page-that-it-is-on/#findComment-1066446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.