cbastian Posted May 14, 2008 Share Posted May 14, 2008 I'm new to PHP, so forgive me if this is a stupid question. I've got a bit of code : <?php require("connect.php"); $sql = "SELECT * FROM table1 WHERE group = 7"; $res = mysqli_query($mysqli, $sql); if ($res) { while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) { $title = $newArray['evnt_title']; } } else { printf("could not retrieve records: %s\n", mysqli_error($mysqli)); } mysqli_free_result($res); mysqli_close($mysqli); ?> It works fine on a page I made to create PDF files, from a database. But when I tried to use it to grab data from a different database, to list events on another page, it keeps returning: "PHP Notice: Undefined variable: title in F:\..... on line 202" This script is in a different file from the page, and the page is requiring it. The page that I'm trying to display it on, is using an array to create different pages. like this: $tarr = array("Music"=>"?mdc=mus"); ............ Private function pageswitch($mod){ if($mod=="mus"){ $ts = $this->dualMN(1); return $ts; } ............. private function dualMN($t){ $ts1 = array( 1=>array( "title"=>"music", "cont"=>"<p class=\"arttitle\">Music</p><br/> <ul><li> <div class=\"arttitle\">Scheduled events<br/> <p class=\"artbody\">title=".$title."</p><br/> </div></li> </ul> ") ); } the page displays fine, but when I put this in, instead of showing the data from the database, it just shows "title" and places the above error in the PHPerrors file. I can't figure out why it is not recognizing the variables, and showing their data... Hope this makes sense. any help anyone can give me, would be greatly appreciated. Thanks, Charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2008 Share Posted May 14, 2008 Variables inside a function are local to that function. You could create a class var called title private $title; function setTitle ($t) {$this->title = $t;} Then use $this->title in the dualMN function. Quote Link to comment Share on other sites More sharing options...
cbastian Posted May 14, 2008 Author Share Posted May 14, 2008 OK, I tried putting this in the main page file : private $title; function setTitle ($t) {$this->title = $t;} and then this in the dualMN Then use $this->title in the dualMN function. like this: <p class=\"artbody\">title=".$this->title."<br/>........ But it shut down the whole page. and gave the error: "PHP Parse error: Syntax error, unexpected T_PRIVATE in f:\..... on line 11" Am I missing something? Or am I putting it in there wrong. Sorry, like I said I'm new to this, kinda got thrown into it... thanks for your help. -Charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2008 Share Posted May 14, 2008 Post the first 11 lines. A parse error usually denotes a mistake somewhere before the line that it eventually reports. Quote Link to comment Share on other sites More sharing options...
cbastian Posted May 14, 2008 Author Share Posted May 14, 2008 Post the first 11 lines. A parse error usually denotes a mistake somewhere before the line that it eventually reports. <?php require("../html_temp_sets/pageset.php"); require("../html_temp_sets/dataset.php"); require("./events.php"); private $title; function setTitle ($t){$this->title = $t;} class FINEART{ public function FAMain(){ and the line trying to display it is: "....... <li> <div class=\"arttitle\"><strong>Scheduled Events</strong><br/> <p class=\"artbody\">title=".$this->title."<br/> </p> </div> </li> ........"; Not sure what coulde be wrong with it. If I remove those new lines it works fine (without displaying the database info)... Thanks A lot, for your help... --Charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2008 Share Posted May 14, 2008 these 2 lines private $title; function setTitle ($t) {$this->title = $t;} need to be inside the class definition Quote Link to comment Share on other sites More sharing options...
cbastian Posted May 14, 2008 Author Share Posted May 14, 2008 OK, that brought it back up, and eliminated the undefined variable error. But, it still isn't showing anything. And it's not giving any errors... Any ideas why? Think I should just put the script to grab data from the database in the same class? Instead of having it in another file and calling it? Thanks for your help... --charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2008 Share Posted May 14, 2008 You now need to set a value for that class variable by calling the setTitle() function, passing your $title as the argument. Quote Link to comment Share on other sites More sharing options...
cbastian Posted May 14, 2008 Author Share Posted May 14, 2008 OK, this is probably a stupid question, but how would I do that? is it just like this: setTitle($title); inside the class somewhere? I tried just doing that, and it killed the page again, giving the unexpected T_String error. Or do I need to create a new function and put it in there? Thanks a lot for your help! --charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2008 Share Posted May 14, 2008 Somewhere after getting $title from the database there should be something like $xxxxx = new FINEART; followed later by $xxxxx->pageswitch($something); Between those lines you need $xxxxx->setTitle($title); Quote Link to comment Share on other sites More sharing options...
cbastian Posted May 14, 2008 Author Share Posted May 14, 2008 OK, I'm feeling pretty stupid right now... There is nothing in my files that says $xxxxx = new FINEART; and the only place I can find pageswitch is in this line: $td .= pageSet::tdSetsimple($this->pageswitch($mod),"valign='top' class='artbody'"); and this line (that calls the arrays for what's on each page): private function pageswitch($mod){ ????? I'm working with someone else's code, who had my job before me, and left things about half done... I might just have to stop trying to edit and just rewrite the page from scratch... ????? Thanks! -Charles... Quote Link to comment Share on other sites More sharing options...
Barand Posted May 15, 2008 Share Posted May 15, 2008 this method should work <?php class FINEART { private static $title; public function setTitle($t) { FINEART::$title = $t; } function printTitle() { echo FINEART::$title; } } $title = 'abc'; // value from database FINEART::setTitle($title); // set value in the class FINEART::printTitle(); // call you function which uses $title ?> 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.