Jump to content

PHP OOP Help


PdVortex

Recommended Posts

Thats wicked and perfectly explained thanks very much appreciate it a lot.

 

One last question for you, is this part necessary?

 

    $cols = implode( ', ', array( FORUM_TOPIC, TID ) );

 

could i not just add the constants directly into the sql line or just the table colum names i need?

something like this

    $sql = "
        SELECT id, topic
        FROM forum_topic 
        WHERE forum_topic.user_id='{$this->userId}' 
        ORDER BY datetime DESC LIMIT 5
    ";

 

Thanks again.

Link to comment
Share on other sites

Both are equivalent ways of specifying the columns.

 

FYI, when I first started PHP I used constants for my columns and table names.  IMO it's more effort than it's worth and I haven't done that in a loooooooooooong time.

 

I do it like this:

    $sql = "
        SELECT id, topic
        FROM forum_topic 
        WHERE forum_topic.user_id='{$this->userId}' 
        ORDER BY datetime DESC LIMIT 5
    ";

 

I typically write my queries in all lower case as well: select id, topic from forum_topic ...

 

And you should properly enclose your table and column names in the delimiter expected by the database engine:  select `id`, `topic` from `forum_topic` ...

Link to comment
Share on other sites

Where are you getting $this->userId ?

That looks to be  undefined. (by your code)..

 

If you want to do this, you have to convert it to an asscotive array, the loop over it in the html.

 

So

 

function forumTopics() {
   $sql = "SELECT * FROM forum_topic WHERE forum_topic.user_id='".$this->userId."' ORDER BY datetime ASC LIMIT 5";
   $tmp = mysql_query($sql);

   while ($line = mysql_fetch_assoc($tmp)){

        $result[] = $line;

   }
   
// Check if its an array (meaning there is a result)
   if(is_array($result)){

    return $result;    

   } else {
   
     return false;

   }


$Topics = $user->forumTopics($user);

// if returned false, then this will trigger.
if(!$topics){

    // nothing found error
}


// Then loop it

 

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.