Jump to content

Cobby

Members
  • Posts

    59
  • Joined

  • Last visited

    Never

Everything posted by Cobby

  1. Whats with the open curly bracket straight after session_start()?
  2. Its a term used to shorten search results, etc. Best google a popular term and check down the bottom of the page (Prev 1 2 3 4 Next). Imagine if every google search result was on the one page. As for the OP, I would suggest die()-ing your major variables so see what their actually values are, or maybe var_dump(). And then doing this inside your condition statements to see if they're actually executing.
  3. Just making a very simple MVC-type system for an IPT assignment. A snippet from the bootstrap goes like: <?php ob_start(); @include root . '/header.php'; include root . $request; @include root . '/footer.php'; ob_flush(); ?> I've got output buffering setup so the $request file can still send headers. In header.php, I have things that need to be set in the header (i.e <title></title>, meta information, javascript, css, etc) and I want it so that $request can set stuff to the header.php. I don't really see how, but would it be any easier if I OOP-erised things? I would rather not because the schools servers are running PHP4 (pretty messed up I know) and I doesn't have very good class support. Cheers, Cobby
  4. That would involve the Products class being static. Instead consider this: <?php require_once 'Products.php'; class ShoppingCart { protected $_products; public function __construct(){ $this->_products = new Products(); } public function getCategoryProducts($cid){ $products; // load Product IDs from given Category ID foreach($products as $pid){ $category[] = $this->_products->getProduct($pid); } return $category; } } ?> Depending on how you have set up the products class, you might consider simply extending, but the above would be my preffered method.
  5. I like your code, its very clean, commented and strict. Have you considered using a static registry class? With regards to a template engine, no matter what you do, you can't really get anything better than smarty. But if you just want to create a very basic template engine that can really only do outputting variables, then making you own would be better to save in file size, efficiency, etc. If you are going to create a separate config file, it could be worth while creating it in an XML format and creating a class to handle it. This way you can update settings from within PHP where as you can't do that as easily with a .ini type file.
  6. "A single connection object can then be used throughout your code, as opposed to creating many additional connection objects which will eat up your resources. That would be the reason I guess." That means if, say you had two classes that both required a database connection you could do something like: <?php class foo { protected $_db; public function __construct(){ $this->_db = DatabaseConnection::getInstance(); } } class bar { protected $_db; public function __construct(){ $this->_db = DatabaseConnection::getInstance(); } } ?> Then instead of creating two connection to the database, it would be like: <?php $db = DatabaseConnection::getInstace()->connect($dbSettings); new foo(); new bar(); ?> Now both classes foo and bar have access to the database, but you only connected once.
  7. It could also be a good idea to define method scope as well. Do you understand the difference between public, protected and private? And I assume that the str_ at the start is reference to the variable being a string? I don't think that sort of practice is necessary, PHP is a loosely type language so unofficially specifying the variable type isn't need. Same with the type casting when you set the class variables, its something that you can do, but isn't necessary. I haven't tried it, but if you pass, say an integer as the database username, PHP would convert it to a string anyway.
  8. I figured it out, Smarty supports resource plugins, so I created a text one. Now I can just go: <?php $smarty->assign('aVar', 'World'); $tpl = 'Hello {$aVar}!'; $smarty->display("text:$tpl"); // Hello World! ?>
  9. I think you might be creating a non-terminating loop because you call the function inside the function. <?php function foo($string){ foo($string); die('Will this execute?'); } ?>
  10. There isn't really any limitation on how many class you can have defined. Are the images stored in the database? Generally, you would have a class to handle the DB connection and provide a few functions to save a bit of SQL. Then you would have a gallery class which you would pass the instance of the DB class to the constructor and assign it to a property. Then you would have functions in your gallery class that do that actually work, like getAlbums(), getPhoto($photo_id), etc.
  11. Hi, I have templates stored in a database that I want to render. At the moment I just generate a temporary file and write the contents of the database query to it, the display that temporary file. <?php $result = $db->query($sql); file_put_contents('temp.tpl', $result['content']); $smarty->display('temp.tpl'); ?> But I don't like doing this as it just seems so unecessary, instead I would rather be like: <?php $result = $db->query($sql); $smarty->render($result['content']); ?> Anyway to do that? Cheers, Cobby
  12. So is $row a property inside the class? <?php class foo { public function echo($var){ echo $this->{$var}; } } $foo = new foo(); $foo->echo('testVar'); // will display $foo->testVar ?> Cobby
  13. You could use the __set and __get magic methods, or create a stdClass and assign it to one variable. <?php class foo { private $_data; public function __contruct(){ // TODO: class contructor function } public function __get($key){ return $this->_data[$key]; } public function __set($key, $value){ $this->_data[$key] = $value; } } ?> You can then access data from the class like this: <?php $class = new foo(); $class->test = 'Test Variable'; // performs $this->_data['test'] = 'Test Variable'; echo $class->test; // displays 'Test Variable' ?> stdClass is basically just an alias to an array: <?php $class = new stdClass(); $class->test = 'Test Variable'; // performs $class['test'] = 'Test Variable'; echo $class->test; // displays 'Test Variable' ?> Of course with stdClass you can't define functions, as its just a special type of array. .Cobby
  14. A registry class isn't too difficult, look into the __set() and __get() magic methods and also have a look at implementing ArrayAccess. Another thing you might want to look is a class that handles global variables, namely the session.
  15. Since your so focused on making your own, which is good fun , I highly recommend Zend Framework. Zend Framework is quite easy, but its just there is soo much to learn. I would allow two weeks to learn it thoroughly. With Zend you will find several video tutorials on creating bootstrap, etc (just google) and make sure you look deeply at: Zend_Controller Modular Directory Structure Zend_Loader Zend_Zb Zend_Config / Zend_Log Zend_Auth / Zend_Layout Zend_View / Zend_Layout Zend_Form Integrating Smarty For modular directory structure, see here: http://framework.zend.com/manual/en/zend.controller.modular.html#zend.controller.modular.directories That's the bulk of the essential modules when developing a CMS. More info at http://framework.zend.com/manual/en/ In any case, actual page content should be stored in a database. Create a regex route that routes all requests ending in .html to your content controller. Then your content controller would retrieve the specified page (from the request) from the database, then render it in the content section of your template/layout. Editing would be a similar process, except a different url to route (like /admin/page/:pid) and have a little editor (either TinyMCE or FCKEditor) and when the form is submitted just update the record in the DB. Again, learn Zend Framework, especially if you will be creating other applications down the track. Cobby.
  16. Ha thanks for that, didn't even think. But I got a better question, is there a program that can add the license to that top of every PHP file in a given directory. I can set up my IDE to add it as part of the new PHP file template, but if I ever need to change anything, I then have to go through every file and change it (which will be several hundred by the time the project is finished.). EDIT: Even better, I'll just make a PHP script
  17. Hi all, Sort of the first time using phpDocumentor and I have a little problem. As required by the GNU GPL, you need that little license info at the top "This file is part of Foobar...". But phpDocumentor documents this and it will be at the top of EVERY file in my project. How can I get it to ignore an entire DocBlock or just a section of one? Cheers, Cobby
  18. I would store site information, like DB connection, in an XML file. Then just write a little class that use SimpleXML, and create a static function which returns the config info as an array or stdClass. Then just call that static method the __construct of all necessary classes.
  19. If you want to make your own, I recommend learning Zend Framework. It takes a little getting used but its well worth the slog and once you get the basis of your application laid out the actually application is very easy to develop. Go to: http://framework.zend.com. It fully OOP and uses MVC. If you want to use an existing one, I would recommend CMS Made Simple over other CMSs. I have had experience with drupal and it just doesn't seem as friendly. If none of the above options above take your fancy, I would also recommend this read: http://www.phpit.net/article/simple-mvc-php5/ Cheers, Cobby
  20. Ah, I see what is going on now, its trying to return two identical indexes, and because admini is the last row, its value are set into the array. It was something silly, lol :-\
  21. Hi, I have a user table in the DB, for example: +---------+----------------+-------------------+ | uid | username | firstname | |----------+----------------+------------------+ | 1 | cobby | Andrew | | 2 | admini | test-account | +---------+-----------------+------------------+ If, in phpMyAdmin, I query: SELECT `username` FROM `users`; It will return results correctly (cobby and admini, respectively). But if I make a simple PHP script, such as: <?php mysql_connect('localhost', 'cobby', 'dbpass'); mysql_select_db('testdb'); $query = mysql_query('SELECT `username` FROM `users`'); print_r(mysql_fetch_assoc($query)); ?> It only returns: Array ( [username] => admini ) Why does it only return the second result? I have a feeling I'm doing something really silly, but I looked over this test script and its got me stumped. Cheers, Cobby
  22. You hit the jack pot mate, just removed error_reporting() and it all works fine now. Thanks, Cobby
  23. Theres a lot of code here: config.php <?php // set database information $settings['server'] = 'localhost'; $settings['username'] = 'cobby'; $settings['password'] = '*******'; // intentionally left blank for the purpose of this post $settings['db'] = 'framework'; $settings['tblpre'] = 'spf_'; $settings['urlformat'] = 'index.php?file='; if(!defined('tbl_prefix')){ define('tbl_prefix', $settings['tblpre']); } if(!defined('urlformat')){ define('urlformat', $settings['urlformat']); } $settings['adminemail'] = 'admin@localhost'; ?> include.php <?php // Set error reporting error_reporting(E_ALL); // get url function function url(){ // get server protocol $sp = $_SERVER['SERVER_PROTOCOL']; $sp = explode ('/', $sp); $protocol = strtolower($sp[0]); // add protocol $url = $protocol."://".$_SERVER['SERVER_NAME']; // get the script path $path = $_SERVER['PHP_SELF']; // separate path by / $path = explode('/', $path); $final = ''; // join parts, start at 1 to remove first blank and end 2 early to skip includes and filename name for($i=1; $i<count($path)-1; $i++){ $final .= "/".$path[$i]; } // add trailing / $final .= "/"; // join parts $ret = $url.$final; return $ret; } // Set Constants if(!defined('DIRSEP')){ define('DIRSEP', DIRECTORY_SEPARATOR); } $site_path = realpath(dirname(__FILE__) . DIRSEP . '..' . DIRSEP) . DIRSEP; if(!defined('site_path')){ define('site_path', $site_path); } if(!defined('site_url')){ define('site_url', url()); } include site_path.DIRSEP.'classes'.DIRSEP.'reg.class.php'; $reg = new reg; $scandir = scandir(site_path."classes\\"); foreach($scandir as $val){ if(preg_match("+.class.php+", $val)){ $val = substr($val, 0 ,-10); if(($val != 'Smarty_Compiler') && ($val != 'Config_File') && ($val != 'reg')){ include_once site_path."classes\\".$val.".class.php"; $reg[$val] = new $val($reg); } } } include site_path.DIRSEP.'includes'.DIRSEP.'config.php'; $reg['smarty']->assign('settings', $settings); $reg['smarty']->assign('siteurl', site_url); ?> index.php <?php // include includes.php include 'includes/include.php'; // connect to database $reg['db']->dbConnect($settings); // run request handler $reg['handler']->page(); // close database connection $reg['db']->dbClose(); ?>
  24. The session is start otherwise it would be throwing a different error. I did print_r($_SESSION) and I get Array() on the screen.
  25. I suggest you read this MVC tutorial: HTML: http://www.phpit.net/article/simple-mvc-php5/ PDF: http://www.phpit.net/article/simple-mvc-php5/?pdf=yes
×
×
  • 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.