-
Posts
4,704 -
Joined
-
Last visited
-
Days Won
179
Everything posted by kicken
-
Rather than a starts with / ends with operator I'd rather see a regex match operator, such as =~ or something. Then you could accomplish not only the starts/ends with easily but other patterns too. //starts with $str =~ /^some string/; //ends with $str =~ /some string$/; Just toss the i modifier on for case insensitive. It'd just be nice syntactical sugar though, using the appropriate functions is fine.
-
Not sure I really follow what the game is supposed to be doing. In particular the way you seem to be dealing your cards is rather confusing. If you just want to give each player a random set of num_cards each then all you have to do is something as simple as: var new_deck = deck.slice(0); //Make a replica of shuffled deck array fisher_yates(new_deck);//Shuffle the deck var p1cards = new_deck.splice(0, num_cards); //Extract cards from the deck for player 1 var p2cards = new_deck.splice(0, num_cards); //Extract cards from the deck for player 2 var b1cards = new_deck.slice(0); //The remainder of the deck That's basically like shuffling a deck then taking the top num_cards for player one, then again for player 2. The splice method removes the elements from the original array so there would not be any duplicates between the three end result arrays. What is the game and how does it work/what are the rules? Might help me to understand the code you have written.
-
You can use CSS to define the styles of each rectangle. Like with a forum all the posts just share a similar class which sets their style properties like colors, sizes, etc. You'd have to use something else to generate the actual elements in the HTML though, that is not CSS's job. CSS only does styling.
-
Your posted example code works just fine. If your going to edit down a problem into sample code, the sample code needs to at least re-produce the issue you are having. Without the real code, or a representative example, it's hard to provide any help. It sounds like a case where you might need to add a closure to lock a value in scope, but I could be wrong. If you can't or don't want to provide the actual code, try to re-create the issue on JSFiddle and then give us the link to or a copy that code.
-
Only a couple points 1) Make sure your executing it via the PHP-CLI interface rather than through the web server. Running it through the web server just ties up a server thread and introduces additional complications. 2) You may want to add something to ensure you don't end up with multiple instances of the script running at the same time. Especially if you start increasing the max execution time higher or disable it (setting it to 0 allows a script to run indefinitely).
-
If your wanting to parse HTML you should use DOMDocument. Your HTML will have to be mostly valid though for it to work properly. Parsing HTML with regex is generally considered a bad idea. It works sometimes, I'll do it sometimes for one-off re-format scripts or small personal-use scrapers but for something like a template engine you'd be better off going with a real html parsing solution like domdocument.
-
That block of IF statements does not make sense where it is, as $i is not defined at that point. From what it looks like you probably want to be using $cut instead of $i in your conditions. The conditions themselves are also incorrect as the = operator does an assignment not a comparison. You need to use == (two equal signs) to do a comparison check. The way it is right now, your price would always be 3.50 because the first condition would assign the value 2 to your variable, and then set the price to 3.50
-
If your using the COUNT(*) function in your query, then you just fetch the result like a normal query. example: $pdo = new PDO("mysql:host=127.0.0.1:3306;dbname=$databaseName", $password, $username, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); $pdoStatement = $pdo->prepare("SELECT COUNT(*) as cnt FROM LargeTable"); $pdoStatement->execute(); echo $pdoStatement->fetchColumn(); Also, disabling the MYSQL_ATTR_USE_BUFFERED_QUERY option might save you memory, depending on how your processing the query results. For a COUNT(*) query though it shouldn't make much of a difference as there is only one row in the result set.
-
Puzzeled in working new(to me) technology
kicken replied to bugcoder's topic in Other Programming Languages
There's no reason really why you can't do both. Many developers have experience in multiple languages and/or frameworks. Keep up with your PHP through existing projects/clients and keep a look out for new clients. Start learning RoR in whatever spare time you have by making little personal projects, or re-coding one of your current projects in RoR just for the experience. Whenever you feel comfortable with RoR then you could start picking up jobs related to RoR and PHP. -
If your only going to run a query once (which is common for SELECT queries) and the query does not depend on any user inputted values (such as querying app config data or similar) then you can just run the query and skip all the prepare steps. If your query does use user inputted values, but you've sanitized them by some other method, then you could also use query rather than prepare. I do this for queries where the only values used in it are a list of ID numbers inside an IN clause, since there is no way to bind a list of values, only single values. I run intval() across the array to sanitize the values, then just concatenate them into the query and run it. All other cases I use a prepared query for though. The sql injection protection is reason enough, any other benefit is just icing on the cake.
-
Yes. It's more common/useful for things like INSERT or UPDATE queries but preparing the query once and executing it several times will result in better efficiency. When you run a query there are two steps to it. First, the server parses and analyses the query to determine the best method of execution (ie, which indexes to use, where to grab data from, what order to get it, etc). After that it executes the devised plan and returns the results. When your running the queries each time, such as with the old mysql_query function then both these steps have to be processed on every run of the query. When you use a prepared query the steps are broken up. When you first prepare the query the server will parse and analyse it and devises a plan. Every execution after that will used the devised plan, saving time that would have been spent re-parsing and re-analyzing the query under the non-prepared methods
-
Puzzle/challenge: making a link or button perform a POST request
kicken replied to jhsachs's topic in Javascript Help
Rather than modify all the links to add an onclick attribute, it would be better to setup a function to run when the dom is loaded which installs an onclick handler on all the link. If you only want to target certain links, give them a class that can be used to identify them. If your using a library such as jQuery then it is a fairly simple task: jQuery(function($){ $('a').click(function(e){ e.preventDefault(); //prevents the browser from following the link. //generate, populate, and submit form }); }); Using plain JS would not be much harder but I'll leave that as an exercise for the reader. -
My post in another thread about the and/or logic might help you visualize it a bit better if you still need a little help understanding it. You can read it here: http://forums.devshed.com/showpost.php?p=1398306&postcount=3
-
Firefox seems to show the content mis-aligned , the body is way over on the right hand side. Your site is also vulnerable to XSS: http://tinyurl.com/crkj3gd The design of the site looks alright to me.
-
If the script does not need to modify the document during the load process (which most don't) then it is generally recommended to put the script before the </body> tag. Doing so allows the browser to get the HTML document rendered to the screen before it spends time loading the scripts for the page. Generally though it doesn't really matter where you put them.
-
FYI, The query string (everything after the ?) is available in $_SERVER['QUERY_STRING']. No need to try and parse it out of the request uri.
-
Your original regex did not allow the dot (.) character so it would not have matched such a URL. Since you would be going to /articles/add_comment.php the new regex probably could cause an issue yes. As I mentioned, you can use a RewriteCond to test if the file exists and if so, by-pass the rewrite. I believe for that you would change your rewrite to these lines: RewriteCond %{REQUEST_FILENAME} !-f RewriteRule articles/(.+)$ articles/article.php?slug=$1 I am not positive on that though.
-
Yes, since that is what you asked for: The new regex will basically just pass on to your script anything in the url after the articles/ part. Then within your PHP code you do all your necessary checks and output any appropriate error messages. It means if you had any files such as a CSS file, Image file, JS file, etc that had a url such as local.debbie/articles/styles.css (for example) that it would not be properly handled with the new rewrite rule. If you do not have any cases of url's like that, then you have no issues.
-
Change your rewrite regex to just look for any characters rather than the specific set of characters: RewriteRule articles/(.+)$ articles/article.php?slug=$1 The only issue's that might cause is if you have other files inside the articles/ url that you want to access, but you can bypass them using a RewriteCond to check for their existence first.
-
If I am understanding the manual entry for SSLRequire you should be able to do the following check to allow for both www.site.com and site.com. You could extend it for any sub domain if you wanted as well by altering the regex SSLRequire %{HTTP_HOST} =~ m/(www\.)?site.com$/ If possible, it might be easier to keep everything that needs to be over SSL in the same sub-directory and just require ssl for that directory. Perhaps someone a bit more familiar with rewriting could provide some rewrite based options. Another thing you could do is just force everyone to use SSL for the entire site rather than try and pick-n-choose just specific pages.
-
You should have a DOCTYPE in your final HTML output. Where you put the actual tag depends on how your generating that output though. If your php file is the HTML page just with little bits of PHP in it then yea, add the doctype there. If your using a template system then your doctype would go in your templates somewhere.
-
Why? There's no problem with exposing ID's.
-
For a case where a user only gets to have one file I just name it after their ID since that is always unique per user and makes linking of the file easy. For a case where a user might be able to have multiple files (say a photo album) I typically just concatenate their ID with the output of uniqid, using something describing the file type as the prefix. Eg, for a photo album I would do: $file = $userid.'_'.uniqid('photo'); Just to be on the safe side I toss that in a loop that checks if the generated name exists and if so it generates a new one, but such a collision should be very very rare. In the particular case of a profile photo, it is going to be linked to a person's profile by virtue of appearing on their profile or next to any post they make. Why does it matter if the file name matches their ID/name or not? It's not like a profile photo is really a private thing anyway. It is meant to be public as it's basically their "face" on the internet. In the case of other situations, so long as you do something like the above using uniqid or some other form of randomness, nobody will be able to predict users file names. You could hash the filename to remove the user id if you wanted but I find it to be an unnecessary step. The most anyone would be able to do by seeing the user id in the name is determine who the file is associated with, but chances are they got to the file in the first place by visiting that user's profile or similar page so they already know that information.
-
I find most of the time either it does not auto-complete correctly or not in a manner that I like. The only type of auto-complete I like is one that is an auto-suggest and will let you tab to complete (Visual studio does this). Pretty much every other editor I've used, disabling auto complete is one of the first things I do as it tends to be more frustrating than it is helpful. For what it's worth, Edit+ does support auto complete (read the features on the site). I didn't put it on my list because I don't use it.
-
Your .+ between the script tags in the regex requires that there be at least one character between the start and end tag for a match. Since you have no characters between your start and end tags the match doesn't work on the individual pairs. It matches both pairs as a whole because it counts the end/start tags as the required content in the middle. Use .* for a zero or more match.