ChenXiu
Members-
Posts
177 -
Joined
-
Last visited
Everything posted by ChenXiu
-
I go to GitHub and see a package I like. I download said package to my Ubuntu Server. How do I install it using Composer? NO, this is not a "I just discovered GitHub today and, by the way, what's Composer?" question. RATHER, this is YEARS of google-searching frustration, and YEARS of NEVER being able to figure out how the hell to use Composer, in spite of hours upon hours upon days upon weeks upon months of research. Whoever invented composer........... hmm. Sitting in my Ubuntu directory is an unzipped package I downloaded from Github. Lets call it "doghouse_master.api" Inside of doghouse_master.api is the README.md which has never made sense. There is also a composer.json in there. And there is a "lib" directory. I also have Composer installed on my Ubuntu server, so I can begin commandlines with the word "composer......." Would someone be kind enough to tell me what might be the next step? Thank you.
-
To internally retrieve only one record from a primary key table (never more than one record), is/are these the best (fastest) queries that will never generate a PHP error? $var = $db->query("select price from table where size = 'small'")->fetch_object()->price ?? ''; echo $var; -or- $var = $db->query("select price from table where size = 'small'")->fetch_assoc()["price"] ?? ''; echo $var; Thank you.
-
fill array with " " to avoid "undefined index" errors?
ChenXiu replied to ChenXiu's topic in PHP Coding Help
Yes! That's the post I tried to find! Thank you!! 😀 Regarding my original post... I know I conflagrated a couple things. Sometimes the arrays are actually missing the keys entirely, and sometimes when there are keys, the values are null. One of the cURL results I use presents arrays that are sometimes missing certain values I need, and sometimes are missing the entire key/value pair. The only way (up till now) to avoid all PHP errors on my end is to initialize every variable that I plan on using like this: $someVar = $curl_result["data"]["some_value"]["what_i_need"] ?? ""; $anotherVar = etc., etc., etc., And it ends up being a big long list of definining every variable to prevent PHP errors. And long lists in my code make it hard to write code because it makes me have to scroll through so many lines to look for things... Thank you, this "array_map" function seems like a very useful thing! I'm reading up on it now. -
I have a json_decoded array which has a key named "location." I want to duplicate this array, but with the location changed to lowercase (strtolower). Does one accomplish this by looping through? Or by using Array_Map? Here is the array: Array ( [data] => Array ( [0] => Array ( [description] => Tools [time] => 2021-07-27 GMT [location] => WEST LONDON // strtolower on location ) [1] => Array ( [description] => Foods [time] => 2021-08-04 GMT [location] => BIRMINGHAM // strtolower on location ) etc. etc. etc...... Thank you.
-
Sometimes I receive arrays from a 3rd party with NULL values. This causes "undefined index" errors on my end unless I check each and every value beforehand. (Checking each of those with "if !empty" gets really time consuming and makes my code messy when there's lots of values to check.) Someone on this forum posted a line of code that added " " to fill any NULL values found in arrays. I can't remember what they posted. I can't find that forum post, and I can't find the answer on google anywhere (and of course, I've tried every permutation of "array_fill" that I could think of). 1.) What's that line of code? 2.) Is doing it this way a good idea? Thank you.
-
JSON into CSV : most efficient for large files?
ChenXiu replied to ChenXiu's topic in PHP Coding Help
Thank you. I've done a lot of benchmarkes and indeed, the fastest speed I could reach was with exactly what you are suggested (using json_decode($result,true) and then looping through $data["content"] ). I thought there might be a way to save 500ms by "capturing" the "content" portion of the json string (using a string function while json is still a string) because string functions always are faster than looping. But maybe not.... I think monkeying with the json with string functions would open a pandoras box of errors 😃 -
If a json decoded array is huge (1,000,000+ lines) what is the fastest, least CPU intensive way to get it into a CSV? I only want the ["content"] portion into the .csv file: // $result = json_decode($jsonFile); print_r($result); stdClass Object ( [content] => Array ( [0] => stdClass Object ( [id] => 1234 [day] => monday [location] => USA ) [1] => stdClass Object ( [id] => 5678 [day] => thursday [location] => EU ) ) [searchCriteria] => stdClass Object ( [from] => monday [toDate] => friday ) [size] => 2 [number] => 0 ) What is the best way to loop to only get the "id" "day" and "location" from the ["content"] portion of the array in to the .csv file? $fp = fopen( 'csv_file.csv' , 'w' ); foreach( $result as $i ) { <------- this part. fputcsv( $fp , $i ); } fclose($fp); Thank you.
-
Ah yes! Now it works -- thank you!! It appears that by having a "flex-basis" / "min-width" pair of equal value gives the desired result. This is the first time I've seen an actual solution to this issue I've been trying to fix for about for 2+ years. About a year ago, I had 3 different "CSS experts" trying to solve this, and none of them could! I'm going to do a lot of experimenting with it today, just to make sure it really works 😃 Thank you again!!
-
Your idea sounded very interesting so I tried it, but it didn't work at all. I made the text part longer (I made the brown fox actually jump over the dog 😃 ) But when I shrank the browser, they both shrank at the same time (the <textarea> shrank while the ellipsis were appearing). I'll tinker with it -- at the very least I've learned something new. Thank you.
-
Nevermind. I figured it out. "sprintf()"
-
Thanks, but, yes, I know. So is there a way to "trigger" the textarea to start to shrink first... and THEN the ellipsis div will shrink? (I thought my 'illustration' clarified that.)
-
When a variable hasn't happened yet, what's the best way? To use a function? To use ob_start()? Or something else? if( $condition == true ){ $instruction = ' hello do this. '; } else { $instruction = " $variable_set_later "; } //code //code //now we set that variable $variable_set_later = 'hello do something else'; // // echo $instruction;
-
I have a <div style="text-overflow:ellipsis"> right next to a <textarea>. As expected, both the div and textarea shrink simultaneously as I shrink my browser. Question: How do I "hold off" ellipsises from occuring until the textarea reaches minimum width? Question (reworded): Let the Textarea shrink a bit first, before ellipsises start to occur. Instead of this: ------------------- The quick brown fox. | | Jumped over the dog. ------------------- ----------------- The quick brown fo . . | | Jumped over the do . ----------------- I would like this: ------------------- The quick brown fox. | | Jumped over the dog. ------------------- ---------------- The quick brown fox. | | Jumped over the dog. ---------------- ----------- The quick bro . . . . . | | Jumped over t. . . . . ----------- Is this possible? Thank you!!
-
Yep, you are right. An error only if it can't find it in any of the places it looks. It's somewhat disconcerting to know that PHP has to look for something in more than one place. Thank you! That sounds like the best idea. LOL -- I was on vacation when they taught "BASE_PATH" and "__DIR__ " and even "define." Seriously. I feel REALLY DUMB not knowing all that stuff. Just about ALL php code written these days has words like define, public, static, class, namespace, use, const, private, try, catch, etc., and I have NO clue what they mean or how to use them. It's a miracle my website with its 1990's php code is way faster, error-proof, and more efficient than over 90% of today's websites I visit. My biggie learning project today is to learn how to indent 😃
-
Good point. Thank you. Using Data Base 64, the statistics are fairly humble: image size: 16K gzipped page size (using "page info" in browser): 19K actual page size: 35K For me, using readfile() seems to be the way to go. Using this header in readfile, the image can have a tidy filename should a visitor want to right-click/save the image: header('Content-Disposition: attachment; filename="Whatever_I_Want"'); Further, should the actual image be missing, readfile can display an image of an error message: $imgData = getimagesize($complete_filename_and_path); if( !$imageData ) { $file_name = "image-of-an-error.png"; } Thank you again. p.s. (even though I solved it before you did 😃 )
-
I read that -- php checks the scripts own working directory, and then the current working directory before failing -- but I thought it would give an E_Warning if it had to look in more than two places? I have all errors turned on, but no warnings were issued. Very strange I think.
-
Why does the following scenario work: index.php is down in the public_html directory. index.php requires "secret.php" which is up in the root directory. How come putting these lines of code at the top of secret.php (which is up in the root directory) both work? <?php require 'secret_2.php'; <--- file alongside of secret.php (notice no dot-dot-slash ../) require 'index_2.php'; <--- file alongside of index.php back down in public_html directory (notice no dot-dot-slash ../) How does PHP know to look in the same directory, or jump down to public_html? Thanks.
-
Is there a line of code to put on top of files that require other files, so no matter where I move them I don't have to change the ../../ stuff?
-
6 days and counting...
-
I sure hope that helped answer your question. If you have any other questions about PHP, or what various functions do, please don't hesitate to ask me, I would be glad to help. I've been doing PHP a long time. But if there is anything I don't know, I'll tell you a little secret: I go onto php.net and look them up. I find it very helpful. PHP is a wonderful programming language. In fact, a lot of forums are built around PHP -- their content seems to build pretty fast, and that shows how much interest there is in the topic. Likewise, I hope I receive an answer to my question one day. I know it's pretty tricky. Anyway, have a wonderful day.
-
Which is better? Why and why not? An image is secured in the root directory, to be displayed to customer in html document. This image can be displayed as a link to a php readfile page The readfile page uses getimagesize("../rootdir/abc.png"), headers, and readfile() echo "<img src='READFILE.php?image_name=abcd.png'>"; -or- The image can be displayed directly to browser using file_get_contents("../rootdir/abc.png"), base64_encode(), and data:image;base64. echo '<img src="data:image;base64, ' .$img. ' " style="width:5in;height:5in;">'; Thank you.
-
Yes, I would be happy to. Readfile() just grabs the file you tell it to grab, and spits it out* as an image, text file, etc., per whatever headers you have set at the beginning of the code. *Technically, it writes the file data into PHP's "Output Buffer" (the memory blocks allocated to PHP) from whence it interfaces with webserver software. The user's web browser interprets the html and renders the file as an image, pdf, or whatever. file_get_contents() just grabs the file you tell it to, and reads the file into a string, rather than into the Output Buffer, like readfile() does. That is why for large files, some authoritative posters say "Readfile is better for large files." But those posters, with their great authority, omit important details like "how large," and "large, compared to what," leaving the rest of us to guess whether a single 72 pixel 4" X 6" image is large or not (probably not). Because file_get_contents() reads the file into a string, base64 has to be implemented and then you have to "tell that to your browser" via the "data:image;base64" implemented in the html <img src> code. I'm going to guess for my purposes (e.g. a single 72 pixel 4" X 6" image) either way is just fine. The reason I asked my question is that I am too much of a newbie to know • all the errors that could possibly happen • How to trap the errors if I don't know what errors might happen • Which is more error prone? Readfile, or file_get_contents? • browser compatibility? So, I really have given this a lot of thought. I am hoping you have some insight on this. Thank you.
-
An image is secured in the root directory, to be displayed to customer in html document. This image can be displayed as a link to a php readfile page The readfile page uses getimagesize("../rootdir/abc.png"), headers, and readfile() echo "<img src='READFILE.php?image_name=abcd.png'>"; -or- The image can be displayed directly to browser using file_get_contents("../rootdir/abc.png"), base64_encode(), and data:image;base64. echo '<img src="data:image;base64, ' .$img. ' " style="width:5in;height:5in;">'; Which is better? Why and why not? Thank you.
-
So circling back to my original question.... a while back it was suggested that when the visitor is finished posting and reposting data to a product page, the final page should be a "get request" (for various reasons including disallowing the back button and inadvertantly changing the final 'shopping cart'). To get from a post/repost page to a "header(location)" get request, the only way to carry over data is via sessions. (The only alternative is to have the final "header(location)" page be an ugly url and end in an order number "...example.com/order-complete?ordernumber=123456778" rather than a tidy url like "...example.com/complete.php") So I put the customer's order number into a Session Value. Then, on the final page, I do a mySQL query, select data from my table where orderid = $_SESSION["order_number"]. My problem has been generating the stupid image from my readfile page... I've already got the customer's order number in $_SESSION["order_number"]. I don't need to add yet another id autoincrement column to my mysql table because the table already has the 100% unique order number for the customer, where I can get all the relevant data to display the final "thank you your order is complete page." Currently, I have the image file named after the order number (using <img src=".../readfile_page.php?label=$_SESSION["order_number"] . ".png">) I'm trying to figure out what's wrong with just <img src=".../readfile_page.php"> because my readfile.php page can easily derive the order number from the session and display the image. So, TL:DR (haha too late) the "order number" is already unique (I don't need an id auto increment column in addition to all of that), and if sessions $_SESSION is good enough to carry data over from my post page to the GET request redirect, why all of a sudden not good enough to display an image to the customer while they're browser is open? (I'm not trying to argue -- I'm well aware my PHP knowledge is about 1.5 on a scale of 1 to 100 -- I'm just trying to find out if I missed the boat somewhere, even though I've implemented almost everything suggested here to me).
-
Sorry, I was just asking if by using sessions and avoiding all file names completely, it seemed to be even more secure. A potential hacker "viewing html source" might see <img src="id123.png"> and then be tempted to try "id124.png" "id125.png" "id126.png" but buy using sessions (which are 100% hidden), the hacker would only see <img src="mylabel"> in the html code. I always thought that in the back of my mind. It actually hurts to see someone say that out loud. Now I'm worried. That begs the question "when" and "how much" should one use sessions? I would assume the following factors as to "when" and "how much" someone uses sessions would be: 1. when the 'convenience value' outweighs the stability of the code 2. how badly will the code and flow be ruined if sessions go wrong 3. how important the session variables are (e.g. just to save a user's color preference red vs green) 4. you need an extra layer of security (e.g. if "order number 12345" is hard coded, making sure $_SESSION["orderid"] == '12345' matches the hard code") 5. when money is involved: if for whatever reason $_SESSION doesn't match the hard code, I would rather have the visitor leave rather than continue with a transaction where something went screwy somewhere.