-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
How to handle json respond from api endpoint (Uncaught TypeError: )
Barand replied to charlion's topic in PHP Coding Help
try <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { var burl = "https://api3.binance.com"; /////////// baseurl///////// var query = burl + '/api/v3/aggTrades'; $.get( query, {"symbol":"BTCUSDT"} , function(resp) { $("#demo").html("a - " + resp[0].a + "<br>") $("#demo").append("p - " + resp[0].p + "<br>") $("#demo").append("q - " + resp[0].q + "<br>") var d = new Date() d.setTime(resp[0].T) $("#demo").append("T - " + d.toString()) }, "JSON" ) }) </script> </head> <body> <div id='demo'> <!--output goes here--> </div> </body> </html> outputs... a - 577817177 p - 48585.69000000 q - 0.10000000 T - Mon Feb 15 2021 17:50:19 GMT+0000 (GMT Standard Time) -
How to handle json respond from api endpoint (Uncaught TypeError: )
Barand replied to charlion's topic in PHP Coding Help
You have an array containing an object. Array ( [0] => stdClass Object ( [a] => 26129 [p] => 0.01633102 [q] => 4.70443515 [f] => 27781 [l] => 27781 [T] => 1498793709153 [m] => 1 [M] => 1 ) ) Try innerHTML = obj[0].p -
Barebones PHP to add a contact email form upload file provision
Barand replied to boilermaker73's topic in PHP Coding Help
https://www.php.net/manual/en/features.file-upload.post-method.php -
https://www.php.net/manual/en/language.types.string.php
-
Your query string is in single quotes therefore $table is not interpreted as "users". Why are you putting the table name in a variable? It's often a sympton of a poorly designed database if it is necessary.
-
Search for string in column in mysql table separated by a character
Barand replied to jigga's topic in PHP Coding Help
Don't. Put your oemnr in a separate table, one per row with the id of the parent record as a foreign key. In other words, normalize your data. +------------------+ | main_table | +------------------+ +------------------+ | id (PK) |-------+ | oem_number | | etc... | | +------------------+ +------------------+ | | id (PK) | +------<| table_id (FK) | | oemnr | +------------------+ -
Your problem is that whatever that mess of code is doing takes more than 2 minutes to execute. You need to find which bit is taking too long to execute and fix. When you post code here, use the code button "<>" in the toolbar. Hopefully that may give readable code (with line breaks and indents). No one is going to attempt to read that.
-
Don't put quotes around placeholders And "LIKE" without widcards is a wast of time
-
If that is how you want the output then GROUP BY may well be the answer student data... +-------+-----------+----------+ | regno | firstname | lastname | +-------+-----------+----------+ | 9738 | Jane | Jenkins | | 9844 | Janet | Gordon | | 9966 | Liz | Lyle | | 9978 | Olivia | Unwin | | 9979 | Curly | NULL | | 9980 | NULL | Larry | | 9981 | NULL | Mo | | 9982 | Fred | | | 9983 | Emily | NULL | +-------+-----------+----------+ query... SELECT CASE WHEN IFNULL(firstname, '') = '' THEN 'No first name' WHEN IFNULL(lastname, '') = '' THEN 'No last name' ELSE 'OK' END as category , COUNT(*) as Errors , GROUP_CONCAT(regno SEPARATOR ', ') as IDs FROM student GROUP BY category HAVING category <> 'OK'; results... +---------------+--------+------------------+ | category | Errors | IDs | +---------------+--------+------------------+ | No first name | 2 | 9980, 9981 | | No last name | 3 | 9983, 9979, 9982 | +---------------+--------+------------------+ CAUTION: the default maximum length of GROUP_CONCAT item is 1024 characters so in my example I would only get a max of 170 ids listed.
-
My way the two scripts would be identical except the US one connects locally to the mysql server and the UK one connects remotely. (If the US DB server is not local to the US page then both would connect using the DB server's IP address)
-
UK Script - downloads from local UK file server and DB connection is to (remote) US DB server US Script - downloads from local US file server and DB connection is to (local) US DB server.
-
In a database the case sensitivity of a field depends on which collation you apply to that field. If the collation name ends with "ci" it is case insensitive. The default collation would normally be case-insensitive but changed for fields where you specifically need sensitivity. I personally would count "fred" and "frEd" as the same username. You wouldn't be storing passwords as plain text for searching anyway.
-
Example of AJAX call to same page
-
You will need INNER JOIN LEFT JOIN GROUP BY GROUP_CONCAT There's a link to SQL tutorials in my sig that might help.
-
That's like asking if a radio transmitter is better than morse code. AJAX is method of communincation between client (javascript) and the server. JSON is a data format.
-
Use a recursive function to search subcategories until found (Pseudocode ti illustrate the logic...) function searchCategories (category, id, title) { foreach (category as cat) { if (this has the id we want) set title value else searchCategories(children, id, title) // calls itself to search sub categories end if } } My test... $category = $categories; for ($searchfor = 1; $searchfor <= 8; $searchfor++) { $title='Not found'; searchCategories($category, $searchfor, $title); echo "$searchfor - $title<br>"; } /* RESULTS 1 - Pants 2 - Boots 3 - Leather 4 - Textile 5 - Sneakers 6 - Sort 7 - Balls 8 - Not found */
-
If you echo '<pre>$json = ', print_r($json, 1), '</pre>'; you get $json = Array ( [terms] => http://www.xe.com/legal/dfs.php [privacy] => http://www.xe.com/privacy.php [from] => USD [amount] => 1.195 [timestamp] => 2021-02-09T16:52:00Z [to] => Array ( [0] => Array ( [quotecurrency] => NGN [mid] => 454.6559871014 ) ) ) Then follow the array keys to the value you want So you need echo $json['to'][0]['mid']; // --> 454.6559871014 [edit] Alternatively foreach ($json['to'] as $price) { printf('%s %0.2f<br>', $price['quotecurrency'], $price['mid'] ); // --> NGN 454.66 }
-
Cannot resolve: Warning: Use of undefined constant
Barand replied to Poet's topic in PHP Coding Help
The "mysql_*" functions were deprecated a decade ago and have been removed from PHP 7. You need to use mysqli or PDO (better). -
Why "$$value" ?
-
Thanks for those beautiful code images. Perhaps I'll hang them on my wall. They're no use for anything alse.
-
https://www.php.net/manual/en/features.file-upload.php
-
sql query, select age range and show none information
Barand replied to nitiphone2021's topic in MySQL Help
CREATE TEMPORARY TABLE ( .... ) -
You're welcome. Good to hear I can make a difference somewhere 🙂
-
sql query, select age range and show none information
Barand replied to nitiphone2021's topic in MySQL Help
Try doing it as I did. -
sql query, select age range and show none information
Barand replied to nitiphone2021's topic in MySQL Help
They have to exist to be output. Now if you'd done as I sugessted last time, and created an "age_group" table, the group would exist. And you are still excluding 80 year olds - do you read replies? SELECT group_name , COUNT(i.id) FROM age_group a LEFT JOIN tb_infected i ON i.age BETWEEN a.lo_age AND a.hi_age GROUP BY age_group_id; +------------+-------------+ | group_name | COUNT(i.id) | +------------+-------------+ | Under 18 | 168 | | 18 - 29 | 116 | | 30 - 39 | 92 | | 40 - 49 | 110 | | 50 - 59 | 109 | | 60 - 69 | 110 | | 70 - 79 | 104 | | 80+ | 0 | +------------+-------------+