Jump to content

ChenXiu

Members
  • Posts

    177
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

ChenXiu's Achievements

Advanced Member

Advanced Member (4/5)

2

Reputation

3

Community Answers

  1. A company allows me to curl their API for up to 3 sku numbers at a time (e.g. appending "sku[]=12345&sku[]=36545&sku[]=88775" to their URL). To exceed their "3 sku limit," they provided me a 2nd API key. The following script is supposed to divide my total number of skus in half, and curl both halves in parallel (half using one API key, half using the other). 🤢 I have a feeling that my script isn't really properly dividing my skus amongst my API url/key pairs! 🤢 <?php $sku_numbers = array( '12345', '33333', '98745', '44444', '11111', '05054' ) $key1 = 'first_SECRET_key'; $key2 = 'second_SECRET_key'; $keycount = 0; foreach(array_chunk($sku_numbers,3) as $chunk) { $api_KEY = $key1; $keycount++; if($keycount % 2 != 0) { $api_KEY = $key2; } $sku_LISTINGS = '&sku[]=' . implode('&sku[]=',$chunk); $url = 'https://api.data_companneee.com/script.php?apiKey='.$api_KEY.$sku_LISTINGS; $url2 = 'https://api.data_companneee.com/script.php?apiKey='.$api_KEY.$sku_LISTINGS; $ch1 = curl_init($url); $ch2 = curl_init($url2); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch2, CURLOPT_HEADER, 0); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); $mh = curl_multi_init(); curl_multi_add_handle($mh, $ch1); curl_multi_add_handle($mh, $ch2); $active = null; do { $status = curl_multi_exec($mh, $active); curl_multi_select($mh); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); $response_A = curl_multi_getcontent($ch1); $response_B = curl_multi_getcontent($ch2); curl_multi_remove_handle($mh, $ch1); curl_multi_remove_handle($mh, $ch2); curl_multi_close($mh); foreach($chunk as $each_sku) { // do stuff (process $response_A) etc. // do stuff (process $response_B) etc. } } May I please have your thoughts on this? Thank you!!
  2. I believe I mis-spoke. Olumide, your answer seems to work! Thank you for that!! You have restored my faith in PHP 😀
  3. Thank you all for your time.
  4. I expected 4 items to arrive. Here are the SKU numbers: 1111 2222 2222 3333 When the box arrived and I opened it, I found these SKU numbers: 1111 2222 3333 3333 Upon opening the box, I immediately saw: • SKU 2222 was missing • there was an extra SKU 3333 My simple PHP script correctly shows the missing SKUs and the reconciled SKUs..... but why is it not showing the extra SKU? <?php $expected = array( '1111', '2222', '2222', '3333' ); $received = array( '1111', '2222', '3333', '3333' ); $expected = array_filter($expected); $received = array_filter($received); $expected_counts = array_count_values($expected); $received_counts = array_count_values($received); $not_received = array(); $reconciled = array(); $extra = array(); foreach ($expected_counts as $sku => $expected_count) { if (isset($received_counts[$sku])) { if ($received_counts[$sku] >= $expected_count) { $reconciled = array_merge($reconciled, array_fill(0, $expected_count, $sku)); } else { $reconciled = array_merge($reconciled, array_fill(0, $received_counts[$sku], $sku)); $not_received = array_merge($not_received, array_fill(0, $expected_count - $received_counts[$sku], $sku)); } } else { $not_received = array_merge($not_received, array_fill(0, $expected_count, $sku)); } } foreach ($received_counts as $sku => $received_count) { if (!isset($expected_counts[$sku])) { $extra = array_merge($extra, array_fill(0, $received_count, $sku)); } } // Output the results print_r($not_received); // SKUs that are missing print_r($reconciled); // SKUs that reconcile print_r($extra); // EXTRA (unexpected) SKUs ?>
  5. @barand, same result whether or not they are strings: $total = 9780393872439 + 9780134130422 + 30.50; echo $total; // WRONG TOTAL AGAIN @ginerjm, I have error reporting on, there are no errors. If you have errors ($total is undefined), then you forgot to initialize "$total = 0;" like I did. @requinix, PHP is the easiest programming language to learn, the others are too complex for me to understand. Just like 13 digit numbers are too complex for PHP to understand. C'est la vie. The correct answer is: When using numbers over 12 digits long, PHP may exhibit 'unexpected behavior' because of the way it handles floating points (euphemistically called "rounding errors"), therefore extensions like BCmath may help.
  6. Now I know why real programmers snicker a little bit when I mention PHP. $numbers = array( '9780393872439', '9780134130422', '30.50' ); foreach($numbers as $num) { $total += $num; } echo $total //WRONG! WRONG! WRONG! I'm frankly disappointed.
  7. For a mySQL query on 4 tables that will be joined on the "sku_number varchar(20) PRIMARY KEY" column, does the ordering of the tables in the query affect performance (speed) ? • I need a large-data longtext column from Table "a" • I need a small varchar(9) column from Table "b" • I need 5 medium sized (varchar(255)) columns from Table "c" • I need 2 small varchar(55) columns from Table "d" Will the speed be just as fast if I do the query in alphabetical order (e.g. "select a.bigdata, b.color, c.weight, c.height, c.depth...") -or- Would the query execute faster if done from the "smallest data size - to - biggest data size" (e.g. "select b.color, a.bigdata...." ... and, should "how many columns I need from a table" affect the order? Thank you in advance.
  8. Thank you both. My goal was actually to speed up 4 mysql queries to 4 different tables, some sharing the same data, some not. I was looking for a huge speed improvement, but with the complexity of the "union all" style, it is a nightmare for me, especially for keeping track of what data comes from which table. But I'm glad I learned, and actually executed a real "union" style query! However, I just now experimented with using "join" instead of "union" and was surprised that using "join" benchmarked in at 3 (THREE) times faster (unlike the meager 2X faster of "union") than doing normal mysql queries of 4 tables in sequence (thus extending the vacation/relaxation portion of my life by an extra 20 picoseconds, instead of 15 picoseconds (golf course, here I come!)). I'm finding mysql to be more of a religion than a science. Like Excel. A few have absolutely mastered it, and for them, mysql can do just about everything on the planet. Same with Excel. The rest of us will never truly get it. But that's what makes it fun to try and learn.
  9. Barand, thank you. Okay, I think I understand. It sounds like if the first query defines the column names, then maybe what I should do is make my first query the longest query with the most actual columns, and set the alias names there to be used for the remaining "union select" subqueries. Regarding using a "dummy column" to differentiate, I am not sure how PHP echoes a dummy column, would it be something like this: $result = $db->query($query); while($row=$result1->fetch_assoc()) { echo $row["A"."description"]; } ( ...probably not, but I want to demonstrate that I am trying 😀 ) The reason some of the columns have the same names is because I have never used "union select" before, so never needed to differentiate, and there is often lots of duplication amongst the data I receive from various sources (e.g. all the sources seem to provide "color" and "description," of which only one of them may provide "weight" or "dimensions," etc. So when I built the mySQL tables, I didn't know in advance I would ever need to differentiate between which table I needed, for example, the color from. I suppose the easiest thing would be to simply rename the table columns so they are all different (takes 5 minutes max to do this), but then I won't learn the new stuff :-) Regarding empty results -- Yes! For whatever reason, out of the 100's of experiments I did earlier today, while looping through the "while ($row = $result->fetch_assoc())" loops, PHP would always echo empty results in seemingly random spots. My "fix" on this was this: $dog = !empty($row["columnName"]) ? $row["columnName"] : ""; $horse = !empty($row["other_columnName"]) ? $row["other_columnName"] : ""; $bird = !empty($row["columnSomething"]) ? $row["columnSomething"] : ""; echo $dog . $bird . $horse, etc., etc. I really want to learn how to make the, "Union Select" technique work! I did some benchmark loops of 1000 queries (union 3 queries, versus 3 queries in sequence, ran 1000 times each) and found that "Union Select" is twice as fast! Instead of taking 30 nanoseconds for a 1000-query loop, it only takes 15 nanoseconds. That being said, just running a "union query" once will only save me 15 picoseconds. However, when you get to be my age, every picosecond counts.
  10. I have tried several "UNION ALL" queries, and I either get: • "undefined index" errors or • when selecting from tables with same column names, I can't differentiate which table the column belongs to • I get alot of empty results Here are some examples I have tried: $query = "SELECT description as ffff, NULL as gggg FROM myTable WHERE sku = '1234' UNION ALL SELECT description as ffff, weight as gggg FROM my_other_Table WHERE sku = '1234'" $result = $db->query($query); while ($row = $result->fetch_assoc()) { echo $row['fff']; echo $ow['gggg']; } $query = "SELECT description as ffff, NULL as gggg FROM myTable WHERE sku = '1234' UNION ALL SELECT color as uuuuuu, weight as pppppppppp FROM my_2nd_Table WHERE sku = '1234'" UNION ALL SELECT color as wwxxyyzz, weight as mmmnnnoooppp FROM my_3rd_Table WHERE sku = '1234'" $result = $db->query($Query); while ($row = $result->fetch_assoc()) { echo $row["ffff"]; echo $row["uuuuuu"]; echo $row["pppppppppp"] echo $row["wwxxyyzz"]; echo $row["mmmnnnoooppp"]; } I think I am almost on the right 'track' but the train conductor wants his ticket :-)
  11. @Barand, thank you! That thing you do -- that putting the "key-back-into-the-array-thing" -- $received[$k] -- got me again! I still don't know how you know when to do this. But it works perfect! Thank you again! Your code appears to work perfectly! Had you not come up with a solution, my next step would have been to use mySQL :-) @jodunno, thank you! Your code worked. The logic you used makes perfect sense now.... but was out of my grasp this past week I've been trying to figure this on out. (I'm sure unintentional, but it is amusing how your variable name "$reckoning" sorta fits right in there with Noah and $ark 😀)
  12. Hmm.... I see 2 replies while I was typing my last post. I'll try them both.
  13. I'll repost my original post: The ark has 14 animals on it: 6 cows, 7 fish, and a cat (keeps the mice away) $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); When the Ark arrived, Billy made a checklist: $received = array( '3 cows', '7 fish' ); As you see, his checklist has only 10 animals: 3 cows, and 7 fish. (story has it that the cat and 3 of the cows had a scheme to escape, right from the beginning. But that's another story....) So the PHP script should yield this: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat .... again, this is way *way* harder than it looks, because arrays are so finicky. When I saw your post, I immediately thought how easy this would be using mySQL: Simply read the textfile line by line into a temp mySQL table, and use "limit 1" for the queries. But I want to see if this can be cracked using PHP arrays...
  14. Without the break, it puts "arrived!" after everything 3 horses arrived! 7 fish arrived! 3 horses arrived! 1 cat I'm trying to get PHP to move onto the next array value when a match is found. This is way *way* harder than it looks. Maybe arrays aren't the way to do this.
  15. Noah is having trouble with his PHP code $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); foreach($ark as $expected) { echo $expected; foreach($received as $line) { if($expected == $line) { echo ' ARRIVED!'; } break; } echo '<hr>'; } The result should be: 3 cows ARRIVED! 7 fish ARRIVED! 3 cows 1 cat What can Noah do to fix his code?
×
×
  • 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.