-
Posts
24,565 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
They both worked when they left the shop. DATA TABLE: johanm (data provided by you) +----+-------+------------+----------+ | id | name | value_from | value_to | +----+-------+------------+----------+ | 2 | Test1 | C1 | L3 | | 3 | Test2 | C3 | T5 | +----+-------+------------+----------+ Query 1 with results... SELECT id , name FROM johanm WHERE FIELD('C4', 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') >= FIELD(value_from, 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') AND FIELD('L2', 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') <= FIELD(value_to, 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5'); +----+-------+ | id | name | +----+-------+ | 2 | Test1 | +----+-------+ Query 2 with (same) results... (JSON version requires MySQL v5.7.7 +) SELECT id , name FROM johanm j JOIN ( select @jarray := '["C1", "C2", "C3", "C4", "C5", "T1", "T2", "T3", "T4", "T5", "T6", "L1", "L2", "L3", "L4", "L5"]' ) init WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', j.value_from), ']', 1), '[', -1) + 0 <= SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', 'C4'), ']', 1), '[', -1) + 0 AND SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', j.value_to), ']', 1), '[', -1) + 0 >= SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', 'L2'), ']', 1), '[', -1) + 0; +----+-------+ | id | name | +----+-------+ | 2 | Test1 | +----+-------+
-
If you are running the same query I posted on the same data that you posted you should get the same results as me. So which is different?
-
Try $amount = 28; $change = calcChange($amount); foreach ($change as $v => $n) { echo "$n €$v coins <br>"; } function calcChange($amt) { $coins = [ 10 => 0, 5 => 0, 2 => 0, 1 => 0 ]; foreach ($coins as $v => &$n) { $n = intdiv($amt, $v); $amt -= $n * $v; } return $coins; }
-
Using ORDER BY I get this with your posted data mysql> SELECT offers.heading -> , offers.description -> , offers.image -> , offers.status -> , offers.extent -> , addons.addon_title -> , addons.addon_desc -> , addons.thumb -> , addon_available -> FROM offers -> JOIN offers_addons USING (id) -> JOIN addons USING (idde) -> ORDER BY offers.id; +-------------------+---------------------------+-------------+--------+------------+-------------------+---------------------+-------------+-----------------+ | heading | description | image | status | extent | addon_title | addon_desc | thumb | addon_available | +-------------------+---------------------------+-------------+--------+------------+-------------------+---------------------+-------------+-----------------+ | addon here | addon description | texecoz.png | OFF | 2021-09-13 | new text | new description | default.png | 2021-05-30 | | this is addon | addon another description | texecoz.png | OFF | 2021-09-13 | some text | another description | default.png | NULL | | this is addon | addon another description | texecoz.png | OFF | 2021-09-13 | testing | product description | 0410.jpg | 2021-05-25 | | addon title | new addon description | images.jpg | OFF | NULL | helllo there | the description | alhua.png | NULL | | addon title | new addon description | images.jpg | OFF | NULL | testing | product description | 0410.jpg | 2021-05-25 | | another title | latest addon description | santa.png | ON | 2021-09-13 | testing | product description | 0410.jpg | 2021-05-25 | | another title | latest addon description | santa.png | ON | 2021-09-13 | helllo there | the description | alhua.png | NULL | | this is a heading | the addon description | paxton.png | ON | 2021-09-13 | testing | product description | 0410.jpg | 2021-05-25 | | this is a heading | the addon description | paxton.png | ON | 2021-09-13 | here is more text | latest description | 44445.jpg | NULL | +-------------------+---------------------------+-------------+--------+------------+-------------------+---------------------+-------------+-----------------+
-
That's because you are using GROUP BY. This for aggregations and gives one row per offer_id. Try changing it to ORDER BY
-
You can also do it using JSON functions (which I thought might be more elegant until I hit the problem of extracting the numeric values of the json paths returned by JSON_SEARCH() eg "$.[12]" ) SELECT id , name FROM johanm j JOIN ( select @jarray := '["C1", "C2", "C3", "C4", "C5", "T1", "T2", "T3", "T4", "T5", "T6", "L1", "L2", "L3", "L4", "L5"]' ) init WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', j.value_from), ']', 1), '[', -1) + 0 <= SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', 'C4'), ']', 1), '[', -1) + 0 AND SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', j.value_to), ']', 1), '[', -1) + 0 >= SUBSTRING_INDEX(SUBSTRING_INDEX(JSON_SEARCH( @jarray, 'one', 'L2'), ']', 1), '[', -1) + 0 ;
-
It's not pretty, but the working with comma-separated lists in SQL never is SELECT id , name FROM johanm WHERE FIELD('C4', 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') >= FIELD(value_from, 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') AND FIELD('L2', 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5') <= FIELD(value_to, 'C1', 'C2', 'C3', 'C4', 'C5', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'L1', 'L2', 'L3', 'L4', 'L5');
-
the properties of the DateInterval object that you will need are ->days ->h ->i so you will have some calculation but it's not rocket science. Note there is also a "d" property but that is the number of days as in "Difference = 5 months 3 days 6 hrs"
-
either method: "POST", or $.post (instead of $.ajax or $.get)
-
Splitting multidimensional array based on values
Barand replied to 684425's topic in PHP Coding Help
One problem in this type of app, where you want to output something when there is no data, is you are asking the question "Hands up everyone who isn't here?". Your query needs to know all status_id values to look for. So you need a status table +-----------+ | status | +-----------+ | status_id | | descrip | +-----------+ Example (I added a status 3 as there weren't any of those) USER STATUS +---------+-----------+----------+ +-----------+----------+ | user_id | status_id | username | | status_id | descrip | +---------+-----------+----------+ +-----------+----------+ | 1 | 1 | peterd | | 1 | Status A | | 2 | 1 | lauran | | 2 | Status B | | 3 | 2 | tomd | | 3 | Status C | | 4 | 1 | cheggs | +-----------+----------+ | 5 | 2 | pollyv | | 6 | 2 | pollys | | 7 | 1 | tomc | | 8 | 2 | comet | | 9 | 2 | cupid | | 10 | 1 | donner | | 11 | 1 | blitzen | | 12 | 2 | dancer | | 13 | 2 | prancer | | 14 | 1 | dasher | | 15 | 1 | vixen | +---------+-----------+----------+ query SELECT s.status_id , coalesce(group_concat(u.username order by user_id separator ', '), 'NO DATA') as users FROM status s LEFT JOIN user u USING (status_id) GROUP BY status_id; results +-----------+--------------------------------------------------------------+ | status_id | users | +-----------+--------------------------------------------------------------+ | 1 | peterd, lauran, cheggs, tomc, donner, blitzen, dasher, vixen | | 2 | tomd, pollyv, pollys, comet, cupid, dancer, prancer | | 3 | NO DATA | +-----------+--------------------------------------------------------------+ -
One problem I can see is that you aren't telling your ajax request to use POST so it is probably defaulting to GET Does it work if you change $_POST in script.php to $_GET?
-
You appear to be missing an "if" elseif (isset($_POST['save'])) ^^
-
If only PHP had a way you could increment a count. $i = 0; $content = ' <style> td { font-weight: normal; font-size: 8px; line-height: 12px; color: #000000; } </style> <table id="receiptTable" style="max-height: 1000px;" > <tr> <td width = "7%" > '. ++$i .' </td> <td width= "8%" > '.'xxxxxx'.' </td> <td width= "10%" > '.'xxxxxx'.' </td> <td width= "30%"> '.'123'.' '.'Kg'.' </td> <td width= "20%" > RM '.'123.45'.' </td> <td width= "20%" > RM '.'123.45'.' </td> </tr> <tr> <td width = "7%" > '. ++$i .' </td> <td width= "8%" > '.'xxxxxx'.' </td> <td width= "10%" > '.'xxxxxx'.' </td> <td width= "30%"> '.'123'.' '.'Kg'.' </td> <td width= "20%" > RM '.'123.45'.' </td> <td width= "20%" > RM '.'123.45'.' </td> </tr> <tr> <td width = "7%" > '. ++$i .' </td> <td width= "8%" > '.'xxxxxx'.' </td> <td width= "10%" > '.'xxxxxx'.' </td> <td width= "30%"> '.'123'.' '.'Kg'.' </td> <td width= "20%" > RM '.'123.45'.' </td> <td width= "20%" > RM '.'123.45'.' </td> </tr> </table> '; OUTPUT
- 1 reply
-
- 1
-
PHP runs on the server, javascript runs on the client after the PHP has finish running and sent the page to the client. Store $PAGE in a hidden field in your html <input type="hidden" id="page-name" value="<?=$PAGE?>" > In your script you can then access it with var page = $("#page-name").val() then send it i your ajax request $.post( "script.php", {"page":page}, // data function(content) { $("#notif").html(content) }, "TEXT" ) In script.php, access the page from $_POST['page]
-
It would appear that some CSS is implemented (such as font-size, line-spacing, color and other basics) but others are not. Your counter appears to be in those that are not. Those that do work can be in a <style> section or in-line. There may be a list in the documentation, who knows? If there were a prize for the worst documentation, tcpdf would win it outright. I have to resort to examining the source code to find out what arguments each method requires and what it does. For the counter you may have to resort to the age-old method of outputting an incremented counter variable.
-
Another way is to give the checkbox form input the value you actually want when it is set (ie "1") <input class="form-check-input" type="checkbox" id="a201" name="a201" value="1"> then $score = $_POST['a201'] ?? 0; which means "if the checkbox exists and is not null, use its value, otherwise default to "0". (See Null Coalesce operator)
-
Why can it not be as simple as ... $res = $db->query("SELECT user_id , month , year , description , CASE is_UBC WHEN 1 THEN 'Yes' ELSE '' END as ubc FROM dbert ORDER BY year, month "); $tdata = ''; foreach ($res as $r) { $date = sprintf("%02d/%4d", $r['month'], $r['year']); $tdata .= "<tr><td>{$r['user_id']}</td> <td>{$r['description']}</td> <td>{$r['ubc']}</td> <td>$date</td> </tr>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf8"> <title>Sample</title> <style type='text/css'> table { width: 800px; border-collapse: collapse; } th, td { padding: 5px; } </style> </head> <body> <table border="1"> <tr><th>User ID</th> <th>Description</th> <th>UBC</th> <th>Date</th> </tr> <?=$tdata?> </table> </body> </html>
-
Strange I know, but I couldn't get that picture of your code to execute. Also, this looks suspiciously like homework - read the guidelines
-
Something like ... $appID = 1; $statement = $conn->prepare("SELECT f.survey_date , r.firstname , f.feel , f.cough , f.fever , f.headache , f.tired , f.appetite , f.swelling FROM tb_feedback f INNER JOIN tb_register r ON r.id = f.user_id WHERE f.app_ID = ? "); $statement->bind_param('i', $appID); $statement->execute(); $res = $statement->get_result(); $data = []; foreach ($res as $r) { if (!isset($data[$r['survey_date']])) { $data[$r['survey_date']] = [ 'datetime' => $r['survey_date'], 'history' => [] ]; } $data[$r['survey_date']]['history'][] = array_slice($r, 1); } $jdata = json_encode( ['Data' => array_values($data)] ); echo $jdata;
-
First of all, don't run queries inside a loop like that. Just use a single query. When processing the query results put the data into an array with the structure required for your JSON data json_encode the array.
-
The function definition clearly shows that it takes only 3 arguments - table, field, condition. In your second call you are passing 5 so the final 2 are ignored. Try $count_long_trades_pos = $db->getQueryCount('tbl_trades','lg_short_trade',' AND lg_short_trade="Long" AND pnl_trade > 0 AND num_bot_trade="'.$_REQUEST['editId'].'"'); Secondly, I don't know what the other functions in your database class are like but that one should be consigned to the trash bin... Incorrect use of prepared statements The query will wil return one row with one column (the count), so why return fetchAll() which is for multiple rows?, and why not just return the record count? The if() condition in the middle is waste of code