Jump to content

StevenOliver

Members
  • Posts

    237
  • Joined

  • Last visited

  • Days Won

    6

Everything posted by StevenOliver

  1. Mac_gyver, I've implemented the auto_detect_line_endings config, and I'm not noticing any slowdown, so I'll use that from now on. The string replace "\r" with "\n" was working fast (even on big files), but then I'm assuming the line is ending with "\r". So auto_detect it is! Thank you!!
  2. Ginerjm, thank you, but I am provided with the .csv file, I don't create it myself. Also, what is wrong with the "!==false" ?? I got it from the examples in the manual. Are the examples wrong? Rather, are you saying the "!==false" is not necessary, and that "while ($data = fgetcsv($file)" is enough? I'll certainly try that, I'm all for efficiency. At my level though, it's never wise to monkey with what's prescribed in the manual... Mac_Gyver, ya, it was probably created on a Mac.... I guess if there are no elegant solutions (e.g. a hypothetical EOL argument to add to fgetcsv), then I'll just keep doing what I've been doing: put 3 lines of code at the top of my script (open file, replace all "\r" with "\n", put file back), and then run my script on the repaired file... Thank you all, again, for your replys.
  3. My .csv file has 3 columns and 10 rows However, the following only returns one row: $file = fopen('document.csv', 'r'); while (($data = fgetcsv($file)) !== FALSE) { $string .= $data[0].','.$data[1].','.$data[2]."\n"; } print $string; If I open the original .csv file in a text editor and replace all the invisible end-of-line "\r" with "\n" then the above code works fine. There must be an efficient way to implement this in my script, without having to remember to modify the original .csv file first.... Thank you in advance 😀
  4. Thank you all for your reply! Ginerjm, I'm provided an array that sometimes has "broken tools" and sometimes does not. I wanted to have just one "blanket response" to echo whether the array has Broken Tools or not (e.g. if absent, then "you have 0 broken tools"). Requinix, good point. The fact that I missed this shows I'm lacking a fundamental understanding about arrays. Often when I'm coding I'll realize I don't really know what I'm doing, and then I'll take an hour (usually several hours or a day or more) to read up and learn. Nowadays I'm realizing that if I just go for the "quick fix" I'll never really know what I'm doing haha Barand, Thank you -- as always, your coding is SO efficient clean and simple! As soon as I've corrected my mistake in my old code (and thus proven to myself I understand what I did wrong), I'll implement your code! Mac_Gyver, your code definitely cleans up my code. However, I want to echo a "blanket statement" saying there are no "Broken Tools" if the array I'm provided makes no mention of Broken Tools. Your code says "$items['tools']['broken']['quantity'] = 3" but sometimes the array I'm provided does not have "broken" in it. I'm trying to "initialize" the $items['tools']['broken']['quantity'] at zero '0' if $items['tools']['broken']['quantity'] does not exist to begin with. .... of course I could always do an "if Broken is not in the array at all" echo Blanket Statement One, otherwise echo Blanket Statement Two.... but that would be the quick fix and easy way out, and I wouldn't have learned anything :-)
  5. In this array, I have 3 broken tools at $10.00 each: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 [broken] => 3 [broken_price] => 5.00 ) ) foreach($tools as $i => $val) { echo 'We have '.$val["broken"].' broken tools at '.$val["broken_price"].' each"; } Sometimes I have no broken tools, then the array will look like this: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 ) ) If I were to run the loop now, it would give an "undefined index" error. However, when I define the index like this, it will give a "Cannot use a scalar value as an array" error: foreach($tools as $i => $val) { if(!isset($val["broken"])) { $val["broken"] = 0; } echo 'We have '.$val["broken"].' broken tools at '.$val["broken_price"].' each"; } Why? And what is the best way to handle this, please?
  6. Thank you both gw1500se and Requinix! The answer from gw1500se was the one I was looking for.... but it ended up not working -- didn't display anything but a blank screen, so maybe some configuration on my server is interfering... The answer from Requinix is important, too -- however, when I tried "print_r($_SERVER)" and just got a blank screen, I gave up..... until (several hours later) it dawned on me that I had to loop through nested arrays. This works for me here: foreach($_SERVER as $a => $b) { echo $a; echo "\n"; echo $b; echo "\n"; foreach ($b as $c=>$d) { echo "$c"; echo "\n"; echo "$d"; } }
  7. For debug purposes, I want to see all (each and every possible) variables from a website visitor, e.g. • their user agent (e.g. cURL) • the reqest string they used (e.g. "https://example.com/product.php?sku=12345") • their IP address • and everything else I haven't thought of I know how to save the info in a database, I might even have the info emailed to myself, I just can't remember the "one-liner" I used to use to capture this info. Thank you in advance!
  8. @kicken, Thank you sir! It works wonderfully!
  9. When using SSH to do a short simple mySQL select query (e.g. "select animal_name from table limit 4"), the result looks like this: | cat | | dog | | fish | | bird | Is there a way to disable the pipe "|" symbol so the result looks like this: cat dog fish bird Thank you.
  10. mac_gyver, thank you! Your answer both answers my question and brings up some good points (e.g. whether a submit button may or may not be set, depending on the browser, etc.). Thank you again!!
  11. To execute code on successfully submitting text input, is this "bare minimum" code secure enough? if(!empty($_POST["textfield_input"])) { //execute code } ...or is it best to make sure all 4 of these are confirmed: if ( ($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST["submit_button_name"] && isset($_POST["form_name"] && (!empty($_POST["textfield_input"])) ) { //execute code } The html portion is simply: <form name="form_name" method="post" action="somepage.php"> <input type = "text" name="textfield_input"> <input type="submit" name="submit_ button_name"> </form> I've searched on the net about this several times, and see different answers, and it looks like each PHP expert has their favorite.... but I would rather know the "best practices" answer to this. Thank you!!
  12. Thank you!! Sometimes it's hard to articulate exactly what I want... but here goes: If the order is finalized, I don't want customer hitting back button to create a new order or modify their order. Rather, I want them to start fresh from the beginning. The way I know that "order is finalized" I create $_SESSION["order_is_finalized"] and set it to TRUE. The way I know that Customer is "starting fresh from the beginning" is if they click the initial Start Button ( $_POST["SubmitButton"] ). Since all 4 versions work, I can't seem to figure out which is the most efficient. They all seem to work equally well. Unless you have a better idea, I'll use the "session conditional" version that you suggested.
  13. If order is finalized, I want Customer to start over from the "very beginning" if they want to create another order. The "very beginning" is the page that has the Submit Button. Without exception, all of these versions work. Which one is the best? (and which one is the worst? ?) 1.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } if(isset($_SESSION["order_is_finalized"])){ include('order_page.php'); exit; } -vs- 2.) if(isset($_SESSION["order_is_finalized"])){ if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } else { include('order_page.php'); exit; } } -vs- 3.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } elseif (isset($_SESSION["order_is_finalized"])) { include('order_page.php'); exit; } -vs- 4.) if(isset($_POST["SubmitButton"])) { $_SESSION["order_is_finalized"]=null; } else { if (isset($_SESSION["order_is_finalized"])) { include('order_page.php'); exit; } } Thank you!!
  14. Thank you! After trying to solve this for so long, I realize my real question all along is: "How can I share Javascript variables with PHP?" Your answer is perfect, hits the nail on the head. (And yes, the basic concept is kinda hard to wrap my head around... I "get it" for a few seconds, then sort of "accidentally forget"...... like paradoxes, schrodinger's cat, etc. :-) I did take your suggestion one step farther.... Because my code generates so many Javascript variables on the fly (up to 100 different quantities, prices, totals, and item numbers), I can't know ahead of time how many preset hidden inputs I would need. Therefore, I wrote Javascript code to create and write entire hidden input tags into a <DIV> using a "document.createElement" function triggered by "onKeyUp" (when Customer inputs quantities). That indeed worked! However, with all those hidden inputs being generated (sometimes 100's), I'm thinking it might be more safe & secure to use Sessions, with the "Redirect" suggestion made earlier in this thread. Then it would be one less Javascript function, and potentially 100's less hidden inputs :-) What do you think?
  15. Hmm...interesting. Before when I tried this, my browser kept showing the original URL. But your answer has been bothering me all day today -- I've been thinking, "How could Barand be wrong?" So I tried it again just now! Clean code, clean script, clean browser, WORKS PERFECT! - Session captures the Post variables - Second submit button triggers header:location - FINISHED.php page displayes all the variables - Browser correctly shows the new "FINISHED" URL! Thank you!! I hereby transfer all 5 of my reputation points to you! ?
  16. mac_gyver, here is a simplified version of what I am working with. Customer gets to alter quantities and see the updated total instantly without actually submitting (client-side Javascript). For a $5.00 item, changing the quantity from 1 to 3 and instantly seeing "$15.00" looks nifty. But since that $15.00 value is only displayed client-side, PHP sessions or hidden inputs won't recognize that new $15.00 value until posted. Up until now, the way my page works is when Customer clicks the "FINISHED" button, that same merchandise.php page posts to itself and displays "FInished! Thank you for your order!" But I'm now trying to have the "Finished! Thank you for your order" appear on its own "FINISHED.php" page. And, I can't get this to work because somehow I need to get all the form data that submits to itself, into the 2nd form which submits to the FINISHED.php page. <script> function update(){ var quantity = document.getElementById("quantity"); var total = document.getElementById("total"); total.value = (quantity.value * 5); } </script></head><body> <!-- THE ADD MORE STUFF / CHANGE QUANTITY FORM --> <!-- NOTE: Although Customer sees the inputs populated with numbers, PHP will not recognize these as variables until form is posted to itself --> <form id="form_1" method="post" action="merchandise.php"> <input id="quantity" name="quantity" value="<?=$_POST["quantity"]?>" type="text" onKeyUp="update();"> at $5.00 each = <input id="total" name="total" value="<?=$_POST["total"]?>" type="text"> dollars. <input type="submit" value="Add More Stuff"> <!-- submits to itself --> </form> <!-- NEW FORM: but the hidden inputs (and Sessions, if I had sessions) will be empty --> <form action="FINISHED.php" method="post"> <input type="submit" value="FINISHED"> <input type="hidden" name="q" value="<?=$_POST["quantity"]?>"> <input type="hidden" name="t" value="<?=$_POST["total"]?>"> </form> Thank you.
  17. mac_gyver, thank you -- I know what you saying. You're right, I'll post the code. Please give me a minute and I'll post what I mean.
  18. Barand, thank you. When you say Redirect, do you mean like this: header('Location: http://www.example.com/FINISHED.php'); I had it that way for a while, and it did work, but what bothered me was even though I was actually on the desired "FINISHED.php" page, my browser still showed the URL as the original "merchandise.php." Is there a way to fix this? Or is that pretty much my only choice (other than "hello year 2019 rewriting my code").
  19. ginerjm, hello, thank you for your reply! I think while you were replying, I was editing my post to add that I had already tried the new HTML5 "formaction" tag. Is that what you mean? It is really really nifty, I was like OMG it works.... but then when I read the documentation and did further testing, I found I would lose some of my customers with older browsers. So I'm still looking for a more universal solution (other than spending year 2019 rewriting my code haha)
  20. Info: When Customer clicks the "Add More Stuff" button on "/merchandise.php" to add more stuff, modify quantities, etc., the page submits to itself. Desired: I want a "FINISHED" button on the bottom of the page. When clicked on, Customer lands on a Thank You, "/FINISHED.php" page. Problem: The <form action="FINISHED.php"....> does not have access to the variables in the first form <form action="$_SERVER['PHP_SELF']">. Question: Using Sessions, javascript, or fancy PHP, is there a way the 2nd form can capture all the data from the first form, so when Customer clicks the FINISHED button, all the variables are sent to the FINISHED.php page? Like: function populate_second_form(){ on-Clicking-FINISHED-button { populate Finished-Form with all data from First-Form; Then-submit-all-this-data-to-Finished.php page; } } Reasons: There are too many PHP and Javascript variables created in the first form, many of which I don't understand, it would take me the rest of year 2019 to go line by line and rewrite all of the code. Requisite whining: Some code is beyond my skill level. For example, when the customer increases a quantity from "1" to "2" javascript will instantly change the displayed dollar-amount from $1.00 to $2.00 without actually "submitting/refreshing" the entire page. Though the displayed amount changes visually, the PHP $variable remains what it was until the page submits to itself. That means I cannot use $_SESSION to capture the newly-displayed amount until the page resubmits to itself. I've tried everything I can think of.... the closest solution I've found is the new HTML5 "formaction" tag, but I'm looking for a more universal solution (doesn't work in all browsers). Thank you!!
  21. mac_gyver, thank you for your reply. I think I understand! Are you saying it is best to completely eliminate <input type="hidden"> and instead use Sessions (or Databases)? So my simplified code would become: ----------------------------------- <?php session_start(); $_SESSION["added"] .= sanitize($_POST["added"]); // sanitize user input echo '<html><body>'; echo "Here is what's been added: ".$_SESSION["added"]; ?> <form method="post" action="<?=$SERVER['PHP_SELF']?>"> <input type="text" name="added"> </form> When finished, click here to create Packing Slip: <form method="post" action="finished.php"> <input type="submit" value="CREATE PACKING SLIP"> </form> </body></html> ----------------------------------- ... and the "finished.php" page would be as follows: <?php session_start(); print $_SESSION["added"]; Is that better?
  22. Customers visit a "select merchandise page." Their list of items is displayed (merchandise SKU numbers). The displayed list of their expands as they add more stuff. When they're done, they create a packing slip that appears on a new "finished.php" page. Question: Is using 2 forms and hidden inputs the secure and accepted way to do this? -------------------------------------------------------- <?php $added = $_POST["added"] . ',' . $_POST["more-stuff"]; echo "Here is what's been added: $added"; // item 1, item 2, item 3, etc. ?> <form method="post" action="<?=$SERVER['PHP_SELF']?>"> <input type="text" name="added"> <input type="hidden" name="more-stuff" value="<?=$added?>"> </form> When finished, click here to create Packing Slip: <form method="post" action="finished.php"> <input type="submit" value="CREATE PACKING SLIP"> <input type="hidden" name="finished" value="<?=$added?>"> </form> -------------------------------------------------------- Thank you!!
  23. " Thank you, I appreciate that you noticed this was just a temporary thing... so many files, and you're right I just needed a temporary fix. I gotta be careful though.... lots of my "temporary fixes" end up being permanent because I get too lazy to clean them all up :-)
  24. I have a directory in public WWW. The files throughout the directory have many PHP includes amongst themselves. For example, the file "registration.php" begins with require('../parameters.php'); The directory is complex, like this: /Public WWW directory/ /My_Directory parameters.php another_reg.php /app files.inc files2.inc /morefiles another.inc another2.inc /css mycss.css /pages config.php registration.php My question: I want the entire directory moved out of reach above the public WWW, but I want "registration.php" to go into the public WWW. How would I do that without breaking all the include paths? (I know the solution is probably a one-liner involving __DIR__ ... but I can't seem to code it so it works). Thank you!!
  25. NotionCommotion, thank you -- I appreciate it. I think I got confused in your post where you said $myArr=json_encode($result, true), but in my code it works the oppositie : $myArr=json_decode($result, true). You have "...encode" and I have "...decode." At first I thought you had a typo, but I've learned that some APIs respond with $result already encoded. For years I did everything I could to avoid learning how to use Arrays... but I'm finding nowadays almost everything I encounter uses arrays, and almost all the APIs use some sort of json encoded this or that..... I feel like I grew up never learning how to drive :-)
×
×
  • 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.