Jump to content

Chris92

Members
  • Posts

    206
  • Joined

  • Last visited

Everything posted by Chris92

  1. Well then I might as well just right it out in an if rather than a try statement and not bother catch it. Can you only catch ErrorExceptions in PHP?
  2. Is there any way to catch warnings without defining a warning handler? Example: try { $settings = parse_ini_file( 'main.ini' ); } catch( ErrorException $e ) { throw new ErrorException('No main configuration file found for webiste', '0003'); } Only ends up throwing a warning if the file doesn't exist. I want to catch it and throw an error.
  3. read the PHP documentation, he posted links on the functions. mkdir() makes a new directory: http://php.net/mkdir fopen() starts a new file stream: http://php.net/fopen once you have started a new stream you can write to it using fwrite(): http://php.net/fwrite and save it using fclose(): http://php.net/fclose I'll write you a small script: <?php $dir = 'myDir'; $file = 'myFile.php'; // check the directory to see if it already exists if( is_dir($dir) === false ) { // if it doesn't make a new directory mkdir($dir); } // start a new stream $f = fopen($file, 'w'); // 'w' for write // write the php to the stream fwrite( $f, '<?php echo "Hello world!"; ?>' ); // close the stream fclose($f); // include our new php script to see if it's working: include $myDir . '/' . $myFile; // Hello world! ?>
  4. You could get the file contents and then echo them along with a different header. <?php header('content-type: image/jpg'); echo file_get_contents('C:/MYFolder/SubFolder/Image.jpg'); ?> Or rather than even need to call the php script, base64 encode it and use that as the url (wouldn't recommend either). <?php $image = file_get_contents('C:/MYFolder/SubFolder/Image.jpg'); $base64 = base64_encode($image); echo '<img src="data:image/jpg;base64,'. $base64 .'" alt="" />'; ?>
  5. It's for a web game. The thing is the game has to be accessible by an API, for cross platform compatibility, so I'm storing everything in a database rather than a cache system. The web application works like any other typical web system would. It sends data through one Ajax request and gets it through another. The getting part uses reverse-ajax. So the client doesn't poll the server when there's no need for it. Then the server runs a PHP script that polls the MySQL server for updates which then returns the new values to the HTTP server which then returns data to the client. I have read up on PostgreSQL and it has listen and notify functionality. Does MySQL not come with the same?
  6. So the answer is no? The only way is polling?
  7. Wasn't really what I was looking for. It just explains reverse-ajax. What I'm looking is more of a MySQL to PHP push technique. Not PHP to JavaScript. What my PHP script does is this: $query = mysql_query("SELECT * FROM `table` WHERE `time` > ". (int)$_SESSION['last_package_time'] .""); $rows = mysql_num_rows($query); while( $rows == 0 ) { $query = mysql_query("SELECT * FROM `table` WHERE `time` > ". (int)$_SESSION['last_package_time'] .""); $rows = mysql_num_rows($query); set_time_limit(1); usleep(250000); } echo json_encode(mysql_fetch_assoc($query)); Which as you can see is rather straining on the MySQL server. There's bound to be a way for MySQL to not return a value until the query has a value.
  8. Is there an alternative to polling a MySQL database? Basically I have a PHP script that polls a table every half a second. This script then returns a value to a client that is doing a reverse-ajax request on the server. Is there a different technique that can be used, so MySQL doesn't return until there is an update rather than polling for an update. Thanks in advance, Chris
  9. You need to port forward port 80 to your local address on your router.
  10. Just to throw even more anoyingness to this thread: $var = 5; $i = 1; while( $i <= $var ) { echo $i . "<br />\n"; $i++; }
  11. I made a nice solution, here's an example: <?php class twitter { var $v = "Hi"; function tweets($i, $b) { return $this->v . $i ."? ". $b; } } $str = "twitter_tweets(5, true)"; $plugin = explode( "_", $str, 2 ); // array( [0] => twitter, [1] = tweets(5, true) ) $func = explode( "(", $plugin[1], 2 ); // array( [0] => tweets, [1] => 5, true) ) $args = substr( $func[1], 0, (strlen($func[1])-1) ); // 5, true $args = explode( ",", $args ); // array( [0] => 5, [1] => true ) // trim whitespace in arguments foreach( $args as $key => $val ) { $args[$key] = trim($val); } $$plugin[0] = new $plugin[0]; // $twitter = new twitter echo call_user_func_array( array( $$plugin[0] , $func[0] ), $args ); // $twitter->tweets(5,true) ?>
  12. PHP isn't Perl. function parimeter($length, $width) { return 2 * ($length + $width); } $parimeter = parimeter($length, $width); echo "parimeter: . $parimeter";
  13. Yeah, I just wrote that code for a quick example. What I'm doing is writing a framework which allows dynamic templates to call functions from different plug-ins example: <div id="twitter"> {PLUGIN_twitter_tweets(5)} </div> This is then parsed by my template system, and then gets the plug-in required (in this case twitter), and call the function "tweets" and pass 5 as a parameter. Here's something like what I've made class template { // shit load of functions, constants blah blah // this function gets called after a few tests // it replaces all the {PLUGIN_*} tags it can. function plugin() { preg_match_all( '~{PLUGIN_[^}]+}~', $this->html, $array ); foreach( $array[0] as $value ) { $value = str_replace( array('{', 'PLUGIN_', '}'), '', $value ); $plugin = explode( '_', $value, 2 ); $this->load->plugin($plugin[0]); // loads the PHP class (once) $$plugin[0] = new $plugin[0]; $str = call_user_func( array( $plugin[0], $plugin[1] ) ); // this is the bit where I'm stuck $this->html = str_replace( '{PLUGIN_' . $value . '}', $str, $this->html ); } } } Twitter class class twitter { // another load of functions and constants are here... function tweets($limit, $image=true, $links=true, $name=true, $sname=true) { $object = $this->getTweets($limit); $html = ""; foreach( $object as $tweet ) { $html .= "<div class=\"tweet\">\n". ( ( $image === true ) ? "<a href=\"http://twitter.com/{$this->username}\" target=\"_blank\"> <img src=\"{$tweet->user->profile_image_url}\" alt=\"{$tweet->user->name}\" /> </a>\n" : '' ). ( ( $sname === true ) ? "<span class=\"screen-name\"><a href=\"http://twitter.com/{$this->username}\" target=\"_blank\">{$tweet->user->screen_name}</a></span>\n" : '' ). ( ( $name === true ) ? "<span class=\"name\"><a href=\"http://twitter.com/{$this->username}\" target=\"_blank\">{$tweet->user->name}</a></span>\n" : '' ). "<p class=\"text\">" . ( ( $links === true ) ? $this->links($tweet->text) : $this->text ) . "</p>" . "<p class=\"time\">" . $this->time($tweet->created_at) . ".</p>" . "</div>\n"; } return $html; } } So as you can see it will requite that I can refer to children and ancestors.
  14. I'm not sure if that's the one I'm looking for. If I'm using it correctly: class myClass { function func($param) { echo "Hello world". $param; } } $class = 'myClass'; $func = "func"; $str = ' It\'s time to partey!'; $$class = new $class; call_user_func($$class->$func, $str ); throws this error: Notice: Undefined property: myClass::$func Making me guess that it's trying to call it as a variable rather than function. Ignore that! I've gotten to this error now : Fatal error: Using $this when not in object context
  15. Hi I'm setting up my own framework for a project. I was coming to the point of completion and thought up of a new idea. Is it possible to call a function like this from a class: class myClass { function func($param) { echo "Hello world". $param; } } $class = 'myClass'; $func = "func(' It\'s time to partey!')"; $$class = new $class; $$class->$func; Thanks for any help!
  16. Oh sorry. I've come up with this: <?php header('content-type: text/plain'); $array = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $level = 0; foreach( $array as $key => $value ) { if( $key+1 == count($array) ) { $level++; } if( $level == 0 ) { echo $value ."\n"; } else { while( $level != count($array) ) { foreach( $array as $key => $value ) { echo $array[$level] . $value ."\n"; } $level++; } } } ?> I can't figure out how to get it to make it's own loops depending on the level. Any ideas? Thanks for the help BTW.
  17. Okay even beter.. the more possibilities the better. Again you're just adding a few numbers together in your loop. That's not the point. It's supposed to loop through the array. Once it's reached the length of the array, loop through it again + with another loop. Then once it's reached the length of the array^2. Loop through it again + another loop + another loop, until it reaches the length of the array^3. etc etc etc. Right up to the length of the array^255.
  18. Yep, and it has to keep making combinations until the string is 256 chars long. Which will be a good few billion combinations, if not more.
  19. No.. you're adding numbers.. maybe i should have made an example with letters. Say I were to do: $array = array( 'a', 'b', 'c', 'd', 'e', 'f' ); I want to make it loop through the array and get a result like this: a b c d e f aa ab ac ad ae af ba bb bc bd be bf ca cb cc cd ce cf da db dc dd de df ea eb ec ed ee ef fa fb fc fd fe ff aaa aab aac aad aae aaf aba abb abc... etc And yes I realise how big this would be.. I'm not planning to do it all in one go.
  20. Hi. I need some help working something out. I have an array that has all the numbers from, 0 to 9. What I want to do is loop through the array to make all the combinations possible up to a string thats 255 chars long. $array = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ); Should be looped through to create strings like this: 0 1 2 3 4 5 6 7 8 9 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 etc.. Just as if it's counting.
  21. sockets Sockets Support enabled
  22. What does the XML look like?
×
×
  • 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.