Noskiw Posted June 2, 2010 Share Posted June 2, 2010 I'm having trouble with my code which is supposed to define the <title> of the page. This is my class code in which the $row variable is defined. <?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){ $sql = "SELECT * FROM page WHERE id='".$id."'"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $id = $row['id']; $page = $row['page']; $title = $row['title']; } } } ?> And 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(1); ?></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> This is getting increasingly frustrating. If anyone could help me, I would be very appreciative. Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/ Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 You're not returning anything from your function. Since I really don't know classes that well, I could be mistaken. Also, since there's probably only one record in your database for each id, you don't need (and shouldn't use) the while loop. Try this: <?php function check_page($id){ $sql = "SELECT * FROM page WHERE id='".$id."'"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res)); return(array($row['title'],$row['page'],$row['id'])); } ?> and in the index.php <title><?php list($title,$page,$id) = $obj->check_page(1); echo $title; ?></title> Note: I could be way off base here. Ken Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066669 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 That has worked perfectly Thank you Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066670 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 But on a quick note... How would I get it so it detects the ID of the page and sorts it without having to put "<title><?php list($title,$page,$id) = $obj->check_page(1); echo $title; ?></title>" all the time... Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066678 Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 How is the page invoked? Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066681 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 Explain invoked? Sorry, I'm only 15... Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066685 Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 What's the URL look like when you want to get to the page. Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066692 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 http://localhost/projects/p_web/index.php I'd assume that i'd have to do something such as "?page_id=1"? Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066694 Share on other sites More sharing options...
Noskiw Posted June 2, 2010 Author Share Posted June 2, 2010 BUMP! :L Link to comment https://forums.phpfreaks.com/topic/203638-how-to-get-a-row-from-a-table-and-set-it-as-the/#findComment-1066758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.