Page 1 of 1

OOP in php

Posted: Fri Dec 16, 2005 4:14 am
by perkin_a
patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have problem with setting content in the following class:

Code: Select all

<?php
class Page
{
var $content;
var $title;

function SetContent($newcontent)
{
$this->content=$newcontent;
}

function SetTitle($newtitle)
{
$this->title=$newtitle;
}

function DisplayTitle()
{
echo "<title> $this->title </title>";
}

function DisplayMeta()
{
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">";
}
function Display()
{
echo "<html>\n<head>";
$this->DisplayTitle();
$this->DisplayMeta();
echo "</head>\n<body>\n";
echo $this->content;
echo "</body>\n</html>";
}
}
?>
I try to do like this:

Code: Select all

<?php
session_start();

require ("page.inc");
$log = new Page();
$log -> SetTitle("Login and password verification");
$log -> SetContent('
<?
echo "lea";
?>
');
$log -> Display();
?>
But it does not show me "lea" on the screen...
When I browse the source code i see:

<html>
<head><title> Login and password verification </title><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head>
<body>

<?
echo "lea";
?>
</body>
</html>

Please help!

Note: I need to set a php content there. "lea" is just a simple example.


patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Dec 16, 2005 4:26 am
by Skittlewidth
You want to assign your text to a variable instead of echoing it out:

Code: Select all

$contents = "Hello world";
log -> SetContent($contents);
Otherwise you are not assigning the text to the object.
It's the same as you have done for the setTitle() function.

Posted: Fri Dec 16, 2005 4:38 am
by perkin_a
patrikG | I'm not doing your job for you, so please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


As a matter of fact I try to insert the following php content:

Code: Select all

<?php
	session_start();

	require ("page.inc");
	$log = new Page();
	$log -> SetTitle("Login and password verification");
	$log -> SetContent('
	 <?php
		
			$db = mysql_pconnect("127.0.0.1 :3306","root","butor");
			if (!$db) 
			{
				echo "Error";
				exit;
			}
			
			mysql_select_db("Kursi");
			$query = "select login, password from logs where login=\"$login\" and password=\"$password\"";
			$result = mysql_query($query);
		
			$num_results = mysql_num_rows($result);
			if ($num_results==0) echo "Wrong login or password <a href=\"index.php\">Try again!</a>";
			else 
			{
				$valid_user=$login;
				session_register("valid_user");
				require ("profile.php");
			}
		  
	 ?>
	');
	$log -> Display();

?>
But when I set an HTML content everything is OK!
Please help to solve this problem!


patrikG | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Dec 16, 2005 4:55 am
by foobar
Can you please post that in this forum's [ php ] tags.

Posted: Fri Dec 16, 2005 4:56 am
by Skittlewidth
Why are you breaking in and out of php with "<?" "?>" tags when the whole page is in PHP?


I don't know anything about the class you are using, but is it possible to assign a page to setContents(), like an include() perhaps?

So all that code for the login for could be in login.php and then you could say:

Code: Select all

$log->setContent(login.php);

Posted: Fri Dec 16, 2005 4:59 am
by patrikG
Move to PHP Code

Posted: Fri Dec 16, 2005 5:16 am
by perkin_a
The SetContent function sets the attribute "content". Then in Display function this content is echoed. When I do it with simple HTML code - it works, but php content is a real problem. The question is: How to set php content in OOP php programming?

Posted: Fri Dec 16, 2005 5:25 am
by Skittlewidth
I think perhaps you need to take a different approach. The page contents should only be the result of your php code after it has been evaluated.

So you could have the html login form in setContent() on one page but all the processing logic needs to be evaluated and then the appropriate response sent to setContent() eg setContent("You have logged in!") after the user has submitted the form.

It's just as suggestion.