Jump to content

Classes Question & Controllers (Should be easy)


noidtluom

Recommended Posts

I have recently learnt OOP and Classes.

 

Time for my first question. I have a simple database fetch:

 

    $result = mysql_query($query);
    if ( mysql_num_rows($result) > 0 ) {
  
      // now let's use the while function to repeat
      while ( $row = mysql_fetch_object($result) ) {

        echo $row->id .' is '. $row->username .'<br />';
  	
      }
  
    } else {
  
      echo $error;
  
    }

 

As part of OOP, (well, it's more of a classes question that's why I didn't put it in the OOP child board) I decided to try and put this into a class where I can call it anytime. However, I'm not too sure how to do this.

 

This was my try (obviously this function is in a class {} ):

 

	function Query($query,$structure,$error)
{

    $result = mysql_query($query);
    if ( mysql_num_rows($result) > 0 ) {
  
      // now let's use the while function to repeat
      while ( $row = mysql_fetch_object($result) ) {

        echo $row->id .' is '. $row->username .'<br />';
  	
      }
  
    } else {
  
      echo $error;
  
    }

}

 

I was thinking of being able to 1) specify the query (hence $query in the function), 2) specify the structure of the output and 3) Easily customize error message. I don't always want it to say "No Rows Found" or something like that. For example in the inbox I would want it to say "No messages" if there were no rows, and in the forum profile checking the number of submissions I would want it to say something like "No submissions." You get the idea.

 

I'm having a problem with 2). I don't know how to specify the structure. Or is there a better way to do it?

 

OK, for my second question, I've heard about controllers recently and I would like to learn how it works. Can anybody link me to a guide? I would hopefully like one without a .htaccess (unless you can provide a .htaccess with a script which you want to allow people to download). I'm not too sure how to layout my files.

 

Thanks in advance, and awaiting your helpful replies!

Link to comment
Share on other sites

To give a little bit more information on the second question:

 

If possible I would really like a site or something that can easily explain controllers (hopefully with examples included) for PHP (specific) for complete beginners. I like those ;)

 

Currently what I have been doing is having an index.php which included a template, and where the content should be I filled it full of <? if ($page=="home") { include ""; } elseif ... etc ?> And to prettify the URLs I would use mod_rewrite so that instead of site.com/index.php?page=mypage (for example) it'll be something like site.com/mypage. However, after hearing about controllers, I feel maybe this isn't the best method.

 

If there isn't a website/tutorial which can explain it for beginners, I would appreciate some example files for the most basic controller system.

 

Thanks in advance.

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.