ChenXiu
Members-
Posts
177 -
Joined
-
Last visited
Everything posted by ChenXiu
-
ALMOST DONE WITH MY WEBSITE! LAST HURDLE AS FOLLOWS (then I won't bother you guys no more hehe) Visitors add SKU numbers to my website in GROUPS AT A TIME to see prices like this SKU# XYZ = $10.00 Then they add another couple SKUs, like this: SKU# ABC, SKU# PQR........ and then they see this: SKU# XYZ = $10.00 SKU# ABC = $20.00 SKU# PQR = $5.00 .... and when they're done they put in their name and address and voila! They have a packing slip. All the data they enter is contained in a PHP $_POST variable using <input type="hidden" name="sku_numbers" value="implode($posted_sku_number__array)"> and then reposted as they keep adding more stuff. Everything's fine unless midstream if they decide to click a referral link to add a referred sku number (https://myWebsite/link.php?Referer=RefererCompany&SKU=XYZ) Obviously, if they decide to click the link, it's a $_GET request, and all my $_POST data vanishes. 😡 So I implemented Sessions, and now my website saves $_POST inside of $_SESSION, and then if a GET request is made, $_POST is repopulated from $_SESSION. Like this: if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION["post"] = $_POST; } if(isset($_GET)) { if(isset($_SESSION["post"])) { $_POST = $_SESSION["post"]; } } My website now works, but I don't like it. I don't like sessions, I don't like 1000 lines of code to make sessions work, and unless I have session_unset() in all the right places, and 1% of the time there's the odd unexpected result.....like if a visitor completes an order with 50 SKUs, then starts over with just 1 sku, the pricing page will still show those last 50 skus because I forgot to unset one of the $_SESSION variables... QUESTION: All this get/post/session nonsense could be AVOIDED if that referral link thingy could be turned into a POST request instead of a GET request so I don't lose all my $_POST data. CAN SUCH A THING BE DONE: TURN A REFERRAL GET LINK INTO A POST AS DESCRIBED ABOVE? Thank you in advance.
-
Thank you! That was my next step -- a string function! Especially since string-style functions are much faster than preg_match. It's funny, I had been thinking "just this one time I'll use preg_match in my coding to save time," and now it's days later, 3 days wasted (I thought preg_match "capture this group as long as it doesn't contain" would be easy, but all the answers I have found are as convoluted as a furniture assembly manual haha.) Thank you again!!
-
The input $string will only have one parenthesis with the word "required" in it (it will never show up more than once). The very best I could come up with is the following disgustingly obtuse code: \(((?![\(]).)*required\) I will not use this code because: 1.) it makes 2 capture groups, which is stupid. 2.) it is disgustingly obtuse. There must be an elegant one liner a pro, such as yourself, would use 😀
-
Desired: Use preg_match to capture only the parentheses containing the word "required." Example: $string = 'Large toolbox (metal) for sale (hammer is required) serious inquiries only.'; Desired: (hammer is required) $string = 'Meeting scheduled for Tuesday (Formal attire required) otherwise call (or email) us.'; Desired: (Formal attire required) I would be happy to share the 3-days worth of experiments I tried, but posting said experiments would not be pretty, and, most importantly, would show my IQ. Thank you in advance!
-
Oooops. I totally goofed up. Please ignore the above question. Here is what I did: I ran the "create table..." command, but kept getting frustrated that the "price" column didn't change. "select price from....." why the heck won't it change!?!?!? Duh! OF COURSE the price column doesn't change -- it's the "MAX(price)" column that gets the updated price. This is such a stupid mistake I made. 😀
-
Greetings! This one makes my head spin 😀 Scenario: 2 existing mySQL tables of merchandise lists with Primary Key (SKU_number) Note: Both tables often have identical SKU numbers. Desired: New table combining the 2 tables, Primary Key (SKU_number) If the 2 existing mySQL tables contain the same SKU_number, the new Table should have highest price. Example: existing tableONE; +-------------+-------------+-------+ | SKU_number | description | price | +-------------+-------------+-------+ | 222222 | big hammer | 13.00 | | 444444 | good knife | 10.00 | | 666666 | small screw | 8.00 | | 888888 | bolt | 2.00 | +-------------+-------------+-------+ existing tableTWO; +-------------+--------------+-------+ | SKU_number | description | price | +-------------+--------------+-------+ | 111111 | saw | 7.00 | | 222222 | large hammer | 20.00 | | 444444 | good knife | 10.00 | | 666666 | tiny screw | 5.00 | +-------------+--------------+-------+ Brand new master table of both existing tables, and will have: • MAX price, when same SKU_number is in both tables • Product Description from tableONE, when same SKU_number is in both tables +-------------+--------------+-------+ | SKU_number | description | price | +-------------+--------------+-------+ | 111111 | saw | 7.00 | | 222222 | big hammer | 20.00 | | 444444 | good knife | 10.00 | | 666666 | small screw | 8.00 | | 888888 | bolt | 2.00 | +-------------+--------------+-------+ Here is the code I thought had been working, but just now discovered it didn't work when I swapped the table order 😀 create table MaxMaster as SELECT * , MAX(price) FROM ( SELECT * FROM tableTWO UNION ALL SELECT * FROM tableONE ) both_tables GROUP BY SKU_number; Thank you!
-
Oooooh! That is fantastic! Like I've said lots before, I wish I had learned PHP from the ground up. This sounds like something every brand new PHP person already knows. So, yes, a "do loop" is exactly what I want. Tangentially, when I was "borrowing code" from an example I read somewhere for something else, they had a "try/catch" thing going on. I immediately got rid of the "try" and "catch" thinking "I don't need those stupid things whatever they are..." So, after I get comfortable using "do loops" I should brush up on what try/catch means and does 😃 As well as what 'break' 'continue' and 'next' actually mean (in my code above, using "break" was just a lucky guess 😊 ) p.s. I should have mentioned before... your use of time() in the code is fantastic -- that never would have occured to me. I thought a $counter + sleep() was the only way to do this sort of thing. Right now I can already think of a dozen places where I can use time() in a loop.
-
Thank you! I've never used a "do" loop before; this will be my first one. Tomorrow morning I'm setting aside time to learn about them, pros and cons, and when to use. It looks elegant!
-
No, you misunderstood. The 3rd party data provider can take up to one minute depending on how many customers are simultaneously using their service. The 3rd party provider produces results in about 1/4 second if 10 people are using their service. If 200 people are using their service, you're in a queue. When they provide the data is when they provide the data. Is my mySQL code okay?
-
Data is supposed to be instantly inserted into mySQL table from a 3rd party. However, sometimes it takes up to a minute or so.... Is this "keep checking for up to a minute" loop okay? If not, how can I make it better? $count = 0; $query = $db->query("select data from mytable where sku = 'XYZ123' limit 1")->fetch_assoc(); // if no instant result, keep trying for up to one minute: while (!$query) { $query = $db->query("select data from mytable where sku = 'XYZ123' limit 1")->fetch_assoc(); sleep(1); // wait for one second before trying again $count++; if($count > 60) { break; } // Give up after one minute. } if (!$query) { echo 'Forget it, no data will be provided!'; // Give up. } else { echo 'Success! Data is ' . $query["data"]; // Success! } Thank you.
-
That sounds like a great idea, thank you! I'll search for one now.
-
p.s. in my real scripts, I have: $result = $db->query($query); while ($row = $result->fetch_assoc()) { ... } (otherwise none of my mySQL queries would work) But when I type my question here, I goofed it up. I'm noticing the following: I have at least 20 new coding ideas every single day. Whenever I try them out, they *always* generate some sort of an error. ... and then I have to go fix the error. Each time. Looking back this entire year, I think every single block of code I've written has generated some sort of an error which I have to go fix. ... hopefully this gets better 😃 I feel like a first grader in english class.... "The dog jumped over the fense." - oops.... "fence" "The three bares liked porridge" - oops... "bears" while($row = $result = fetch_assoc()....
-
Thank you! Yes you are correct. I botched that one. And this is super basic stuff I should already know. Sometimes I wish I was in an actual (physical, not online) PHP classroom, and was made to learn PHP from the ground up, starting all the way from the beginning. There are so many rock-bottom absolute-basic things I have no clue about. One day, hopefully 😃
-
Ha! You're onto me! No one liners. 😃 Something like this? $listings = ''; $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { if(!isset($firstrow)) { $firstrow = $row["item"]; $listings = "The first row is $firstrow<br><br>"; } $listings .= $row["item"].'<br>'; } echo $listings;
-
This produces 10 results: $query = " select item from table order by date asc limit 10 "; while($row = $db->query($query)->fetch_assoc()) { echo $row["item"]; } Can PHP extract the first of the 10 results without running a duplicate query with "limit 1" in it? (I already tried things like echo $row[0] and echo $row[1] and print_r($row)). Thank you.
-
Thank you! That's perfect! I don't think I would have figured that one out... Whenever extracting values requires looping through an array using its own keys (e.g. "$array[$key_from_line_above]"), it always throws me for a loop (pardon the pun 😃 ).
-
$matches is an array as follows: Array ( [0] => Array ( [0] => sku234567 the price equals $33.00 [1] => sku999888 the price equals $82.44 ) [1] => Array ( [0] => 234567 [1] => 999888 ) [2] => Array ( [0] => $33.00 [1] => $82.44 ) ) Using PHP, how do I end up with this: 234567 is $33.00 999888 is $82.44 I have tried just about everything, like echo "$matches[0] is $matches[2]" and $matches[1][1][2] and foreach loops..... I know I can access $matches[1] using a foreach loop, but then how do I access its corresponding value from $matches[2] ? Thank you.
-
There is no loop, I just have three lines of code that do this: file_get_contents("php://input") ---> Sanitize ---> then insert into mySQL database ---> exit. My computer browser has an html/mysql page that queries the database for the results I'm waiting for. I refresh the browser while patiently waiting. There are 100s of records -- whenever I query 3rd party server and they post results to my endpoint, my mySQL database increases in size. At first, that's what I had done, but this browser page is the same one I use to query the 3rd party for new data. If this browser page is auto refreshing to display the results previous request, I can't be using this browser page to be posting my next request. The more I think about this, the more the logic of it all fades away... 1.) There are 100's of records that are slowly building to the 1000's of records. I only want my most recent request showing up. 2.) How can I continue using the page to post data to query the 3rd party if all of sudden my previous request results commandeers the page... My computer monitor is only so big.... I already have 4 windows open to do what I need to do. It sounds like I'm going to need a 5th browser window open that will say "ChenXiu, Your Results Are Ready." I wish I could think out of the box on this one... I'm sure there is an elegant way of accomplishing this.
-
1.) My query script requests data from 3rd party. 2.) The 3rd party processes request, and about a minute or so later, pushes results to my server. 3.) My PHP code uses file_get_contents("php://input") to capture and insert that data into my mySQL table. 4.) I sit at my computer, waiting up to 2 minutes, constantly refreshing my browser's mySQL page to see if mySQL has results yet. To avoid having to sit at my computer refreshing my page like a monkey pressing the "give me a banana" button, I have a question. Question: Without having to learn an install one of today's complex Python/Java/Javascript technologies, is there a simple way to have my browser simply display that data when it's ready? With my limited knowledge, I'm experimenting with SSE, but it looks like SSE polls actual files, for data that's been written. Somehow there might be a way using ajax to have a 2nd page with an infinite loop where mySQL is queried every 5 seconds, and when $db->query is "TRUE" then it will a.) writes the data to a file using file_put_contents and b.) break out of the mySQL loop. And when the file gets written using file_put_contents, SSE will be polling that file and delivering the results. Am I going about this wrong? Currently I am at the "thinking up an idea how to do this" stage. And before I spend 6 weeks trying to write the code, I thought I'd check to see if there isn't already a "one-liner" that accomplishes all of this 😃
-
When logged into my shared server, I usually see only my own username logged in when I type the SSH command "> who" For example, I normally see: > who; ChenXiu pts/1 2021-09-02 09:55 (ip addr) However, I sometimes other users, like this: > who; ChenXiu pts/1 2021-09-02 09:55 (ip addr) AnotherName pts/2 2021-09-02 09:55 (ip addr) SomeoneElse pts/2 2021-09-02 09:55 (ip addr) I'm assuming this is normal? I tested this on other shared server hostings that I use (using commands like "who" and " last -i " Should this be alarming? Thank you.
-
Thank you, this will help! The main problem I have is I have a javascript window.open -- the window opens and it flashes the error for a nanosecond and then reloads with correct html data. So I need to figure out what that error message was that only flashed for a nanosecond. Regarding my error reporting code (e.g. "ini_set('display_errors', '1');"), I don't know how (or where) to put the "exit;" in there.
-
If my page has a PHP error, I want to immediately exit with the error displayed in my browser. Because I'm on a shared server (no access to php.ini file, and NO error logs!) I have to have this on top of my page: ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); However, I also need PHP to exit with the error message displayed. (The reason is some of the scripts reload to another page and I only see a milisecond flash of error message.) Thank you.
-
Thank you. At first I had this: $var = $db->query("select price from table where size = 'small'")->fetch_object()->price ?? ''; But a PHP error would be generated if the "price" value was missing from the mySQL table. But when I do this: $begin = $db->query("select price from table where size = 'small'")->fetch_assoc(); $result = $begin ? $begin["price"] : 'no result'; I then only get errors if something is really wrong, like a missing table. So unless there is anything wrong with that, I think I'll go with that. I really like bare bones code. It is almost impossible to find one-liners for anything. All the mySQL tutorials have while loops. Thank you.
-
Ha! I been around da' block! I wear a T-Shirt that says "Composer" on the front and "Require" on the back! So, seriously, I have tried "composer require......" at least 100 times (over the years I've found at least 100 things on GitHub I've wanted to try). IT NEVER WORKS! So, giving you an example (to prove that I'm not an armchair complainer that doesn't lift a finger), here is what I did: 1.) I downloaded a package today called doghouse-master-api.zip. 2.) I sent it to my Ubuntu server. 3.) I unzipped it. 4.) Now I'm cd'd into my home directory and there is now a directory in there called "doghouse-master-api" 5.) I ran the command "composer require doghouse-master-api" 6.) An ugly apocalyptic crimson rectangle appears (they use crimson just to freak me out) with the error message: [Composer\Downloader\TransportException] The "https://api.github.com/repos/git_user_id/git_repo_id" file could not be downloaded (HTTP/2 404 ) 7.) Like always, I google the error message and it says to run "composer config -g repo.packagist composer https://packagist.org" 8.) Like always, this, and any other suggestion, doesn't work. 9.) I try the 2nd suggestion: "add such-and-such to your composer.json file" 10.) WHAT "composer.json" file? I didn't put no composer.json file in there.... 11.) I check, and sure enough there is a composer.json file there with just { } in it. So I stick what they said I should stick in there, so now it looks like: {"repositories": [ { "type": "composer", "url": "https://packagist.org" }, { "packagist": false } ] } 12.) I run "composer require doghouse-master-api" again. 13.) Now I get ANOTHER error message that says "could not find matching package of doghouse-master-api" 14.) I realize that I put a "packagist.org" url in my composer.json file. I'M NOT USING PACKAGIST the stupid computer should know that. 15.) I then re-read the clumsy "README.md" in my package. It says to put this in it.{ "repositories": [ { "type": "vcs", "url": "https://github.com/GIT_USER_ID/GIT_REPO_ID.git" } ], "require": { "GIT_USER_ID/GIT_REPO_ID": "*@dev" } } 16.) "Aaah! There's my answer" I think in my mind. 17.) WRONG! I get another apocalypse that says I can't use capital letters even thought that's what the damn README.md says. 18.) I change it to lower case. 19.) I get YET ANOTHER error message: [Composer\Downloader\TransportException] The "https://api.github.com/repos/git_user_id/git_repo_id" file could not be downloaded (HTTP/2 404 ) 20.) And that concludes another day of several hours buried in Composer hell. I think in my mind "All this stuff is so stupid it's a ridiculous thing for stupid people that didn't learn PHP back in the 90's like I did and they need their stupid back forward slash autoload dependancy public class private class backslash nonsense because they're too dumb to write the 2 lines of code to get the job done and they learned PHP from a dumb teacher in a dumb college and all this stupid program that I'm trying to download needs is probably about 2 lines of code buried somewhere within it if I could just find it." 21.) And then I go and watch Anime and relax, and chalk up another wasted day of "trying to make Composer work." I'm going to watch Anime now. 😀
-
A prepared query would definitely upset my apple cart 😃 Fortunately these queries are internal queries generated by my own code (more sanitized than a hospital -- Edward Snowden couldn't penetrate). That is what I am having trouble with. Do I have to "error trap" every step of the way? I have variations of this code all over my site, I wish I could have one bulletproof formula for all these similar internal queries. Is this better: $begin = $db->query("select price from table where size = 'small'")->fetch_assoc(); $result = $begin ? $begin["price"] : 'no result'; echo $result;