Jump to content

gizmola

Administrators
  • Content Count

    4,944
  • Joined

  • Last visited

  • Days Won

    48

gizmola last won the day on January 2

gizmola had the most liked content!

Community Reputation

167 Excellent

2 Followers

About gizmola

  • Rank
    Prolific Member

Contact Methods

  • AIM
    gizmoitus
  • Website URL
    http://www.gizmola.com/

Profile Information

  • Gender
    Male
  • Location
    Los Angeles, CA USA

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. Well here is where the variable is assigned: $human = $_POST['human']; This code will have issues if it's submitted without the various form fields filled in. You don't check for that for any of the form variables. Probably, the code would be better if all the form assignments was moved inside the the if ($_POST['submit'] ... condition because it makes no sense to try and process the form and assign local variables to form variables, if it wasn't. And in fact all the form variable assignments will benefit from changing them to be trimmed. $human = trim($_POST['human']); Better yet, if you want to leave the variable assignments at the top of the script, check that the variable actually exists so you don't get a runtime error if it doesn't: $human = !empty($_POST['human']) ? trim($_POST['human']) : ''; Now you fix your condition: if ($_POST['submit'] && strtolower($human) == 'wednesday') { Note that I fixed the quotes around wednesday. Originally they were backtics, which have a special function in PHP. Read these pages: trim, strtolower
  2. This is apples to oranges. 1. SSH to a shell on your host 2. Connect to localhost mysql server on port 3306 from shell on host Trying with Workbench: 1. Try to connect to localhost mysql on 3306 from your workstation See the problem? You could set up a port forward on your workstation, but Workbench and SQL pro both have some built in support for configuring forwarding in the client. See this article among many others out there if you google: https://www.digitalocean.com/community/tutorials/how-to-connect-to-a-mysql-server-remotely-with-mysql-workbench
  3. If you mean HTML I am doubtful anything near pure html with css and js will work. You could play with the Source view, but any forum is going to be extremely limited in what it allows you to put in. I don't know how much trouble you want to go to, but sites like Codepen, jsfiddle, jsbin etc. were built for that, so maybe that's a solution?
  4. In simplest form, PHP implements symbol table(s) which store variable names used in the script and zval(s) which are the actual data structures that manage variables. You could have 3 variable names: $a = $b = $c = 5; And this will have 3 symbol table entries but only 1 zval will be created. The zval has a reference count, so not surprisingly, this zval will have the value 3, indicating that there are 3 symbols using this zval. Now you change the value of one of the variables: $a++; As requinix states, PHP will make a new zval and copy the original value from the first zval. The symbol table entry for $a gets changed to point to the new zval, and the original zval has its reference count decremented by 1. $b and $c still point to the original zval. If you want to really geek out on PHP internals, there's an excellent series on PHP 7 vs 5 changes. The author also wrote a great article on the hashtable implementation that powers a lot of things in PHP including arrays. https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html https://nikic.github.io/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html
  5. With most of these examples you are omitting blocks. Here is a block: { //Something } Many languages use the {} to denote a block. There are others that use something different, or use indentation (most notably python), but PHP is not one of those languages. Let's go back to an example you gave (Indentation mine): if($_POST['form']['hobby'] != '') echo "<br />"; $modUserEmailText .= '{hobby:caption} : {hobby:value}'; After your condition ($_POST['form']['hobby'] != '') You do not have a start block. So only the next line will be executed if the condition is true. I don't think that is what you expect. So you need a block around ALL the code that will execute if the condition is true: if($_POST['form']['hobby'] != '') { echo "<br />"; $modUserEmailText .= '{hobby:caption} : {hobby:value}'; } Obviously you are not giving us real code or are giving us snippets of the code as the errors don't correlate to the code you've provided, but perhaps this explanation will help you out. Just for completeness: if (!empty($_POST['form']['hobby']) { echo "<br />"; $modUserEmailText .= '{hobby:caption} : {hobby:value}' } This would be the proper way to handle the condition you are checking for. The problem with the code you presented, is that it will provide a runtime error if the $_POST['form']['hobby'] element does not exist. For things like checkboxes, this is a common occurrence. empty checks that the variable exists (isset) AND that it has a value (!= '''); You might also want to trim the value, as even a space would pass through your check and empty.
  6. Porbably the best solution in this case is to use media queries Here's an example that might help that targets phone sizes: /* Smartphones (portrait and landscape) ----------- */ @media only screen and (min-width: 320px) and (max-width: 480px) { .logo img { width: 60px; } /* other css rules */ } You can make alterations to the rules that drive the carousel using this technique, perhaps hiding it or doing something else.
  7. gizmola

    Cache Memory Issue

    Top is something that you have to run for a while and watch while the server is under a sort of typical period of load. At any one instance it can be misleading, but if we take this at face value, it's telling you that your cpu's are doing almost nothing, so you have a lot of cpu power you could harness for additional work. Since you mention this server also runs a mysql db, I can assume that when you pull crypto data from the api's you mentioned you then process that data and store it in your mysql db. I can suggest to you that, with all the memory you have, and again assuming your db will grow over time, 2 things to look at: Make sure all your tables are InnoDB! If you did not declare them as such originally you can still use ALTER TABLE to change this. Take a large chunk of your available server memory (anywhere from 40-80%) and allocate it to the mysql innodb buffer pool. This is mysql InnoDB data cache which will cache the results of queries in mysql memory. Again you have to be somewhat careful that you retain enough memory for whatever else you might be doing. Here's one article with some algorithms for how to determine what to allocate and configure for your mysql server: https://scalegrid.io/blog/calculating-innodb-buffer-pool-size-for-your-mysql-server/ If you were to run for example, an AWS hosted MySQL server via their RDS service, you'd find that by default it's going to have allocated 80% of available instance memory to the buffer pool, so that gives you a good idea of what works well for most servers. Once you were to reconfigure mysql to allocate a big chunk of memory, you'll see the cache go way down accordingly, and the performance of mysql should improve again assuming your system does frequent queries of your crypto pricing data in MySQL.
  8. Echoing Requinix on this, but they aren't all that much different than an include/require of some code in a class body. Obviously better than that, from a documentation point of view, in that at least you see the declaration of the trait at the top of the class, but otherwise, it's about the same. In regards to interfaces, I would say it's best practice and certainly used in large projects and frameworks that class designers start with one or more interfaces to lay out the api contract. For example, take a look at the Laravel Cache system. Let's say you have a type of cache that doesn't come out of the box. How do you make your ACME cache work with Laravel? Laravel has a contracts directory where the interfaces are defined, and for cache we find this interface for a "Store" https://github.com/laravel/framework/blob/5.3/src/Illuminate/Contracts/Cache/Store.php You can see that this interface describes the cache api with methods like get, put, increment, decrement, forget, etc. When you look at the implementation of a specific cache store with bundled support like Redis for example, this is what you find in RedisStore: <?php namespace Illuminate\Cache; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Redis\Database as Redis; class RedisStore extends TaggableStore implements Store { /** * The Redis database connection. * * @var \Illuminate\Redis\Database */ protected $redis; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * The Redis connection that should be used. * * @var string */ protected $connection; /** * Create a new Redis store. * * @param \Illuminate\Redis\Database $redis * @param string $prefix * @param string $connection * @return void */ public function __construct(Redis $redis, $prefix = '', $connection = 'default') { $this->redis = $redis; $this->setPrefix($prefix); $this->setConnection($connection); } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { if (! is_null($value = $this->connection()->get($this->prefix.$key))) { return $this->unserialize($value); } } /** * Store an item in the cache for a given number of minutes. * * @param string $key * @param mixed $value * @param float|int $minutes * @return void */ public function put($key, $value, $minutes) { $value = $this->serialize($value); $this->connection()->setex($this->prefix.$key, (int) max(1, $minutes * 60), $value); } So it should be fairly obvious to someone looking to add this capability to their Laravel project that an Acme cache will need an AcmeStore class that implements Illuminate\Contracts\Cache\Store. I looked around a bit at the Laravel Docs and found this section that outlines the same thing I've discussed using MongoDB as an example: https://laravel.com/docs/5.1/cache#adding-custom-cache-drivers As a dependency injection framework, there's going to be some sort of service system that takes care of instantiating a service, which is why this discusses how to register the store. Here is a MongoDB Laravel cache implementation I found on Packagist: https://github.com/alfa6661/laravel-mongodb-cache/blob/master/src/MongoStore.php When you look at this code, you'll see that it extends the Laravel DatabaseStore class rather than implementing the store interface, but that simply has to do with the fact that Mongo is a type of database and has some database-esque features that most pure cache servers don't have. When you look at the DatabaseStore Class you can see that it too implements the Store interface: class DatabaseStore implements Store { I think looking at how Laravel and Symfony have done things could be helpful in seeing the language features and techniques class designers use to create reusable and extendable class hierarchies. Another interesting example I could suggest to look at would be the Symfony finder component. It's a component library that facilitates searching for and working with files and directories on a local or network file system. The finder class implements 2 Standard PHP Library interfaces: class Finder implements \IteratorAggregate, \Countable This allows a finder object to be used in a statement like: if (count($finder) > 0). Implementing the IteratorAggregate interface facilitates iterating through an object using foreach(). If you are interested look at the implementation public function getIterator() in the class source. This is all far more interesting and important than Traits. You'll also probably start to notice that the interfaces are used to typehint dependency injected objects in related classes, which keeps those classes loosely coupled and yet still with some type resiliency. I can pass an object of a class that implements the interface, but if an object that does not implement the interface is passed, a fatal error will be generated.
  9. gizmola

    Cache Memory Issue

    This is what Linux does. When the OS has lots of extra memory available it will use it to cache the filesystem. If more memory is needed it will take memory from cache and allocate it. You can think of it as Linux being proactive in making use of an available resource rather than having it go to waste. In regards to your site, the main question would be what level of CPU usage (top is a good tool to start with) you have, and what else the server is being utilized for. I don't know if you can increase the number of processes that are pulling crypto price data, or processes that are doing other sorts of data or number crunching to make more use of the memory and cpu resources you have. If this server also hosts a website that handles clients and displays data or allows for searching, it might be better to just be satisfied that you have ample headroom, and work on building up your audience. Really nobody could advise you without knowing more about what you are doing, but there is no reason to worry about your cache, until such time as your server becomes IO bound, and you are seeing sluggishness and lots of IOWait, where the machine is doing nothing as it waits on the reading & writing of data.
  10. A couple of comments on your code: function convertTimeFormat($time12Hour){ echo date("H:i:s:A", strtotime("06:40:03 PM")); } First off you are not using the parameter $time12hour. Some smart editors will introspect and alert you to this. You can also typehint your parameter and the return value. Since you require $time12Hour to be a string, it is helpful to add the typehint to the parameter. Also, with functions it is best practice to return a result. You shouldn't echo the result, but instead return it. You can also typehint the return value, which should also be a string. function convertTimeFormat(string $time12Hour): string { return date("H:i:s:A", strtotime($time12Hour)); } Now your example code becomes this: $time12Hour="06:40:03PM"; echo convertTimeFormat($time12Hour); You could also save the function result to a variable,.
  11. Foo/Bar examples aren't helpful in exploring a question like this. I also think you are leaving out a key ingredient when you aren't pairing inheritance with one or more interfaces. Setter/Getter examples are also no helpful when you have __set and __get magic methods available. Where I have seen traits used frequently, is in providing oft repeated exception throwing/handling code. This often includes logging, so you almost always see classes injecting a logger object and then logging out information when exceptions/throwables are caught/thrown. You could also put these routines in a base class, but it's extra work and possibly extra inheritance for something that is necessary but not intrinsic to a specific class hierarchy. Traits and classes are not mutually exclusive nor in my opinion should they be used that way. OOP languages were created in order to help model paradigms and simplify the passing of data, messaging and events between things. Typically it's best to start with OOP, DI and other design patterns, and only when things become particularly odious, or are highly disconnected from the data and behavior you are modeling in the core class hierarchy, would I look for alternatives like a Trait.
  12. They are part of the language. Whenever possible you should always use the built in types and functions unless you have a very good reason not to.
  13. No that is not a php array. That is json. What have you tried? If you don't know where to start, start by turning it into a php array using json_decode. Your actual question doesn't make sense. Average of which values?
  14. Sorry, you are using mysql_query(). It's a waste of time for people to answer questions about mysql_ functions. It was listed as deprecated (which meant you should no longer create NEW CODE with those functions in 2013. As of now, there is no supported version of PHP that has the mysql_ functions. Convert your code to mysqli_ or better yet, PDO, and we'll look at. Probably your issue has to do with your lack of use of WHERE IN ().
  15. The main argument against using 'new' to create objects inside a class, is that it now becomes very difficult to unit test your code, you now have no way of mocking the dependent class. If you use a service container, then you can mock the container itself, so long as you pass the DIC to the class that needs to create the objects internally.
×
×
  • 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.