Jump to content

ChenXiu

Members
  • Posts

    153
  • 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)

1

Reputation

3

Community Answers

  1. @mac_gyver THANK YOU! The $matches[1][$key] <--- This is the one thing that I didn't try. That "$key" variable in that spot. Thank you again!
  2. my preg_match_all works perfectly. I just can't figure out how to loop through the "$matches" array it creates. preg_match_all( $pattern , $string , $matches); The code print_r($matches) yields the following super simple array. ( [0] => Array ( [0] => 123456wordsandwordsandmorestuff$40.97 [1] => 987654wordsandmorewords$26.79 ) [1] => Array ( [0] => 123456 <----------this goes with the [0] below [1] => 987654 <----------this goes with the [1] below ) [2] => Array ( [0] => $40.97 <-----------this goes with the [0] above [1] => $26.79 <-----------this goes with the [1] above ) ) The problem is I can't figure out how to do the foreach() loop to get this: SKU number 123456 costs $40.97 SKU number 978654 costs $26.79 I know the answer will be embarrasingly simple..... but I've tried at least 200 different foreach variations and I am close to giving up. Thanks.
  3. May I please ask one final question on this topic? Question: Does this line of code "global $ch;" cause cURL to be inited each time I call this function later in my script? And, if so, can my code be modified so that cURL is only inited once at the beginning of the script? (unneccessarily initing cURL over and over when it only needs to be inited once wastes script execution time): $ch = curl_init(); // I want to only init cURL just once function my_curl_function($url_with_SKUs) { global $ch; curl_setopt($ch, CURLOPT_URL,$url_with_SKUs); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $decoded = json_decode($result,true); foreach($decoded as $my_results) { // do something } } // some php code here my_curl_function($url); // some php code here my_curl_function($url); // some php code here my_curl_function($url);
  4. Uh oh.... fetchALL didn't work. "error stack trace blah blah blah" @Barand, yes PDO. I don't use PDO. I'm still stuck in PHP circa 1999. (But at least I learned *2* things today 😀)
  5. @mac_gyver & @Barand: THANK YOU! 😀 Just in this one thread I've learned 3 new things! • array_chunk (very cool!) • using "implode" instead of nested foreach loop! • mySQL "fetchALL" (I didn't know that existed -- I only knew about fetch_obj, fetch_assoc, and fetch_array) Thank you again!!
  6. In the example in my previous post, "$sku" was a normal array of SKU numbers. What would I do if the SKU numbers came from a mySQL database? Given the following mySQL query: $result = $db->query("select sku_numbers from table"); Should I put the mySQL result into an array, and then use array_chunk, like this? $result = $db->query("select sku_numbers from table"); while ($each = $result->fetch_assoc()) { $sku[] = $each["sku_numbers"]; } foreach(array_chunk( $sku, 5) as $chunk { // do stuff Or is there a better ("more efficient") mySQL style code to use? Thank you!
  7. Ohhhh okay I see what you meant. Instead of nested foreach loop, the "implode" function does the same thing, but much more efficient! THANK YOU!
  8. Thank you. When using array_chunk, is this nested "foreach" loop the BEST way to return values, for the API/cURL script I had described: $myArray = array( '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ); $chunked = array_chunk( $myArray , 5 ); foreach($chunked as $key => $value) { foreach ($value as $key1 => $value1) { // $value1 is the result I would want } }
  9. An API allows me to access information on an unlimited number of SKUs that I submit via cURL. However, their "multiple-lookup" URL limits the SKU numbers to maximum of 5 at a time. Question: Given a list of 100 SKU numbers with a maximum of 5 at a time, what is the best way to loop through them? Their API format is: https://example.com?key=secret&SKU=1234&SKU=4444&SKU=555&SKU=0101&SKU=3333 My PHP script makes the cURL calls, and place the results into a $_SESSION. Here is an example of my code using 7 SKU numbers: $ch = curl_init(); my_curl_function($url_with_SKUs) { curl_setopt($ch, CURLOPT_URL,$url_with_SKUs); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch) $_SESSION["curlResults"][] = $result // I put the results into Session. } $sku = array( '1234', '5678', '4444', '2222', '9393', '1111', '8689' ); $url = 'https://example.com?key=secret'; $append = ''; for($i=0;$i<count($sku);$i++){ $append .= '&SKU='.$sku[$i]; if(($i+1) % 5 == 0) { $url .= $append; $append = ''; my_curl_function($url); } } ...but my code has flaws: • when I use modulus, there are still the final left-over SKU numbers that don't get put in the URL • I'm not even sure I should be using a function at all • Should I even be doing a "for" loop? Or would a "while" loop or a "do" loop be better? Even though this seems to be SUPER basic novice PHP stuff, I can't seem to put this all together in my brain. I've spent weeks on this... Your suggestions will be most welcome.
  10. Arrays use brackets. What is the syntax if I wanted a bracketed variable inside brackets? For example "https://www.example.com/index.php?animal=horse" has this $_GET variable: $_GET["animal"] Security vulnerabilities notwithstanding, the following syntax does not work: echo $_SESSION[$_GET["animal"]]; I assume it does not work because it's a bracketed variable inside brackets. What syntax makes this work? Thank you!
  11. Barand, thank you, but in your answer you used the phrase, "...unless you tell it what could potentially be there." I was hoping that the query itself tells PHP what could potentially be there. My query here demonstrates that "rose" and "carnation" could potentially be there: SELECT * FROM prettyflowers WHERE flower = 'rose' UNION SELECT * from prettyflowers WHERE flower = 'carnation' ...etc., etc. That's why I thought PHP could somehow keep track of what's not in the table ( "carnation" in this example). Currently my only solution is to do TWO separate queries: • "check and see if it is there" query, and • the main query Like this: $not_in_table = array(); // CAPTURES WHAT DOESN'T EXIST IN TABLE foreach($flowers as $foo) { //NOTE: "$flowers" would be list of flowers input by visitor if ($result = $db->query("select exists(select * from prettyflowers where flower = '$foo') as c")->fetch_assoc()) { if(!$result['c']) { $not_in_table[] = $foo; } } } // NOW I RUN MY MAIN QUERY // (using "UNION SELECT" because customer will input 100 flowers, and we want to keep the same order as input by customer) $main_query = "select * from prettyflowers where flower = 'rose' UNION SELECT * FROM prettyflowers WHERE flower = 'carnation' "; $flowerList = $db->query("$main_query"); while ($flower = $flowerList->fetch_assoc()) { echo $flower["color"]; echo '<BR>'; } // AND THEN I CAN LET VISITOR KNOW WHICH ONES WEREN'T IN TABLE echo 'Sorry, these were not in database<BR>'; foreach ($not_in_table as $absent) { echo "$absent <BR>"; }
  12. The code I provided was just a silly example to articulate my question: How to capture list of what keys are not in a given table. If a given table has the keys 1, 2, 3, 4, 5 in it, and my query is select blah from table where key = 2, 3, 8, and 9 Obviously "8" and "9" are not in the table, and that's what I want captured by PHP. PHP would give me this result: $not_in_table = array( '8', '9' ); So while I'm doing my queries and retrieving valid results, etc., I am also wanting PHP to capture/keep-track-of keys not in the table.
  13. Table name: "prettyflowers" +--------+--------+ | flower | color | +--------+--------+ | daisy | yellow | | rose | red | | orchid | purple | | lily | pink | +--------+--------+ Query: SELECT color FROM prettyflowers WHERE flower = 'rose' UNION SELECT color from prettyflowers WHERE flower = 'carnation' UNION SELECT color from prettyflowers WHERE flower = 'orchid' UNION SELECT color from prettyflowers WHERE flower = 'tulip'; Question: My query requests data on 4 flowers, but the table only has data on 2 of the flowers. (Notice that "carnation" and "tulip" are absent from the table.) When running a Union Select query like the one above, is there a way for PHP/mySQL to return a list of what's NOT present in the table? Here's a non-working "pseudocode" to illustrate what I'm looking for instead of: while ($results = $query->fetch_assoc()) { ...... maybe there's something simple like while ($results != $query->fetch_assoc()) { $array_of_things_not_in_table[ ]= Thanks!
  14. I almost took offense, thinking you were singling me out... But I see you're this way with everyone https://forums.phpfreaks.com/profile/109381-ginerjm/content/
  15. Correct, one session at a time. By "One big session," I mean place the session variables under one named variable. Analogy: imagine a manila folder containing photos of fruits. Option 1.) Manila folder is unlabeled: $_SESSION["apple"] = 'red'; $_SESSION["grape"] = 'green'; $_SESSION["lemon"] = 'yellow'; Option 2.) Manila folder is labeled "fruit": $_SESSION["fruit"]["apple"] = 'red'; $_SESSION["fruit"]["grape"] = 'green'; $_SESSION["fruit"]["lemon"] = 'yellow'; I'm trying to learn PHP best practices. Which is "best practice?" I know, because my PHP code generates the $ordernumber value, and immediately places it in session before serving to browser. 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.