Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. As I stated to your first post, you are not providing good information. I took the time to answer as best I could based on the info given. To be honest I'm still not clear on what you want. What maxxd posted may be closer to what you want. Instead of copying the fields, I would have one set of fields on the left and one set on the right (that are hidden). Then, when the user clicks the copy button make the fields visible and populate with the values from the left.
  2. As you have posted no code and your request is not 100% clear I'm exactly sure what you need. But, here is a rough example based on what I think you are asking: https://jsfiddle.net/10t3gzb9/
  3. Not sure what you are really asking. You state "without cronjob" but then ask how would the "query be". First, create the logic. This would entail determining which users should get the notification. For example, you may want to exclude users that are disabled/inactive. You may want to send a notification to users if the jobs in question "match" the users on some predetermined data (industry, job type, etc.). Once you've made that determination, create the query to select the users' names and their email addresses. From the list of email addresses, create a process to generate the emails to include the name, email address, subject and body. Then send the email. There are plenty of email classes to simplify this for you. I would start with a plain-text email then modify it to an HTML version (if that's what you want to do). You can build and test the above by just pointing to a page to trigger the functionality. However, the last part would be to automate the process. Typically, this is what CRON jobs are for. If you are with a host, they should have the tools necessary to schedule such a job.
  4. Small modification suggestion to kicken's first code example. The code is 1/2 way there to modifying the current array data. Might as well go all the way - then no need to create an "$out" array to store the values. Just return the input array when done processing. function rename_keys($input, $from, $to) { foreach ($input as $key => $arr){ if (isset($arr[$from])){ $input[$key][$to] = $arr[$from]; unset($input[$key][$from]); } } return $input; }
  5. That is VERY useful information. I appreciate that you were trying to simplify the question, by not including "real" data or how the information is generated and used. But, those details can greatly change the response. In this case, I would probably create a function and have it dynamically change 'name' to 'label' if it exists in the data - regardless of what position it is in. That way, if the API ever changes the order of the fields your code would not break.
  6. I would fist ask why this needs to be done. The first array is being built somehow. It would be best to change the process of building that array to be in the required format rather than trying to reformat it later. You can create a new array with different keys using array_combine(), but it requires that the number of keys and values are the same. From your example, I would imagine the number of primary arrays (three in your example) could likely change. Although, I don't know that array_combine() would work on multi-dimensional arrays. You will likely need to run a loop over the parent array and change the child keys. But, not knowing what your logic will be, not sure what the best method would be. E.g. is it always a/b changing to c/b? Would the keys be dynamic? Etc.
  7. Why not this? DELETE FROM benchmarks WHERE id NOT IN ( SELECT timestamp FROM benchmarks ORDER BY timestamp DESC LIMIT 1 OFFSET 6 )
  8. Is the loop creating duplicate select lists of the same data? If so, you should call the process (I assume from a DB) to get the values for the select lists one time. Then, unless each list will have different default values, create the list options one-time. Then populate the list of options in each of the unique select lists. If each select list needs a different default value, then create the lists options independently from the data that was retrieved before the loop.
  9. The code you provided is HTML code, not PHP. HTML doesn't know how to use a variable and that variable would be set on the server anyway. Not to mention the variable (as you've presented it is defined after you want to use it). You need to have PHP code that generates that HTML. E.g. <?php $doc = "Darth.pdf"; ?> <object data="<?php echo $doc; ?>" type="application/pdf" width="950" height="800"> alt : <a href="<?php echo $doc; ?>">test.pdf</a> </object> Or <?php $doc = "Darth.pdf"; echo "<object data='{$doc}' type='application/pdf' width='950' height='800'>\n"; echo "alt : <a href='<?php echo $doc; ?>'>test.pdf</a>\n"; echo "</object>\n"; ?>
  10. As discussed previously, there should never be more than one 'active' hash for a user at a time, so the 'easiest' thing to do is to store it in the user table. You can still track the activity of requesting a password reset, resetting the password, etc. in a separate table. There's no reason the hash has to be in that table. In fact, there are likely activities you may want to track that have no hash as part of that activity. E.g. unsuccessful logins. So, I would keep the hash in the User table and just log the events in a separate table. That table might look something like this id | event_type_ID | userID | IP_address | timestamp The event_type_ID would be a foreign key back to a table with a defined list of events: Login Successful Login Failed (No matching User ID) Login Failed (Wrong Password) Password Reset Request Account Locked Login w/ Temp Password Password Changed etc. Those are only examples and would depend on the business requirements of your application. You could use the data to proactively prevent unauthorized access. FYI: The two "Login Failed" messages above would only be for internal use. You would never provide the user whether the User ID or password were incorrect.
  11. First, you need to decide what "is set" in the database means. Does it mean the field is NULL, an empty string, or what? Can the values you are pulling be FALSE? If not, have your function return the Boolean FALSE when the value is not set. Then you can do something like this: if(findit('DEGREETYPE','1moreinfo') !== false){ //Do this } else { //Do that } Are you actually using the value from the DB or just checking if it "exists" (whatever that means). If you are just checking if the value exists, then have the function return TRUE/FALSE. Also, the !== is only needed if any legitimate values would be interpreted as false. For example, a value of 0 or an empty string would be interpreted as false, do the double equal sign is needed to do a strict comparison. If you won't have any such values in the return data, you could simplify it even more such as this: if(findit('DEGREETYPE','1moreinfo')){ //Do this } else { //Do that }
  12. Right, the number_format() function is adding a comma as a thousands separator. Then, trying to add that formatted value to a number it is only adding the value before the comma.
  13. Really, just one space? The debug step that Muddy had you use shows exactly what the problem is, just look at ANY of the values returned: string(5) "CPU " It is a string with FIVE characters. There are probably TWO spaces at the end. You are probably only seeing one space because of how the browser renders multiple spaces. string(5) "KITH " This is also a string with five characters. This one is obvious that there is just one space at the end. You should fix your data (remove the spaces). Somewhere in your code you are setting these values in the DB and you are introducing those spaces. Fix that code and then fix the existing data.
  14. While you're at it, you might as well add multi-factor authentication.
  15. Did you try the code I provided? If I understand you correctly, the values you need should be generated correctly.
  16. Your question doesn't make sense. For example you state No, that's not a number, that's a string. Looking at your code, this is what I see: You have a running total calculated in the loop ($gr_total). Then the if/else code has two subtotals based on whether a record matches the condition or not. But, you are not outputting those values. Are you outputting them after the loop is complete? If so, how are the values different from what you expect? I would suggest creating some additional output for development/debugging purposes. You could then show that output and explain how it needs to change for your purposes. <?php $gr_total = 0; $cpu_total = 0; $kith_total = 0; $output = ''; while ($row = odbc_fetch_array($result)) { //Update totals $row_total = number_format($row['total'], 2); $gr_total = $gr_total + $row_total; if ($row['ompShippingMethodID'] == "CPU" ){ $cpu_total = $cpu_total + $row_total; } else { $kith_total = $kith_total + $row_total; } //Create output $output .= "<tr>\n"; $output .= "<td>{$row['ompShippingMethodID']}</td>\n"; $output .= "<td>{$row['UOMPTRUCKNUMBER']}</td>\n"; $output .= "<td>{$row['cmoName']}</td>\n"; $output .= "<td><a href='kf_order_by_id.php?id={$row['ompSalesOrderID']}'>{$row['ompSalesOrderID']}</td>\n"; $output .= "<td>{$row_total}</td>\n"; $output .= "<td>{$cpu_total}</td>\n"; $output .= "<td>{$kith_total}</td>\n"; $output .= "<td>{$gr_total}</td>\n"; $output .= "</tr>\n"; } ?> <table border="1"> <tr> <th>Shipping Method</th> <th>TRUCK NUMBER</th> <th>cmo Name</th> <th>Order ID</th> <th>Row Total</th> <th>CPU Total</th> <th>Kith Total</th> <th>Grand Total</th> </tr> <?php echo $output; ?> </table>
  17. You could pass the search criteria on the query string of the GET method E.g. GET /users?name=Joh
  18. The answer is "it depends". It depends on what you want to do. In Googles case they are apparently maintaining a history of the passwords used. I'm a little surprised by that, but it is not an uncommon process for enterprise level software that prevents users from reusing the last n number of passwords. If you do not plan on utilizing any feature where old passwords are needed then you don't need to maintain that data. The same logic applies to your question regarding the token - with one caveat. DELETE operations are expensive in terms of performance. If you don't need to delete a piece of data, then don't bother. Since the registration and reset processes will both use a token, that means that all users will need a token at some point. Plus, I would assume that no user would ever need two tokens concurrently. To keep things simple, you could simply have a token field in the "user" table. After someone has used a token, set the token field to an empty string or a NULL value (or setting the extirpation date to null or a prior date would work too). I would also expect that you also have some data in the table to know if a user has been registered or not: password confirmed field, registration Boolean, etc. So, when the token is used you could use that data to determine whether to run the registration logic or the password reset logic.
  19. Or you could move these directories to a location that is not publicly accessible and implement a simple "download script" that allows users to access the files. By using a download script you can also implement authentication/logging/ext to add additional security measures if warranted.
  20. What is the source of these arrays? Are you retrieving data from a database? If so, there will definitely be a better way to combine the data than combining arrays. Also, as others have said, the data has no context. In your example of: [32347] => 8 => 5 => 484 What does the 8, 5 and 484 represent? Besides, your example seems to indicate that you want each value to be a sub array of the preceding value. Perhaps it should look more like this [32347] => (8, 5, 484) Or this: [32347] => ( 'time' =>8, 'amount' =>5, 'foo' => 484 ) With your original data, here is a possible solution <?php $array1 = array(32347 => 8, 22188 => 3, 37493 => 4, 37201 => 7); $array2 = array( array( array(32347, 28470, 35319, 37493), array(5, 2, '', 4), array(484, 383, 315, 320) ) ); $newArray = array(); foreach($array1 as $key => $value) { $newArray[$key][] = $value; foreach($array2 as $subArray) { //extract the first array index (with the keys) $keyArray = array_shift($subArray); //Get the index for the key - if exists $keyIndex = array_search($key, $keyArray); //If the key exists, get the other values in the sub array if($keyIndex !== false) { foreach($subArray as $valuesArray) { if(isset($valuesArray[$keyIndex])) { $newArray[$key][] = $valuesArray[$keyIndex]; } } } } } echo "<pre>" . print_r($newArray, 1) . "</pre>"; ?> Output Array ( [32347] => Array ( [0] => 8 [1] => 5 [2] => 484 ) [22188] => Array ( [0] => 3 ) [37493] => Array ( [0] => 4 [1] => 4 [2] => 320 ) [37201] => Array ( [0] => 7 ) )
  21. You state Do you get those results on the same source and destination directories? Aside from your specific issue, I see some things that could be improved. $result = ($dir === false ? false : true); if ($result !== false) { The condition for setting $result will return a Boolean (true/false), so there is no need to create a ternary operator to assigne true/false. Plus, since $result will only be true/false, there is no need for the explicit type check on the if() condition. Additionally, the reuse of $result for many things throughout the logic could lead to confusion. But, more importantly, there is no error handling within the code when errors do occur. So, of course you don't know what the problem may be. One problem in your code that may be the source of the copy problem is the check in the if() condition for $result for the while loop condition. if (( $file != '.' ) && ( $file != '..' ) && $result) Because in the loop there is this: $result = copy($src . '/' . $file,$dst . '/' . $file); If that copy fails for any file it will skip all other files in the source directory because $result will never get reset (i.e. will be false for the rest of the execution). So, my guess is that there is a file that can't be copied for some reason (locked?) that is causing your problems. This is exactly what I was referring to above about reusing the same variable for different things. Here is a quick rewrite that fixes some problems and adds some error handling. This is not ready for production use (should never display system errors to the user), but it should give you an idea of what the problem may be static public function recurse_copy($src, $dst) { $dir = opendir($src); //Open the source directory if (!$dir) { echo "Unable to open source directory '{$src}'"; return false; } //Create the destination directory if (!is_dir($dst)) { if(!@mkdir($dst)) { echo "Unable to create destingation directory '{$dst}'"; return false; } } //Copy the files while(false !== ( $file = readdir($dir)) ) { if (( $file == '.' ) || ( $file == '..' )) { //Skip . and .. continue; } //Create variables instead of concatenating same strings multiple times $srcPath = $src.'/'.$file; $dstPath = $dst.'/'.$file; if ( is_dir($srcPath) ) { $result = self::recurse_copy($srcPath, $dstPath); } else { if(!copy($srcPath, $dstPath)) { echo "Error copying file '{$srcPath}'<br>\n"; } } set_time_limit(5); } closedir($dir); return true; }
  22. Right, but . . . you also have this public function getValidatedData($data) { if(!isset($data['city'])){throw new USERException("City field is missing.");} if(!$this->isACity($data['city'])){throw new USERException("$data[city] is not a valid city.");} // ... return ['city'=>$data['city'],'etc'=>123]; } The FIRST "error" will throw an exception and go strait to the catch condition. In the specific two conditions you gave in the example it is not a problem, because if the value is empty then there is no need to check if the value if valid. But, you follow that with the // ... which I assume to be other form validations. That is where the problem exists. Let's say there was another validation to check the state value. If a user submitted a form with no city and no state, the check for the city field would throw the exception and go to the catch logic. The logic would never validate the state field. What would result is you would provide the user an error message that the city field is required. They then enter a value and submit the form only to then be told that the state is required. Check form submissions for all such validations. Then if any fail, provide the user with a response that shows all the errors to be resolved. Example $errors = array(); $city = trim($_POST['city']); if($city == '') { $errors[] = "City is required"; } elseif(!$this->isACity($city)) { $errors[] = "The selected city is not valid"; } $state = trim($_POST['state']); if($state =='') { $errors[] = "State is required"; } elseif(!$this->isAState($state)) { $errors[] = "The selected state is not valid"; } if(!empty{$errors)) { //Provide all the errors to the user to resolve } else { //All form validations passed, process the data }
  23. And why is that? You should never run queries in loops as they are expensive from a performance aspect and can bring your site to a crawl based on the amount of data and usage. In this case, you should instead run ONE query for all the applicable user IDs in your list. You are already grouping by the user ID anyway. Plus, the currently query doesn't even use the user ID, so each iteration of the loop is apparently running the exact same query.
  24. Also, there is a usability problem in that code. Form validations should not stop at the first validation error found. It should check for all possible errors and then notify the user of all the problems they need to resolve.
×
×
  • 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.