-
Posts
6,055 -
Joined
-
Last visited
-
Days Won
153
Everything posted by gizmola
-
Please run this in command line mysql or phpMyAdmin and give use the result: describe company; describe co_info; When you do this, what do you get? SELECT * from company WHERE active = '0';
-
Composer and packagist were an extremely important step in the evolution of php. Other trends like the rise of git to prominence in the software development world, also contributed to this, but in the modern world of development languages, having a universal package manager like node has with NPM, RubyGems for Ruby or Python has with Pip is more or less expected by any serious software developer these days. Prior to the addition of namespaces to PHP and subsequently PSR-0 and eventually composer & packagist, using libraries in your project was a perilous and potentially error-fraught exercise. For example, someone may have developed the most amazing image processing library for use with Joomla, and there was just about no way possible that you could make use of that library with Drupal, or your own home grown framework. We had numerous competing frameworks with their own conventions and modularity and just about no reuse available. This lead to rampant reinvention of the wheel, and the libraries and components themselves were often slipshod and lacking in unit tests or other quality controls, and frequently would fall behind as the original developers moved on. Compared to other languages PHP was far behind in this regard. An early attempt to address the problem was PEAR, but it was far from successful for many reasons. Now that we have Composer and packages, PHP has entered a new age, where we've seen the development of discreet well tested component libraries that tackle individual problems, and using composer you can integrate them into any project you want, whether that project is based on a framework, or one you built yourself. You create systems and I assume, intranet or not they need to do things. For example, they may store files, or manipulate images, or could benefit from MVC or templating for seperation of data and presentation. If you take a look throught packagist you will be floored by the sheer quantity of libraries out there, and how they solve problems and can make daunting development tasks as easy as adding a new component and creating an instance in your app. With composer, and a quick perusal of packagist you can be up and running with one of any number of libraries in record time, and without the need to download and copy the libraries, or determine if they depend on other libraries. Quite simply, it's a huge game changer for the PHP community, and if you aren't using composer, you just aren't doing it right.
- 3 replies
-
- open-source
- packagist
-
(and 1 more)
Tagged with:
-
PHP arrays are most undoubtably a better answer: $page = array(); $pages[1] = 'page1.php'; $pages[2] = 'page2.php'; $pages[3] = 'page3/page3.php'; $link = (int)$_GET['link']; if (isset($pages[$link])) { @require_once(realpath($pages[$link])); } else { // hacking url param or page not found }
-
Don't concern yourself with closing the database connection, especially when using MySql. When the script is done executing all handles are closed for you. There are very few situations where you need to worry about opening and closing individual database connections within a script.
-
Implementing captcha to my dynamically generated forms
gizmola replied to VanityCrush's topic in PHP Coding Help
In your function comment_form($id, $captcha), you added the $captcha as a param but you never use it in function. Probably after this line you would use it: <input type='hidden' name='blog_id' value='$id'> $captcha <input type='submit' name='submit' id='post' value='post'> -
Did you read this page? http://php.net/manual/en/function.session-set-save-handler.php Session handlers involve a bit of black magic and attempting to trace them can be confusing. With that said, the write handler that is going to write out any changed variables gets executed at the end of script execution. So yes, if you closed the database connection it was going to use, that is not going to work. You should never be issuing html output prior to a session_start(). If you really want debugging/logging then you're much better off adding a file logging feature to your framework, and writing debug information there.
-
The bigger question these days is: -Relational data store (mysql,postgresql,oracle,sqlserver) or non-relational/nosql/document oriented (mongodb, cassandra, couchbase) -if relational, straight API vs db class? -if db class, create your own vs use existing - if db class, use ORM (doctrine2, propel, laravel eloquent)
-
Implementing captcha to my dynamically generated forms
gizmola replied to VanityCrush's topic in PHP Coding Help
Heredocs will interpolate variables. So something like this should work: function comment_form($id, $captcha) { global $user_data; if (logged_in() === true) { return <<<EOT <form method='post' action='' class='comments_form'> <input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'> <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea> <input type='hidden' name='blog_id' value='$id'> <input type='submit' name='submit' id='post' value='post'> $captcha </form> <hr class='artline'> EOT; } $captcha = create_captcha(); echo comment_form($id, $captcha) Of course you need to alter the create_captcha function so that it returns a string rather than echoing out the markup directly, but that is a general pattern you should be using in all your functions. It will start to improve the separation of concerns. -
Reverse words in a sentence without using PHP functions
gizmola replied to Dota2's topic in PHP Coding Help
The key to requinix's ingenious solution is the use of the '@' error suppression operator. for ($i = 0; @$input[$i] != ""; $i++) { This allows him to for-loop character by character through the input by treating the string as a character array, and then by reading beyond the end of the string. That allows him to avoid the use of the strlen() built-in used by Scootsah. From there it's just concatenating the strings he finds in reverse order. Nice! With that said, I think what they wanted you to do was use a stack, given the way the question was posed. An array works pretty well for this, so Scootsah's solution is basically what I think they expected. He reads the words and adds them to the array using "$array[] = $word". You get a numerically ordered "stack". Then it's just a matter of for looping through the array in reverse order. So just for fun, here's a way you could walk through the array in reverse order without using a negative for loop to duplicate the array into a reversed form, based on Scootsah's solution: <?php function reverseString($input) { $currentWord = ''; $words = array(); for ($i = 0; $i < strlen($input); $i++) { if ($input[$i] == ' ') { $words[] = $currentWord; $currentWord = ''; continue; } $currentWord .= $input[$i]; if ($i == (strlen($input) - 1)) { $words[] = $currentWord; } } if (!empty($words)) { $output = ''; for (end($words); key($words) !== null; prev($words)) { $output .= current($words) . ' '; } return trim($output); } } $input = 'this is phpfreaks'; $output = reverseString($input); echo "Input: $input<br />Output: $output"; -
Hi Rocky48, I have to agree with ch0cu3r & ginerjm. I don't think anyone is being condescending. To clarify things: The mysql_ api is deprecated. mysqli_ is not. PDO and mysqli are two different ways to stay current with mysql use. You do not have to change your code to use PDO to avoid the deprecation. The mysqli api has both an "object oriented" interface, and a procedural (function based) interface. It also has a substantial number of "alias" functions. Unfortunately, the mysqli devs went a bit overboard with all the aliasing and eventually the PHP community came to the conclusion that they should clean things up by removing a bunch of the aliases, and that is what you have hit upon in the "warnings" you are seeing for your current mysqli based code. You could remedy this with the least pain and suffering by simply looking at the page ch0cu3r linked, and simply changing the deprecated name of the function(s) you used to the one(s) it is aliasing. With that said, if you really want to move to PDO that is fine, but please understand that people were simply trying to clarify things for you, and address what clearly were some unfounded assumptions about the warnings you received. Mysqli_ is not going anywhere, and you don't have to abandon it for PDO!
-
All of the above. In general terms, the most important thing is to make sites. You will learn more by doing than all the other methods combined.
-
Registration vs. login and password resend are a couple of different issues. I can't explain why you couldn't login. Email sending is somewhere between iffy and non-existant. I'll put it on my list to poke into when I have a moment. If mail is being sent out I'd expect that many systems would see it as spam. I really do appreciate these reports however. It takes a village
-
Visited parts of Maine in my youth -- beautiful state. Glad to have you here.
-
500 internal server error - uploading avatar - Form action
gizmola replied to reloadmvp's topic in Third Party Scripts
Did you check the permissions for files in the upload directory matching the spec that is being unlinked? Is there possibly a file there that has different perms, such that it can't be removed? Are warnings turned on, and are any warnings being generated? There are many things that could be going wrong, but if I understand you correctly, this code works for other users, just not for you? For that reason, you want to check your assumptions by making a test account or several and determining if it is local to your workstation environment or your specific user. What else could be different about your account? What is the nature of $user->userID? Is this a number or a string?- 1 reply
-
- forms
- upload image
-
(and 2 more)
Tagged with:
-
PHP has date/datetime classes. They literally allow for any type of format you want. I'd suggest you start with those because they also provide the ability to do date arithmetic. With that said, there seems to be no rhyme or reason behind your examples. What are these dates and where do they come from? There is no way to show you a loop when we don't know what you're looping through!
-
How many more posts will you make without using code tags for readability of your code? The first reply from scootstah used them. One would think that would be a hint you might look into.
-
Thanks for taking the time to let us know. We'll look into this issue and see if we can figure it out.
-
I think you need to elaborate on your issue. Is it that you are adding/removing files and the list on your page is not being updated? or The contents of the individual files are not being updated?
-
Unfortunately cron jobs are fairly archaic and opaque. You could create the illusion of a schedule in javascript and present countdown timers, so long as you either codify the cron schedule, or read it and parse it, however given the use of directories like cron.daily/weekly/hourly and the possibility of having many different users owning cron jobs, there is nothing out there that already does this for you that I am aware of. If there are cronjobs installed you can get the raw crontab listing using: crontab -l But then you get something like this: * * * * * /var/www/foo/utility/something.sh params You'd be re-wrting the cron parser before you even began to figure out rendering a display and timers.
-
How about leaving triggers out of it and just have a proper relational structure? MySQL triggers reduce concurrency and are very slow relatively speaking. MySQL provides a way to find the auto_increment id of a just inserted row as alluded to by scotch in pseudocode -begin transaction -insert parent -get id -insert many-to-many cities table rows for any relevant cities -commit There should be a cities table and not a redundant storage of the city name over and over. I realize this was not your design, nor your design mistakes but when you have a requirement that you change something, it gives you an excuse to start doing things properly and this will improve the system in the process and preserve performance over time.
-
I don't see an issue with the code. Is there anything in your apache or php logs? With that said, the code: imagepng($img, 'signature.png'); is going to attempt to write the file in the current working directory where that script is running. Your permissions may not be allowing a file to be written there. Try providing the full path to a directory and filename you are sure has write access for the user apache is running as. I'm not much help there because I don't use windows/xamp etc. You should also try and turn off the warning level while debugging this. Try adding: error_reporting(E_ERROR | E_PARSE);
-
There is an old compsci saying: "Garbage in garbage out". In order to determine what data you have to work with, you need to to dump out the data structure in some human understandable way so that you can check your assumptions. There are several tools for this in php: var_dump and print_r for example. I also think that given a small amount of effort, you could probably learn to understand JSON format, which is fairly easy to understand if you invest maybe 20 minutes studying it online and looking at some examples. Here is the raw JSON output from your example: { "results" : [ { "address_components" : [ { "long_name" : "95117", "short_name" : "95117", "types" : [ "postal_code" ] }, { "long_name" : "San Jose", "short_name" : "San Jose", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara County", "short_name" : "Santa Clara County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "San Jose, CA 95117, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 37.3299101, "lng" : -121.9499179 }, "southwest" : { "lat" : 37.2943199, "lng" : -121.9817339 } }, "location" : { "lat" : 37.3120731, "lng" : -121.9643745 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 37.3299101, "lng" : -121.9499179 }, "southwest" : { "lat" : 37.2943199, "lng" : -121.9817339 } } }, "place_id" : "ChIJ4YcqPMHKj4AR_oe_L8U_ojs", "types" : [ "postal_code" ] } ], "status" : "OK" } In the code you provided, which simply converts the JSON to a php array, once you understand JSON a bit you can figure out what the array structure is going to look like. It appears that determine the city data isn't as simple as finding a fixed key, because the structure contains an array named 'address_components' with a number of generic sub objects. You have to actually look for the 'locality' key in the list of types for that embedded object: { "long_name" : "San Jose", "short_name" : "San Jose", "types" : [ "locality", "political" ] }, So for example, I can see that something like this would seem to allow you to determine the city component: $city = array(); foreach ($data['results'][0]['address_components'] as $addrObj) { foreach($addrObj['types'] as $type) { if ('locality' == $type) { $city['short_name'] = $addrObj['short_name']; $city['long_name'] = $addObj['long_name']; } } } Of course, I'd highly recommend using var_dump($data) to debug these assumptions.
-
finding difference between current date and future date.
gizmola replied to cloudll's topic in PHP Coding Help
Use the datetime object, and this is pretty simple. $date1 = new DateTime(); $date2 = date_create("2013-12-12"); $diff = date_diff($date1,$date2, true); echo $diff->format("%R%a days");