Jump to content

Variables Shared Among Breaks


false74

Recommended Posts

Here is my problem/question:

 

Say I have a class stored in a php file called 'myclass.php' and I have my main index php page 'index.php'.

In that index.php page I include the 'myclass.php' file and create a variable as a new object of the class from myclass.php.

However in the index page there are multiple php breaks <?php ?> <?php ?>, how can I use the variable that is of my class throughout the entire page?

 

myclass.php:

class MyClass {
function MyClass() {
...
}
function foo() { ... }
function bar() { ... }
}

 

index.php

<html>
<head> ... </head>
<body>
<?php
include_once('myclass.php');
$thing = new MyClass();
?>
...
<?php
$thing->foo();
?>
...
<?php
$thing->bar();
?>
</body>
</html>

 

When trying to call $thing outside of the first php block I get an 'undefined variable' error. What is the scope of this variable, how can I have it shared amoungst all the php code blocks?

Link to comment
Share on other sites

showdb.php

<?php
class DBManager {
var $showthumbfile;
var $dblocation;
var $db;
var $mediaroot;
var $showtable;
var $epsiodetable;

function CityBeatDBManager() {
$showthumbrootdir = "thumbs/";
}
function connectDB() {
try {
$db = new PDO('sqlite:' . $dblocation);
} catch( PDOException $e ) { die('Error connecting to show database'); }
}

//QUERIES
function queryAllShowRows() {
return $db->query('SELECT * FROM shows ORDER BY show_id DESC')->fetchall(PDO::FETCH_ASSOC);
}
}
?>

 

index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<LINK REL=StyleSheet HREF="grey.css" TYPE="text/css" MEDIA=screen>
</head>
<body>
<?php
include_once("showdb.php");
$databse = new DBManager();
?>
<ul class="nav">
<li><a id="navbrowse" class="more" href="javascript:void(0);" onclick="toggleBrowse();">Our Programming</a></li>
<li><a id="navabout" href="?page=about">About Us</a></li>
</ul>
</div>
<div id="browse" class="close" tabindex=0 onclick="displayBrowse(false);" onblur="displayBrowse(false);">
<h1>Browse our programs</h1>
<ul class="shownav">
<?php
 function listShows() {
 foreach($database->queryAllShowRows() as $row) {
 echo "
 <li>
	 <a href='?show=" . $row['show_mediadir'] . "' class='show'>"
	 . "<img src='" . $row['show_mediadir'] . "/mainthumb.jpg' />"
	 . "<span>" . $row['show_title'] . "</span>"
	 . "</a>"
 . "</li>";
 }
 }
 listShows();
 ?>
</ul>
</div>
</div>
<div id="wrapper">
<h1>
<?php
echo $databse->$dblocation;

?>
</h1>
</div>
</body>
</html>

 

undefined variable when calling $database inside that foreach loop

Edited by false74
Link to comment
Share on other sites

You can't access a variable like that within the scope of a function. Why are you wrapping that foreach loop in a function in the first place?

 

Even when I remove the function and do this: It still is undefined.

<?php
  foreach($database->queryAllShowRows() as $row) {
   echo "
   <li>
    <a href='?show=" . $row['show_mediadir'] . "' class='show'>"
	 . "<img src='" . $row['show_mediadir'] . "/mainthumb.jpg' />"
	  . "<span>" . $row['show_title'] . "</span>"
    . "</a>"
   . "</li>";
  }
 ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.