Jump to content

kicken

Gurus
  • Posts

    4,704
  • Joined

  • Last visited

  • Days Won

    179

Everything posted by kicken

  1. If you need two mutually exclusive values you should be using two radio buttons or a select element, not a checkbox. Checkboxes have one value which is either active or not depending on the checked state. Using JS to change it like your attempting to do is a horribly wrong usage or checkboxes.
  2. Here is a sample of how to use pcntl_fork properly. This is based on a script I wrote that launched multiple crawlers. The MAX_PROCESSES define limits the number of concurrent processes. It will launch up to that many processes and then waits for them to finish. When one finishes it cleans it up then launches the next one. It also checks for a .stoplauncher file which I can create to terminate the program if necessary. You could just do a endless loop and kill it instead if you wanted. <?php if (file_exists('.stoplauncher')){ die('.stoplauncher file exists.'); } define('MAX_PROCESSES', 3); $runningPIDs=array(); while (!file_exists('.stoplauncher')){ while (count($runningPIDs) < MAX_PROCESSES){ echo "Launching worker process."; if (($pid=pcntl_fork()) == 0){ RunProcess(); } else { if ($pid == -1){ die('Failed to launch child process'); } else { echo "PID=", $pid, "\r\n"; $runningPIDs[] = $pid; } } } echo "Waiting for a child to finish.\r\n"; cleanupChild($runningPIDs); } while (count($runningPIDs) > 0){ echo "Waiting for a child to finish.\r\n"; cleanupChild($runningPIDs); } function cleanupChild(&$runningPIDs){ $pid = pcntl_wait($status, 0); if ($pid == -1){ echo "Failed to wait\r\n"; } else { echo "Cleaned up process ", $pid, "\r\n"; for ($x=0,$l=count($runningPIDs); $x<$l; $x++){ if ($runningPIDs[$x]==$pid){ unset($runningPIDs[$x]); } } $runningPIDs=array_values($runningPIDs); } } function RunProcess(/* You can pass in whatever parameters you need from the parent */){ //Do whatever you need to do here //exit when you are finished. exit; }
  3. Take whatever number they enter into the field, verify it is a number, and then convert it to a decimal percentage. For example: <?php $currentPrice = 54.99; if (isset($_POST['percent'])){ if (ctype_digit($_POST['percent'])){ $percent = intval($_POST['percent']); $percent /= 100; // Convert it from say 40 to 0.40 if ($_POST['type']=='+'){ $percent += 1; } else { $percent = 1 - $percent; } //Now $percent will be say 1.40 if an increase or .60 if a decrease //Just multiply it by the current price. echo $currentPrice * $percent; } } ?> <form action="" method="post"> Enter percent: <input type="text" name="percent">% Type: <select name="type"><option value="+">Increase</option><option value="-">decrease</option></select> </form>
  4. Try changing the line: if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { to if (($char == ':' || $char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  5. A file can have as many extensions as you want it to have. In this case, .inc indicates that it's a file ment to be included, not run directly. .php indicates it is a file containing PHP code. Having the .php also makes it so the server will execute it so if someone tried to load it directly in their browser they would just get a blank page, and not your source code (like they would with just .inc). That bug where they rename the files is just due to their own coding conventions where they decided to standardize on using a .inc extension but a few files were still named .inc.php
  6. I have a small handful of functions that I use defined in a functions.inc.php file and I just include that file in the main script.
  7. You can use call_user_func_array to execute it with a variable number of arguments.
  8. Your code looks ok to me as it is. Your query is probably just returning zero-rows so your ->fetch call is returning false. Run the query manually in mysql to verify (substituting the proper value for your :token placeholder).
  9. The last character of a string is at index strlen($string)-1, not strlen($string). function convert_mails($source_path, $mail_file = '') { global $output_path; print "sp=$source_path"; // -- parameters must have a trailing backslash -- $last = strlen($source_path)-1; if ($source_path[$last] != '\\') $source_path .= '\\'; ...
  10. There is not way to follow what a user does on another site without enlisting that site's help. What I mean by that is you would have to contact Google, Yahoo, etc and ask them to provide you with some method of determining that the user submitted the review. PHP sessions are only valid for your own pages and JS is limited by the same-origin policy which prevents you from interacting with the other sites. The best you could do without entering some kind of partnership with the sites is either: 1) Have the user's submit the review to you and then you re-submit it to the other sites. If the other sites require a login first then this option won't work. 2) Use screen-scraping techniques to check the sites periodically to see if the review has been posted. Not a very reliable method and you'd need some way to identify their review vs any other user reviews that have been submitted.
  11. My guess is that your version of rand() could only seem to come up with 5048 unique values. That also explains why my code just looped forever because mine would not stop until you reached 600,000 unique values which for some reason your setup is incapable of doing. I have no idea why your implementation of rand() seems to be so poor. What OS and PHP version are you using?
  12. There is a basic intro and some documentation about it on the PHP website in the manual: PHP at the Core: A Hacker's Guide to the Zend Engine
  13. The only thing I can think of is that maybe your rand() implementation sucks and you end up in a long loop generating several duplicates. You could try using mt_rand instead and see if that results in anything different.
  14. There demo is not working because the .js file (http://www.uploadify.com/uploadify31/jquery.uploadify.min.js) is missing. If you were just referencing their hosted JS file rather than downloading it and hosting it yourself, then that would explain why yours stopped working as well.
  15. I also just use plain PHP for template purposes. As far as control structures such as loops and conditionals go it's native format is not much different than any other template engine I've encountered, and is far more flexible if needed. I have a class and small collection of additional functions that I used to make certain things easier/shorter inside the template, such as: function sel($value, $options){ if (is_array($options)){ foreach ($options as $optval){ if ($value == $optval){ return 'selected="selected"'; } } } else if ($value == $options){ return 'selected="selected"'; } return ''; } Which would be used like so: <select name="state"> <?php foreach ($StateList as $state): ?> <option value="<?=$state['abbr']?>" <?=sel($state['abbr'], $Defaults['state'])?>><?=$state['name']?></option> <?php endforeach; ?> </select> The same thing in say smarty would look like this <select name="state"> {foreach from=$StateList item=state} <option value="{$state.abbr}" {if $state.name == $Defaults.state} selected="selected" {/if}>{$state.name}</option> {/foreach} </select> It's really not that much different from the plain PHP method.
  16. What your attempting to do cannot be done reliably with just PHP. The problem is there could be any number of buffers between your script and the client browser which you have no control over. Your browser itself could buffer the data before rendering it to the screen. Calling both flush and ob_flush after your output operations is pretty much about as good as you can do. If you do that and it still does not work then you must have some other buffer somewhere preventing it from working.
  17. Not sure what your asking there. I said you should be using prepare and bind for your queries if they are not static. Even if they are static queries you could still use prepare if you wanted.
  18. $iptc['2#025']; that index would be an array of all the keywords. You can use that array however you wish. Loop through it, implode it, whatever.
  19. Unless your query is static or does not depend on user input in any way, then you should be using prepare/execute and bound parameters as in your processQuery function. It will protect you from any injection attempts and make query handling easier. That said, your processQuery method is not going to work when you have multiple parameters/binds. All your fields will end up with the same value (the last value) being bound to them since your binding them all to the same $value variable. bindParam is assigns the variable as a reference so the value of the variable is not taken/used until your ->execute() call is made. You can either use bindValue which will assign the value normally rather than by reference, or make sure you use separate variables for each bind you do.
  20. This is on my desktop. 600000 Script took 3.4052720069885 sec and used 59.5 megabytes of memory This is on my VPS. 600000 Script took 4.3914389610291 sec and used 55.5 megabytes of memory And this is on my home server I built back in '03 600000 Script took 13.989145994186 sec and used 73.75 megabytes of memory With the code I posted above. If your server is taking that much longer or using a lot more memory sounds like perhaps your server is either underpowered or overloaded (my guess). Are you testing only this code or is there other stuff involved when your testing? I just have the above code in a file and run it via php-cli to test.
  21. No, what your proposing is less efficient and harder to use. Databases are designed to work with tables composed of many rows. Creating a new row per id is what it will do and handle best. Think about this, what happens when you decide you want to list out all the members of a group and their details (name, email, phone, whatever you have). With your method you have to: - Select the BLOB from the table - Use PHP to decode the ID's from the table - Run a second query to get the details for each user - Display the results If you do one row per user ID, then all you have to do is - Run a single query for the details - Display the results What if you decide you want to paginate the results because there are a lot of members? Your method becomes increasingly more complicated and harder to deal with, while one-row-per-user method only has to alter the query to include a LIMIT clause.
  22. Why are you trying to store multiple user id's in a blob? Just store one per row in a normal int column. As for ibase, it is a different database system. If your using mysql, you use it's functions not the ibase* functions. As for working with binary data, you'd use pack and unpack but I believe your approach is incorrect.
  23. I ran your code as well and didn't have any problems with a limit like your suggesting. I'm not sure if your code is really what your wanting though. Based on what I see in the code you want to generate 600,000 unique addresses, one for each 15 stars in each of the sectors. What your code does currently is generate 600,000 addresses but then after filtering out the duplicates you'll end up with less at the end. You won't have enough for one address per star. What you need to do if you want 600,000 unique addresses is keep looping until you get your 600,000 address. So you generate an address, check if it already exists and if not, add it to the array. Repeat until your 600,000 addresses are generated. Yes, you cannot have duplicate key names so by using the address as the key name you ensure you do not ever have a duplicate of that address. The value can be whatever you want it to be, doesn't necessarily have to be the address. This is some re-factored code based on what it seems like you want: <?php function stargate_address_check() { $map_x = 200; $map_y = 200; $stars_per_sector = 15; $star_amount = $map_x * $map_y * $stars_per_sector; $stargate_address_array = ''; $create_address_array = 0; for ($i=0; $i<$star_amount; $i++){ // creates a huge array of address's do { $address = stargate_address_generator(); } while (isset($stargate_address_array[$address])); $stargate_address_array[$address] = 1; } $count = count($stargate_address_array); return $count; } function stargate_address_generator() { $chevron1 = rand(1,38); $chevron2 = rand(1,38); while ($chevron1 == $chevron2) { $chevron2 = rand(1,38); } $chevron3 = rand(1,38); while ($chevron3 == $chevron1 || $chevron3 == $chevron2) { $chevron3 = rand(1,38); } $chevron4 = rand(1,38); while ($chevron4 == $chevron1 || $chevron4 == $chevron2 || $chevron4 == $chevron3) {$chevron4 = rand(1,38);} $chevron5 = rand(1,38); while ($chevron5 == $chevron1 || $chevron5 == $chevron2 || $chevron5 == $chevron3 || $chevron5 == $chevron4) {$chevron5 = rand(1,38); } $chevron6 = rand(1,38); while ($chevron6 == $chevron1 || $chevron6 == $chevron2 || $chevron6 == $chevron3 || $chevron6 == $chevron4 || $chevron6 == $chevron5) { $chevron6 = rand(1,38); } $stargate_address = "$chevron1-$chevron2-$chevron3-$chevron4-$chevron5-$chevron6"; return $stargate_address; } echo stargate_address_check();
  24. You can just reverse the quotes so you encase the php code in single quotes. php -r 'print_r(getimagesize("http://www.example.com/images!/logos/ps_logo2.png"));' Then bash won't interpret the ! as special character.
  25. So long as your using the same input then yes you will get the same hash as a return. If you have different hash values then you have different inputs and you need to debug why that is. var_dump() the values just before your md5 call in both places to ensure they are the same.
×
×
  • 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.