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. OMG! Barand, you nailed it! (pardon the pun). That works "right out of the box!" (pardon yet another pun) What worries me is that I am realizing there was (and is) NO WAY I could ever have figured this out on my own. There's nothing on the 'net like this. I don't know how you know this stuff.... but you have my undying respect! THANK YOU!!
  13. SaranacLake, I'll have to get back to you. I am reading and appreciating Barand's most recent answer. Thank you.
  14. SaranacLake, actually, I am trying to accomplish this in real life. You also ask "what happens" if I have blue pliers with 25 sku numbers? All the more reason to have it all on one line: Pliers, blue, 2222, 1111, 8888, 9999, 3292, 2992 And, you're right, usually there is a better way to communicate things.
  15. SaranacLake, thank you, but I just did. Here is the real-life problem I want to solve (and I went ahead and used plain English): I have a mySQL table with items, descriptions, and sku numbers: Hammer, red, 5555 Pliers, blue, 2222 Pliers, blue, 1111 Pliers, blue, 8888 Goggles, yellow, 6666 I want the result to look like this, with similar items (in this case, the blue colored pliers) grouped as follows: Hammer, red, 5555 Pliers, blue, 2222, 1111, 8888 Goggles, yellow, 6666
  16. My fruit table example was too simplistic (oops), may I please ask from a different angle: I have a mySQL table with items, descriptions, and sku numbers: Hammer, red, 5555 Pliers, blue, 2222 Pliers, blue, 1111 Pliers, blue, 8888 Goggles, yellow, 6666 I want similar items (in this case, the blue colored pliers) grouped like this: Hammer, red, 5555 Pliers, blue, 2222, 1111, 8888 Goggles, yellow, 6666 After several 12 hour days, I cannot figure out how to do this.....
  17. This will be my worst-ever question posted on this forum 😀 But here goes.... I have a mySQL script which retrieves data.... then my PHP script creates a text document out of it, and ends with: $output = ob_get_contents(); ob_end_clean(); file_put_contents("document.txt", $output); The resultant thousand-item list will have some of the items duplicated, with one or more numerals appended, like this: banana 1 apple 1 apple 1 and 2 lemon 1 blueberry 1 strawberry 1 strawberry 1 and 2 strawberry 1 and 2 and 3 melon 1 When an item is duplicated and suffixed with more than one numeral, I want only the latest duplicated item, like this: banana apple 1 and 2 lemon 1 blueberry 1 strawberry 1 and 2 and 3 melon 1 Is there a way to make "file_put_contents" go like "oh, an item was listed more than once, let's only list the latest ("modified") item"?
  18. Perfect! I see it now. I ended up following your advice and created an fgetcsv PHP script (which only took me an hour, not the 2 days I anticipated :-) Now instead of the hassle of opening the file in excel, copy-and-pasting into text editor, creating a mySQL lookup, formatting the data to paste back into Excel, etc., all I have to do is open up SSH and type "php my_new_script.php" and voila.
  19. Barand: Thank you (I like the "Hands up" analogy) 😀 I cannot change the table structure (my table has 2 columns "fruit" and "color"), and your table doesn't have colors, and I tried but couldn't figure out how to modify your suggestions. Benanamen: Yes, I can explain. I have an excel spreadsheet, I select the entire "fruit" column, open my text editor and quickly create a mySQL query "Select fruit,color from mytable where fruit = 'apple' or fruit = 'banana' or fruit = 'orange'.... 1000 more fruits." Then I copy-and-paste the mySQL result into excel and I can see which ones match. They never match because every 10 fruits or so, one is missing from my mySQL table... I know I could take a full 2 days off and hack out some PHP code to take a .csv file, parse it for fruit names, create an array, create PHP/mySQL script, etc., I just hoped there would be a way for mySQL to at least do this via SSH command line result using an ifnull or coalesce or some mysterious acronym.... | grape | purple | | apple | 0 ERROR NOT HERE | | banana | yellow | | pomegranite | 0 ERROR NOT HERE |
  20. What is the most efficient way to return results in the same order as I used in my Select Statement, and return "0" if no record is found. Example: "SELECT fruit,color from table where fruit = 'grape' or fruit = 'apple' or fruit = 'banana' or fruit = 'pomegranite'; Current Result: | banana | yellow | | grape | purple | Desired Result: | grape | purple | | apple | 0 | | banana | yellow | | pomegranite | 0 | Thank you.
  21. No -- no warnings. I created the tables with both "primary key col2 (col2)" and "primary key (col2)" and there were no warnings. Then I tried the "alter table" syntax after dropping the primary keys: "alter table Table1 add primary key col2 (col2)" -and- "alter table Table1 add primary key (col2)" ... and neither of those gave any warnings. For all intents and purposes, they seem 100% identical. Perhaps in the olden days the "...primary key col2 (col2)" was how it used to be done, then it got deprecated and now "...primary key (col2)" is how it's done today, with both commands having the same effect. What are your thoughts?
  22. show index from Table1; +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table1 | 0 | PRIMARY | 1 | col2 | A | 0 | NULL | NULL | | BTREE | | | +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.007 sec) show index from Table2; +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table2 | 0 | PRIMARY | 1 | col2 | A | 0 | NULL | NULL | | BTREE | | | +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.007 sec)
  23. I did -- I did "show index from Table1" and the result was identical to "show index from Table2."
  24. Hmm... I did that, the results are absolutely identical. I created the tables like this: Create table Table1( col1 varchar(3), col2 varchar(3), primary key col2 (col2) ); Create table Table2( col1 varchar(3), col2 varchar(3), primary key (col2) ); What would you think I should be seeing that I'm missing?
  25. Requinix, thank you!! That is exactly what I was looking for. (It actually talks about exactly what I'm asking -- what happens when you delete values, etc.). Thank you!! Regarding question 2... (good idea, by the way.... try both then describe the tables!).... So.... I just now created two tables, one with "...primary key (col3)" and the other with "...primary key col3 (col3)" but they both look the same, so I am still not quite sure what the difference is: describe Table1 +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | col1 | varchar(43) | YES | | NULL | | | col2 | varchar(3) | NO | PRI | NULL | | | col3 | varchar(3) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ describe Table2 +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | col1 | varchar(43) | YES | | NULL | | | col2 | varchar(3) | NO | PRI | NULL | | | col3 | varchar(3) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ I also ran the command "show index from Table1" and it was identical to Table2's index. Thank you.
×
×
  • 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.