Jump to content

Redroest

Members
  • Posts

    150
  • Joined

  • Last visited

Everything posted by Redroest

  1. Well some frameworks apply the method I just told. I guess it depends on what is important. Smart technologie or performance
  2. Hey people. Im stuck again.I am building a framework based on the Model View Controller principe. My model has an query function like below: <?php //Load all default modules public function getBlockModules() { $sql = 'SELECT * FROM core_modules WHERE blockMode=1'; if(!$ModuleQuery = $this->db->query($sql)) { trigger_error('Error in the modulequery: '.$this->db->error); } else { $rows = array(); while($row = $ModuleQuery->fetch_object()) { $rows[] = $row; } return $rows; } }?> And my controller function to retreive data looks like this: <?php //Load all blocks $modules = $this->Model->getBlockModules(); foreach($modules AS $property) { //Load module into the view object $this->View->setContainer('modules/'.$property->controllerFile, 'modules/'.$property->headerFile, 'themes/Redroest/'.$property->layoutFile, $property->title, $property->positionTag); } }?> As you can see, I need 2 loops in order to keep the data separated from the logic part. Is there a way to prevent multiple loops from happening and still keeping the logics separated?
  3. Well, try to make a navigation structure in your website. For exampleindex.php <?phpinclude './config/config.php';include './libraries/output.php';include './libraries/session.php';if(!$_GET['page'] == 'picture'){ include './libraries/siteDB.php'; $dbConn = new siteDB;}else{ echo 'My image'; echo '<img src="image.png" alt="image" title="my image" />';}?>
  4. Redroest

    MVC model

    Ok here's the problem. I am trying to make a model object for my system. I'm trying to work step by step so things like a registerclass and a controller etc are not yet implemented. First of all the procedural code: <?phperror_reporting(E_ALL);$included = true;//configuration globals//This will be arranged by the registry object later on$host = "localhost";$username = "root";$password = "";$database = "core";$dbprefix = 'core';//Load classfilesinclude('classes/class_database.php');include('classes/class_module.php');include('classes/class_layout.php');include('classes/class_model.php');//Start database connection$db = new database($host, $username, $password, $database);//The container object is an extention of the module object. It loads files and returns its output//In this case it loads the template and returns its html$template = new container('themes/Redroest/layout.php', '', 'layout');//The template html will be stored in the template object to insert containers that load later on$layout = new layout($template->getBody());//The model will load moduleinfo from the database using query's//To switch to another storage type just change this model$model = new dbModule($db);//The model contains a method to load all the requested modules//If no condition is set it will load the home module + the blocks using 'WHERE home=1 OR blockMode=1';$model->condition = 'WHERE moduleID=13 OR blockMode=1';$model->getModules();//Output the final layoytecho $layout->getLayout(); Second is my model. This is where it goes wrong. The model contains a query that loads the modules using a while loop. My goal is to process these modules using my moduleclass without writing them in my model. This should be controlled using the controller....??? I inserted my situation in the code, I hope to get a solution or another approach to solve this problem. <?phpclass publicModel{ private $db; function __construct(database $db) { //Check if the database connection exists if($db->ping()) { $this->db = $db; } } //Load a default condition when no condition is set public function __get($value) { if(!isset($this->condition)) { $this->condition = 'WHERE home=1 OR blockMode=1'; } } //Load all required modules public function getModules() { $sql = 'SELECT * FROM core_modules '.$this->condition; if(!$ModuleQuery = $this->db->query($sql)) { //I know I know, this should be an exeption trigger_error('Error in the modulequery: '.$this->db->error); } else { //$modules = array(); while($ResultModule = $ModuleQuery->fetch_assoc()) { //This is where it goes wrong..... I can push the modules into another array and return it //but this requires me to make another loop in for example my controller object to load them //into containers and to push them into a layout. //Load a new module object $modules[$ResultModule['moduleID']] = new container('Modules/'.$ResultModule['controllerFile'], 'Modules/'.$ResultModule['headerFile'], $ResultModule['title']); //Set the layout for the module $modules[$ResultModule['moduleID']]->setLayout('themes/Redroest/'.$ResultModule['layoutFile']); //get the final output marked up with layout $container = $modules[$ResultModule['moduleID']]->getOutput(); //Insert the container into the layout $layout->setContainer($ResultModule['positionTag'], $container); } } }} ?> Help me on this please. I spend some hours trying to solve this puzzle, but couldn't find any usable examples
  5. Ah ok, thanks. Its important for me to know that. I never learned php from an education. Its just something I learned in my spare time.
  6. Cool, so I finally seem to get it now :)But how would you implement the optional parameter in the constructor then?
  7. Hello, I just made an moduleclass which loads files using filelinks and combines it with a layout. I just want to know, is this a correct OOP class? And do you people have any hints on how to make this better? <?phpclass module{ private $controllerFile; private $headerFile; private $title; public function __construct($controllerFile, $headerFile = '', $title) { if(is_file($controllerFile)) { $this->controllerFile = $controllerFile; } if(is_file($headerFile)) { $this->headerFile = $headerFile; } if(is_string($title)) { $this->title = $title; } } public function getTitle() { return $this->title; } public function getControllerFile() { return $this->controllerFile; } public function getHeaderFile() { return $this->headerFile; } public function getBody() { if(isset($this->controllerFile)) { $output = $this->getContent($this->controllerFile); if($output) { return $output; } } } public function getHeader() { if(isset($this->headerFile)) { return module::getContent($this->headerFile); } else { return ''; } } public function getContent($file) { ob_start(); include($file); $content = ob_get_contents(); ob_end_clean(); return $content; }}class container extends module{ public function setLayout($layoutFile) { if(is_file($layoutFile)) { $this->layout = module::getContent($layoutFile); } } public function getOutput() { $body = module::getBody(); $output = preg_replace('/<!-- blockcontent -->/', $body, $this->layout); $output = preg_replace('/<!-- blocktitle -->/', $this->getTitle(), $this->layout); return $output; }}?>
  8. Aw omg I made one little error. I've been struggling with it for over 2 days now and now suddenly it works just as you say.. Don't really know what went wrong though!Thanks anyway
  9. Well is it possible to group the buffers? like below??? <?php//Start primary bufferob_start('primary');//Start secondary buffer (for example in a function)ob_start('secondary');//End secondary bufferob_end_clear('secondary');//End primary bufferob_end_clear('primary');?> On php.net they say that ob_start() is a function that nests.. like: <?php//Start primary bufferob_start('primary');//Start secondary buffer (for example in a function) //This will output the first bufferob_start('secondary');//End bufferingob_end_clear('secondary');?>
  10. Make sure not to forget security (mysql_real_escape_string etc)
  11. Hey, I am working on a system that includes modules by using outputbuffering. The following function loads files from modules. The output generated by the module will be checked for so called "custom tags" and replaces them with modules. These modules will then be included into a template for example. <?phpfunction file_to_var($file){ if (is_file($file)) { ob_start();//Load the modulefile include($file); $content = ob_get_contents(); ob_end_clean(); return $content; }}?> This code works fine to me, but there is one problem. I just made a module in another system. This system has no outputbuffering and my module works fine in it. Here comes the problem:The module contains its own outputbuffering (I made a photoalbum manager using the JUpload applet which uses ob_start.To simplify: <?phpfunction file_to_var($file){ if (is_file($file)) { ob_start();//Load the modulefile include($file);//Module also contains ob_start()//as simulated on the below line: ob_start(); $content = ob_get_contents(); ob_end_clean(); return $content; }}?> As you can see and guess, the module will be outputted before the rest of the themplate and appears on top of the website.How to prevent this from happening? I don't want to modify every class, module or function that contains outputbuffering.
  12. I know thats the way I used to work, but this does not work also:function callback(xmlhttp){ var output = xmlhttp.responseText; return output;}Is there a way to simulate this ?
  13. Hey people I am trying to make an AJAX object that returns its values to a variable. Every time when I try to alert the output from the server it says undefined or [Object]. When I alert the xmlhttp.responseText within the sendRequest function it works! var XMLHttpFactories = [ function () {return new XMLHttpRequest()}, function () {return new ActiveXObject("Msxml2.XMLHTTP")}, function () {return new ActiveXObject("Msxml3.XMLHTTP")}, function () {return new ActiveXObject("Microsoft.XMLHTTP")}];function GetXmlHttpObject() { var xmlhttp = false; for (var i=0;i<XMLHttpFactories.length;i++) { try { xmlhttp = XMLHttpFactories[i](); } catch (e) { continue; } break; } return xmlhttp;}function sendRequest(url,postData){ //alert('sendRequest'); var xmlhttp = GetXmlHttpObject(); if (!xmlhttp) return; var method = (postData) ? "POST" : "GET"; xmlhttp.open(method,url,true); xmlhttp.setRequestHeader('User-Agent','XMLHTTP/1.0'); //xmlhttp.setRequestHeader('X-AJAX', 'True'); if (postData) xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttp.onreadystatechange = function () { //alert('readyState: ' + xmlhttp.readyState); if (xmlhttp.readyState != 4) return; if (xmlhttp.status != 200 && xmlhttp.status != 304) { alert('HTTP error ' + req.status); return; } this response = xmlhttp; } if (xmlhttp.readyState == 4) return; xmlhttp.send(postData);} Call users values (for example) function getServerUsers(username, password){ var ServerValues = sendrequest('users.php', username='+username+'&password='+password); alert(ServerValues.responseText);} Im not very familier with javascript but I know that it has to be something like this to make it work. The Serverside script works and outputs. I always used a callback function but I think it can be shorter. So how to make this work?
  14. Simply said: real_escape_string is for making a legal query to INPUThtmlspecialchars is used for legal OUTPUT to your html/xhtml
  15. Redroest

    Moduleclass

    That was my original way of thinking synook. But I used this in an function oriented style and it gave the modulebuilder strict giudelines on how to make a module. So when I inserted something like a photoalbum that was used on many other websites it had to be rebuilded before I was able to use it.
  16. Redroest

    Moduleclass

    Ok I will read the above links to see what suits my needs. To answer your question. I am trying to make a modulesystem that includes modules. For example:In my filesystem is a map : (For example) modulesIn the modulesmap there are other maps: members, guestbook, startpage etc etc...Every module consists out of functions: function index(), function procesform(), function showform() etc...In the first place, my class has to work like a buffer: Get module on pageload, check if the module and the called functions exist and execute themI thought this would be the best way to handle such things and I also want it to be OOP so that others can use this class into there own system. But maybe I am not looking in the right way because when I search on "modules, OOP php class" it will have little good results
  17. Redroest

    Moduleclass

    I reworked the class so that values are checked. Is the below class made in a correct OOP way?: <?phpclass modulinator{ //$moduleMap will also be the tablename in the database var $moduleMap = 'modules'; //$connection contains the databaseconnection returned by another class var $connection; function __construct($connection = '') { if(!file_exists($this->moduleMap.'/')) { die('<strong>Filesystem error:</strong> Unable to find the modulemap!'); } if(empty($connection)) { die('<strong>Database error:</strong> Unable to find a database to load the modules!'); } //$this->moduleMap = $moduleMap; $this->connection = $connection; } function select_module() { //ID number from url has to be a numeric value if(isset($_GET['MID']) && is_numeric($_GET['MID'])) { //select chosen module from database $sql = 'SELECT * FROM core_'.$this->moduleMap.' WHERE ModuleID="'.$_GET['MID'].'"'; } //$_GET['MID'] is not set so select default module elseif(empty($_GET['MID'])) { $sql = 'SELECT * FROM core_'.$this->moduleMap.' WHERE Home=1'; $home = true; } //$_GET['MID'] was not empty but there is no modulename with that value else { die('Invalid url, please return to the previous page.'); } //Return error when there is one if(!$Modules = $this->connection->query($sql)) { trigger_error('An error occured while loading a module: '.$this->connection->error); } //No errors and a module is returned elseif($modules = $Modules->fetch_assoc()) { include_once($this->moduleMap.'/'.$modules['ModuleMap'].'/index.php'); self::load_tasks($this->moduleMap, $modules['ModuleMap']); } else { if(isset($home)) { die('<strong>Filesystem error:</strong> Unable to load the homemodule!'); } } } //every module contains tasks in the form of functions. //So if there is a function in the members/index.php called register() //I simply have to make an url like: modules.php?MID=2&task=register function load_tasks() { //Check if task is set and if there is a function set with that name if(isset($_GET['task']) && function_exists($_GET['task'])) { //execute function, although I think that this is buggy since it also cannot contain parameters //(maybe some help here) call_user_func($_GET['task']); } else { //Any non-existing values/functions will call index() (set as default function in every module) index(); } }}?>//Call class:$modulinator = new modulinator($mysqli);$modulinator->select_module();
  18. Hey people. I like to post reply's here to help others and in the same time I try to pick up many tips and hints that the other people post here.Full name: Chris VisserBirth date(dd/mm/yyyy): 4 july 1988Gender: maleZodiacal sign: CancerCountry of residance: NetherlandsCity of residance: AmsterdamSmoking/Drinking/Drugs: no/yes/noFavourite Music: Trance, techno, blues, irish, celtic, rockInterest: Soccer, party's, websitesPets: dog, cat, aquariumAdditional comments: I've never followed any education or course on any weblanguage, but always relied on online tutorials. I don't like copying blindly so I try to understand everything I use. In return I post my own scripts to benefit others.
  19. Redroest

    White Space

    It still is strange because it is common to change an youtube embedment this way. There are a lot of articles on the web that explain it this exact same way.
  20. First of all, put it between code tags and second, it is not complete. I also think that the purpose of this forum is asking questions or anwer them, not to post random scripts. There are many other websites that have a script library.
  21. Redroest

    White Space

    The code looks how it is supposed to be. Can you send the rest of your html code? It looks like there is a box, or table with a fixed height/width
  22. I am using the mysqli version of what justsomeguy tells. The below example explains why this is a great tool:database.php <?php//Database connection$db = array ( 'host' => $db_host, 'user' => $db_user, 'pass' => $db_pass, 'dbname' => $db_name);//Create the connection as object$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['dbname']);if(mysqli_connect_errno()){ trigger_error('Fout bij verbinding: '.$mysqli->error);}?> part of my main.php file that handles a part of the configuration <?php//Database connection vars$db_host = 'xxxx';$db_user = 'xxxx';$db_pass = 'xxxx';$db_name = 'xxxx';//Include the database connectioninclude_once('database.php');//Select the defaultsettings from the configtablefunction configuration(){ $sql = "SELECT * FROM table_xxxxx WHERE xxxxID=xxx"; if(!$Config = $GLOBAL['mysqli']->query($sql)) { trigger_error('Fout in query: '.$GLOBAL['mysqli']->error); } else { if($config = $Config->fetch_assoc()) { $theme = $config['DefaultTheme']; $language = $config['DefaultLanguage']; } }}?> As you can see, the class takes care of the connection while I don't have to worry about any extra coding to include the connection to my functions. The way I explaned in my first example just uses pure functions and thats also a way to handle stuff and still keep things flexible. In your way (using a function in a function) it cannot be possible to add a new parameter to your db_connection(); like db_connection($par); without changing all the function calls in the rest of your files. Its just a way to keep things flexible and it will safe time in the long run when you have to update/change things.
  23. Redroest

    Php And Mysql

    You ar using the capital C in include('Config.php') I dont know if your file is also with a capital C? files like these are Case sensitieve.Also the below code is wrong. You are actually outputting html before php processes. Also let php output everything. It is not required but it creates the effect that you output html using php. This is recommended because html is only a markup language. <!-- The below html lines have to be echo'd using php --><table><tr><td><?php//This has to be set before outputrequire "Config.php";echo "php runs first!";?><br></br><?php//This has to be set below the include('Config.php');$pwdb = mysql_connect($dbhost,$dbuser,$dbpass) or die("Unable to connect to $db");mysql_select_db($db) or die("Unable to select $db");$result = mysql_query("select * from site_users");while($row = mysql_fetch_array($result)) {foreach ($row as $value) {}}?><?php echo "$value"; ?></td></tr></table>
  24. I recommand making the db_connection a object:$connection = db_connection(); function queryDB($connection){} or function queryDB(){ global $connection;}
  25. Redroest

    Php.ini

    Can you post the script here? I gues it has something to do with the syntax?
×
×
  • Create New...