OOP in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perkin_a
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2005 4:10 am

OOP in php

Post 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]
Last edited by perkin_a on Fri Dec 16, 2005 4:57 am, edited 1 time in total.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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.
perkin_a
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2005 4:10 am

Post 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]
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Can you please post that in this forum's [ php ] tags.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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);
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Move to PHP Code
perkin_a
Forum Newbie
Posts: 3
Joined: Fri Dec 16, 2005 4:10 am

Post 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?
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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.
Post Reply