Jump to content

kicken

Gurus
  • Posts

    4,045
  • Joined

  • Last visited

  • Days Won

    123

Community Answers

  1. kicken's post in Add event to an element created with javascript was marked as the answer   
    You need to use a delegated event which involves
    Adding the event to some parent element that will always exist and Adding a selector argument when calling the .on method. $('#tableDiv').on('click', '#edit', function(){ alert('Hi!'); });  
  2. kicken's post in include the script only once was marked as the answer   
    Make it a named function, then pass the name to your event handlers.
    function loadData() { $.ajax({ type: 'POST', url: 'returnPDO.php', dataType: "json", data: { id: "1", rows: "7" }, success: function (data) { ...CODE.... } }); } $("#searchTbl").keypress(loadData); $('#go').on('click', loadData);  
  3. kicken's post in fill array with " " to avoid "undefined index" errors? was marked as the answer   
    Ah, but have you tried array_fill_keys?  See also, this post: Notice: Undefined index: driving me insane.
  4. kicken's post in Block direct access to files but use them was marked as the answer   
    Some places will try and do that by checking the referrer and denying access if it doesn't exist or match the right domain.  It's not going to stop anyone with even the slightest determination from downloading your file though.   IMO, it's not worth the effort to even try and do something like that.  
    Basic rule of the web is if the browser needs access to the file, then by necessity the user can also download a copy of that file.
  5. kicken's post in echo an array inside form confirmation email was marked as the answer   
    Use a foreach loop above the $body and create a new variable containing the order details as a string, then use that variable in $body.
    $orderDetails = ''; foreach ($orders as $order){ $orderDetails .= $order . '\n'; } It seems your orders are JSON encoded, so you'll probably want to json_decode them and format them nicely within that foreach loop.
  6. kicken's post in Javascript postMessage(msg) with Instance Of Class w/Methods was marked as the answer   
    postMessage serializes the data that you want to send.  That means that for the most part, only simple data is able to be sent, not complex stuff like functions or objects with methods.
     
    What you need to do is define MyClass on both pages and give it a way to be serialized to a string or simple object on the sending page and then unserialized back into the full object of the receiving page.
     
  7. kicken's post in results are sorted by directory name and not date was marked as the answer   
    Use usort rather than sort, then you can provide a comparison function and implement whatever logic you need to get the correct sorting.  The comparison function will be given two items from the array ($a and $b) and must return a number that indicates if $a is less than $b (-1), they are equal (0), or $a is greater than $b (1).
    If your files all began with the date in Y-m-d format you'd be able to simply compare them.  In m-d-Y format though you need to parse the date out of the name and then compare the date.
    usort($files, function($a, $b){ $a = basename($a); $b = basename($b); $aDate = DateTime::createFromFormat('!m-d-Y+', $a); $bDate = DateTime::createFromFormat('!m-d-Y+', $b); if ($aDate && $bDate){ $comparison = $aDate <=> $bDate; if ($comparison === 0){ return $a <=> $b; } else { return $comparison; } } else { return $a <=> $b; } }); Here, the function reduces each path given to it's basename, then attempts to parse a date in m-d-Y format from the beginning of the filename.  If it's able to successfully parse a date from both filenames then it compares the dates to get that -1, 0, or 1 value indicating their order.  If the result is 0, meaning the dates are equal, it compares the entire string and returns that order.  If it's unable to parse a date from either of the filenames then it just returns an order based on a string comparison.
     
  8. kicken's post in CORS and Sandboxing User Javascript, and Cookies was marked as the answer   
    I downloaded your code and added that attribute and it seemed to be working fine after that.
    You need to add it to both the CSP and the iframe's sandbox attribute.
  9. kicken's post in Errors that make no sense to me. What am I doing wrong? was marked as the answer   
    A query can have more than one parameter (eg, multiple WHERE conditions), as such PDO::execute() needs to be able to accept more than one value to bind to those parameters.  The way it does that is by taking in an array of values (even if it only needs one).  Pass an array to execute with $site as an element of that array to fix that problem.
    $stmt->execute([$site]); Your foreach error is due to the variable you provide ($table) not being an array (or other Traversable object).  You don't actually define $table anywhere in your code, it just magically appears.  If you want to loop over the results of your query, then you have a couple options.
    PDO Statements can be used directly in foreach, so just loop over $stmt.  If you do this, you do not call $stmt->fetchAll() first. Grab the results using $stmt->fetchAll() and loop over that variable ($fcid in your code).  
  10. kicken's post in Does a class's constructor run when I call a static method from outside the class? was marked as the answer   
    No.  Constructors only run when you're creating an instance of an object using new.
    Static methods are not associated with an instance so there's no need to run a constructor.  You also cannot use $this or any non-static class properties or methods
  11. kicken's post in Various Issues with backslashes and replacing strings was marked as the answer   
    This is just a matter of two separate levels of escape sequence processing that you need to wrap your mind around, which can be difficult at times.
    When you're setting a string in PHP first you have PHP's escape sequence processing.  PCRE then has it's own level of processing that is done on the value that was passed to the function.  For example, if you wanted to use \0 in a replacement literally rather than have it interpreted as a back reference you have to pass the string '\\0' as your replacement.    If you're defining value in your PHP source as a string then you need to escape those slashes again for PHP's sake so you have $replacement = "\\\\0"
    If you get the value from a file or database you don't have to worry about the PHP level of escaping, but do still need to account for the PCRE level so you need your file to contain \\0 not just \0.
    It's not clear to me exactly what output you're expecting in your code sample.  The addslashes call effectively mitigates PHP's escaping meaning $replacement is set to the literal value "<pre>\\\\</pre>".  preg_replace will then see that and process it's own escaping which means the value it's working with is effectively "<pre>\\</pre>".  That means your final replaced output would be "<div><pre>\\</pre></div>"
    If you have "<pre>\\\\</pre>" stored in your database and are pulling that value from there then you should get the same result, just don't run it through addslashes() as you don't have to deal with the PHP level of escaping things.
     
  12. kicken's post in syntax error detection was marked as the answer   
    Your host probably re-configured things when it upgrade PHP. In the past many shared hosting providers I used would have things configured with display_errors=On but that's not an ideal configuration for a production server as it exposes too much. Your host was probably setup like that previously and when they upgraded finally changed it to the better display_errors=Off with logging setup instead.
     
    The old configuration would let you see the parse errors because of the PHP configuration, not your script specific settings. The new configuration doesn't show the errors, and your script can't override that if it can't be parsed.
     
    So your options now are
    Re-configure PHP to show errors again or Start checking the error log file when you get a blank screen or Re-work your application to use a main front-end file that sets your error reporting then require's the rest of your code.
  13. kicken's post in Can you see anything dodgy in this code? was marked as the answer   
    It sounds like you have your referrer id value coming in from potentially different sources. What you need to do is decide on the priority of those sources and then check for a value in that order.

    $referrerId = $default_referrer; if (isset($_GET['id'])){ $referrerId = $_GET['id']; } else if (isset($_COOKIE['referrer'])){ $referrerId = $_COOKIE['referrer']; } That will assign $referrerId the value from $_GET['id'] if it exists, otherwise $_COOKIE['referrer'] if it exists, and if neither of those exist fall back to $default_referrer. 
    After that, you know your referrer so you just do whatever you need to do with it.

    //Set the cookie for future page loads. $duration = 60 * 60 * 24 * 365; setcookie('referrer', $referrerId, time() + $duration); //Grab the user's details $userInfo = get_userdata($referrerId); //Do whatever with $userInfo
  14. kicken's post in is this the best way to access a multi array was marked as the answer   
    I'd guess something like this would work for you.

    foreach ($image as $img){ $imageID = wp_get_attachment_image($img['image'], 'full'); echo $imageID; } In your original code you would run wp_get_attachment_image twice, once for the [image] key and once for the [image_fallback] key which is probably not what you intended to do. If you only want the [image] key then just access it directly.
  15. kicken's post in Unexpectet String causes me cancer was marked as the answer   
    You need commas separating your fields in your app definition.

    var app = { shipping : 5.00, products : [ { "name" : "2.4GHz GPS WIFI Drone", "price" : "164.99", "img" : "IMG/COV/SHOP/IMG/REG/2017.10.2815.17.5925474Drone_SML.PNG", "desc" : "Flugzeit: 12 - 15 Minuten Ladezeit: 3- 4h", "pid" : "38", "qua" : "1" } //... However, the better way to do something like that is to use json_encode
    <?php $cart = []; if (isset($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $row) { if ($row > 0){ $cur_pro_on_car = $Shop->product_get($row); $quantity = 1; if (in_array($cur_pro_on_car['PID'], array_keys($_SESSION['quant']))) { $quantity = $crud->word($_SESSION['quant'][$cur_pro_on_car['PID']], 'UTF-8'); } $cart[] = [ 'name' => $crud->word($cur_pro_on_car['prdname'], 'UTF-8') , 'price' => $crud->word($cur_pro_on_car['price'] / 100, 'UTF-8') , 'img' => $crud->word($cur_pro_on_car['prdpicsml'], 'UTF-8') , 'desc' => $crud->word($cur_pro_on_car['detailsstr'], 'UTF-8') , 'pid' => $crud->word($cur_pro_on_car['PID'], 'UTF-8') , 'qua' => $quantity ]; } } } ?> var app = { shipping : 5.00, products : <?php echo htmlspecialchars(json_encode($cart)); ?>, //... };
  16. kicken's post in decbin() - Is there an upper limit? was marked as the answer   
    decbin's range depends on whether you are on a 32-bit php version or a 64-bit php version. Your input is fine for both, but converting that binary value back to decimal is the problem.
     
    decbin(3584) = "111000000000"
    (int)"111000000000" = 111,000,000,000
     
    On a 32-bit system that decimal value is too high so you end up with 2,147,483,647 (the maximum value for an integer) when doing % 10.
     
    Dividing by 100 gets you low enough to fit within the 32-bit range so you stop having issues at that point.
     
    What sort of problem are you actually trying to solve? It looks like you want to test if certain bits are set in a number. If that is the case, then you want to be using the bitwise operators, not decbin.
     

    function teams($number){ if($number & 0x01) {echo " <font color=green>ARENA</font> ";} //1 if($number & 0x02) {echo " <font color=maroon>FLEET</font> ";} //2 if($number & 0x04) {echo " <font color=yellow>FLEET RESERVE</font>";} //4 if($number & 0x08) {echo " <font color=gold>FLEET COMMANDER</font>";} //8 if($number & 0x10) {echo " <font color=fuchsia>RANCOR</font> ";} //16 if($number & 0x20) {echo " <font color=darkblue>AAT</font> ";} //32 if($number & 0x40) {echo " <font color=tomato>SITH</font> ";} //64 if($number & 0x80) {echo " <font color=blue>LSTB(1)</font> ";} //128 if($number & 0x0100) {echo " <font color=blue>LSTB(2)</font> ";} //256 if($number & 0x0200) {echo " <font color=blue>LSTB(3)</font> ";} //512 if($number & 0x0400) {echo " <font color=blue>LSTB(4)</font> ";} //1024 if($number & 0x0800) {echo " <font color=blue>LSTB(5)</font> ";} //2048 if($number & 0x1000) {echo " <font color=blue>LSTB(6)</font> ";} //4096 }
  17. kicken's post in Escaping the @ sign was marked as the answer   
    You can use explode to separate the email into two parts and run your checks on the second part.
     
    Whatever you do though, do not auto-correct the users email address. If you think they may have made a typo you can prompt them "Hey, did you mean <whatever>?" but always let them push their email through unchanged.
     
    I've encountered a few places that thought my email address should have been @aol.com instead of what I entered. Had they automatically fixed it they would have ended up with an incorrect email and probably inadvertently locked me out of my account.
  18. kicken's post in how to prevent a class to be instantiated? was marked as the answer   
    The info you're looking for would be found by looking up the singleton class pattern. In this design a class is only constructed once by forcing the construction to be done by a static method of the class.
     
    You can prevent external code from constructing instances of your class by making your __construct method private.
  19. kicken's post in passing an array though anchor link was marked as the answer   
    I'd just implode the array with a separator, such as a comma or dash or whatever else. Then explode it on the next page to get your array back.
     
    You could also form a URL with multiple parameters using PHP's parameter array syntax so your URL would look like:

    http://mysite.com/all-projects/?var[]=944&var[]=17&var[]=19&var[]=1310 There is a function for this built into PHP.
  20. kicken's post in getting file names/sizes from directories/sub-directories was marked as the answer   
    if(is_dir("$resource_dir$entrydir"))
    {
    $retvaldir[] = array( "dir_name" => "$entrydir", "dir_size" => disk_total_space("$resource_dir$entrydir"));
    $retvaldir = array_merge($retvaldir, getFileListDir("$resource_dir$entrydir"));
    }
    That adds your current directory entry to the list, then appends all the entries found by the recursive call to the list.
  21. kicken's post in Alternating row styles not working... was marked as the answer   
    You appear to be wrapping every row in a (incomplete) <tbody> tag.  That means every row is a first-child (in other words row 1 / odd).
  22. kicken's post in Set array values and preserve keys was marked as the answer   
    Do you want to keep the keys, but reset all their values to empty array's? Not really sure what you're asking/trying to do.
     
    If you just want the keys + empty array values then you could just do this:

    $arr2 = array_fill_keys(array_keys($arr), []);
  23. kicken's post in Dice Roller worked in PHP5, but won't in PHP7 environment was marked as the answer   
    From the manual page for eregi:
    You need to update your code to the alternative.
     
    Or preferably, use a library for your mailing needs such as PHPMailer or SwiftMailer which will properly handle this task.
  24. kicken's post in why 'i' means minuites in PHP date format? was marked as the answer   
    If the first letter of a word can't be used as a shortcut because it's taken by something else, most people just move on to the second, then third, etc until one is available.
     
    So, minutes.
  25. kicken's post in array_filter not === with $varible only with text entry was marked as the answer   
    You're not showing $variphone being defined there anywhere. If it's not defined within your function then it won't exist. PHP doesn't inherit variables from outer scopes automatically like some other languages. You can import variables you need using a use statement, as demonstrated in ignace's example.
×
×
  • 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.