TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-08-2007, 02:53 PM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default Newbi OOP Question

I am just starting to learn OOP, this very moment, and I have a question. I got a few things done (more than expected), but I am having some problems.

I am trying to just get a post from a database based on the ID.

PHP Code:
class post {
    var 
$id;
    
    function 
set_post($id) {  
        
$this->id $id
        
        
$post_query mysql_query("SELECT * FROM `v7_posts` WHERE id = '$this->id'");
        if(
mysql_num_rows($post_query) < 1) {
            return 
false;
        } 
        
        
$this->title mysql_result($post_query,'0',"title");
        
$this->post mysql_result($post_query,'0',"post");
        return 
true
    }

    function 
get_post() {
        return 
$this->title;
        return 
$this->post;
    }


Thats my class, and I'm using this to call it.

PHP Code:
$post = new post;
$post->set_post("1");
echo 
$post->get_post(); 
My problem is, that it is only returning the title, not the actual content. Am I doing this completely wrong?

Thanks!
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 12-08-2007, 02:58 PM   #2 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

maybe instead of echoing the $post->get_post();

You can do that within the class itself.

PHP Code:
function get_post() 
{
  echo 
$this->title."</br>";
  echo 
$this->post;

Because it might be overloading the return so its only returning the title.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
Gurnk (12-08-2007)
Old 12-08-2007, 03:00 PM   #3 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

Wouldn't I still have to echo the get_post function in order for anything to show up? And Isn't return doing the same thing as echoing?

EDIT: Got it :) I still echoed the get_post, but in the actual function, I used echo instead of return. :)

EDIT:
Is there an easier way to define the stuff, instead of this?

PHP Code:
$this->title mysql_result($post_query,'0',"title");
     
$this->post mysql_result($post_query,'0',"post"); 
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 12-08-2007, 03:23 PM   #4 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Just for the record, when you return, you return.

The function returns the value and ends when you return, no if ands or buts! Therefor you can only return once in a function.

In PHP 5, I believe "var" is being deprecated, use public and private to declare variables:

PHP Code:
class MyClass
{
    public 
$foo 'bar';
    private 
$bar 'foo';

Secondly, take caution with echo and print, try not to use them like you did, one after another.. it's more logical to use it like such:

PHP Code:
function get_post()
{
    echo 
$var1 $var 2;
    
// personal I wouldn't use a function to return something that's public

Thirdly, you don't need a function to return variables (that is, if they're not private)

PHP Code:
    print $object->var1
Edit

Yes, there is an easier way, use arrays:

PHP Code:
class MyClass
{
    public 
$data = array( );

    public function 
data_to_array$result )
    {
        while( 
$data mysql_fetch_array$result ) )
        {
            
$this->data[] = $data;
        }
    }
}

print_r$object->data ); // look at how the array is structured

// when you figure the structure out, you can then access the array like:
print $object->data['foo']; 
Jay is offline  
Reply With Quote
The Following User Says Thank You to Jay For This Useful Post:
Gurnk (12-08-2007)
Old 12-08-2007, 03:30 PM   #5 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

Wow! Thanks for all that useful information!

I knew about public and private, but I get an error when I try to use them. My server says its on PHP 5, but I guess its not... I'll have to ask my host about upgrading.

Thanks again.
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 12-08-2007, 03:35 PM   #6 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Your host probably runs PHP4 and PHP5 at the same time, yes it's possible, but sometimes annoying when working with PHP5, as PHP4 is dominant over the .php extension.

Try renaming your file to <name>.php5, and see if you still get the errors, if you don't, make a file over the FTP called ".htaccess", and add the following content into it:

Code:
	#	Modify the PHP Handler
	AddType x-mapp-php5 .php
Now you can work with PHP5 and the .php extension at the same time
Jay is offline  
Reply With Quote
The Following User Says Thank You to Jay For This Useful Post:
Gurnk (12-08-2007)
Old 12-08-2007, 04:02 PM   #7 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

Thanks Jay! I got some code similar to that from my host, but it didn't work. But yours fixed the problem, thanks a lot. :)
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:06 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design