Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Yes, change the file name from xxx.html to xxx.php.
  2. Most servers are configured so that files with names ending with ".php" are processed by the PHP preprocessor. Unless your server is specifically configured to do so, ".html" files will not be processed and any php code is treated a text.
  3. That is only a problem if you store the "display name" in the list. Store the users' ids (which will be unique and never change) and the problem vanishes. EG +----------------+ +--------------------+ +-------------+ | user | | group_member | | group | +----------------+ +--------------------+ +-------------+ | user_id PK |------+ | group_id |>-----------| group_id PK | | username | +----<| user_id | | group_name | | password | +--------------------+ +-------------+ | display_name | +----------------+
  4. For the record (several user variables, three levels of subquery and a user-defined function later) here is an SQL impementation. The query on it's own, without the function, gave +----------------+----------------+ | my | days | +----------------+----------------+ | May 2005 | 21 | | June 2005 | 22 | | November 2006 | 1-2 | | September 2019 | 28-29-30 | | October 2019 | 1 | | August 2020 | 8 10-11-12 16 | | September 2020 | 20 27-28-29-30 | | October 2020 | 1-2 | +----------------+----------------+ The contiguise() function parses the "days" string, adds commas and removes the inner numbers from the ranges and replaces them with a "-". With the function it gives +----------------+--------------+ | my | days | +----------------+--------------+ | May 2005 | 21 | | June 2005 | 22 | | November 2006 | 1-2 | | September 2019 | 28-30 | | October 2019 | 1 | | August 2020 | 8, 10-12, 16 | | September 2020 | 20, 27-30 | | October 2020 | 1-2 | +----------------+--------------+ QUERY SELECT my , contiguise(REPLACE(GROUP_CONCAT(contig order by dno separator ' '), ' - ', '-')) AS days FROM ( SELECT CASE WHEN dno = @prev + 1 AND my = @prevmy THEN concat('- ',dno) ELSE dno END AS contig , date , @prev := dno AS dno , @prevmy := my AS my FROM ( SELECT DATE_FORMAT(date, '%M %Y') AS my , DAY(date) AS dno , date FROM date ORDER BY date ) pre JOIN (SELECT @prev := -1, @prevmy := '') init ) data GROUP BY my ORDER BY date; FUNCTION DELIMITER $$ CREATE FUNCTION `contiguise`(days varchar(150) ) RETURNS varchar(150) CHARSET utf8 BEGIN DECLARE temp varchar(150) DEFAULT ''; DECLARE result varchar(150) DEFAULT ''; DECLARE pos1 int DEFAULT 1; DECLARE pos2 int DEFAULT 0; WHILE pos1 <= LENGTH(days) DO SET pos2 = LOCATE(' ', days, pos1); IF pos2 = 0 THEN SET pos2 = LENGTH(days)+1; END IF; SET temp = SUBSTRING(days, pos1, pos2 - pos1); IF LOCATE('-', temp) <> 0 THEN SET temp = CONCAT(SUBSTRING_INDEX(temp, '-',1), '-', SUBSTRING_INDEX(temp, '-',-1)); END IF; IF result = '' THEN SET result = temp; ELSE SET result = CONCAT(result, ', ', temp); END IF; SET pos1 = pos2 + 1; END WHILE; RETURN result; END$$ DELIMITER ; TEST DATA +------------+ | date | +------------+ | 2005-05-21 | | 2005-06-22 | | 2006-11-01 | | 2006-11-02 | | 2019-09-28 | | 2019-09-29 | | 2019-09-30 | | 2019-10-01 | | 2020-08-08 | | 2020-08-10 | | 2020-08-11 | | 2020-08-12 | | 2020-08-16 | | 2020-09-20 | | 2020-09-27 | | 2020-09-28 | | 2020-09-29 | | 2020-09-30 | | 2020-10-01 | | 2020-10-02 | +------------+
  5. However, using the string just as far as the the first entity $valrD = json_decode(valrGet, true); echo '<pre>$valrD = ', print_r($valrD, 1), '</pre>'; gives therefore $target = 'BTC/ZAR'; foreach ($valrD['response']['entities'] as $k => $ents) { if ($ents['pair_name'] == $target) { echo "$target asking price : {$ents['ask']['price']}<br>"; break; } } outputs "BTC/ZAR asking price : 179382.54"
  6. Your json has been truncated and therefore invalid.
  7. Ask yourself "Why are you running that query?"
  8. Define "allows a connection"
  9. SQL could, but it would be a huge PITA using user variables to keep track of year and month changes and contiguous day ranges. Far easier to do that bit in the PHP ... ## ## SET UP SOME TEST DATES ## $dates = array( '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-16', '2020-08-08', '2020-09-20', '2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02' ); $db->exec("CREATE TEMPORARY TABLE dates (datecol date)"); $stmt = $db->prepare("INSERT INTO dates (datecol) VALUES (?)"); foreach ($dates as $d) $stmt->execute([$d]); ## ## PROCESS THE DATES ## $res = $db->query("SELECT DATE_FORMAT(datecol, '%M %Y') as my , GROUP_CONCAT(day(datecol) ORDER BY day(datecol) SEPARATOR ', ') as days FROM dates GROUP BY my ORDER BY datecol "); foreach ($res as $r) echo contiguise($r['days']) . " {$r['my']}<br>"; /** * replace contiguous ranges with "-" * * @param mixed $list comma-space separated */ function contiguise($list) { $nums = explode(', ', $list); $new = ''; $prev = -1; $contig = 0; foreach ($nums as $k => $n) { if ($n == $prev + 1) { $contig = 1; } else { if ($contig) { $new .= "-{$prev}" ; $contig = 0; } if ($prev != -1) { $new .= ', '; } $new .= $n; } $prev = $n; } if ($contig) $new .= "-{$prev}" ; return $new; } ... giving us ... 8, 10-12, 16 August 2020 20, 27-30 September 2020 1-2 October 2020
  10. Given the array values from you previous topic, you should be aiming to generate an SQL string that looks like this INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES ('Bacon', 500, 3.25, '2020-12-12'); 1 ) The $name needs to be inside single quotes otherwise it interprets bacon as a column name and not as a string value. 2 ) Your weight and price columns should be numeric types, not varchar (weight int, price decimal(10,2) ). 3 ) Your current date format of 12/12/2020 is not a valid DATE format. Your unquoted date string in that format is interpreted as "12 divide by 12 divide by 2020" You current method of putting variables inside the SQL string is unsafe. Use prepared statements and pass the values as parameters EG $stmt = $pdo->prepare(("INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES (?,?,?,?) "); $stmt->execute( [ $name, $weight, $price, $date ] );
  11. I hope you are not storing dates in your db table in that "12/12/2020" format. What is the structure of the table you are trying to update?
  12. Sort the array then loop through your array of dates. Store them in another array whose key is "month year" and the elements are arrays of days. EG [ 'January 2020' => [1, 22, 31], 'March 2020' => [10, 12] ] Loop through that array outputting the imploded day arrays and keys. edit: PS if your dates are a database table mysql> SELECT DATE_FORMAT(datecol, '%M %Y') as my -> , GROUP_CONCAT(day(datecol) SEPARATOR ', ') as days -> FROM datetbl -> GROUP BY DATE_FORMAT(datecol, '%M %Y') -> ORDER BY datecol; +----------------+------------+ | my | days | +----------------+------------+ | May 2005 | 21 | | November 2006 | 1, 2 | | September 2020 | 28, 29, 30 | | October 2020 | 1 | +----------------+------------+
  13. Correction to my above code <input type="checkbox" name="fruit['apple']" id="apple" value="1"> ^ ^ | | remove quotes in input names
  14. I've added the tags for you - this time. Next time, use the "code" icon
  15. Try running the script below. It will display the posted form input. If you see what is being posted it should give you a clue about how to process it. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { // was form data submitted? if (isset($_POST['fruit'])) { echo "Your form inputs were<br>"; echo '<pre>$_POST = ', print_r($_POST, 1), '</pre>'; echo "<hr>"; } else echo "No fruits were selected<hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <style type='text/css'> label { display: inline-block; width: 80px; } </style> </head> <body> <form method="POST"> <fieldset> <legend>Select fruits</legend> <label for='apple'>Apple</label> <input type="checkbox" name="fruit['apple']" id="apple" value="1"> <br> <label for='orange'>Orange</label> <input type="checkbox" name="fruit['orange']" id="orange" value="1"> <br> <label for='banana'>Banana</label> <input type="checkbox" name="fruit['banana']" id="banana" value="1"> <br> <label for='pear'>Pear</label> <input type="checkbox" name="fruit['pear']" id="pear" value="1"> <br> <label for='kiwi'>Kiwi</label> <input type="checkbox" name="fruit['kiwi']" id="kiwi" value="1"> <br> </fieldset> <br> <input type="submit" value="Submit"> </form> </body> </html>
  16. Yes, there will be some variables to output that were prepared in the php section, but any validation conditions requiring you to send a location header to another page will have occured before anything is sent to the browser in the html section.
  17. Simples! Do your php processing before the html section. <?php ?> <html> </html>
  18. Perhaps ## ## create a test table ## $db->exec("CREATE TABLE IF NOT EXISTS production_status ( id int not null auto_increment primary key, submit_time datetime ) "); ## ## randomly add 1,000 records for last 2 months ## $data = []; for ($i=0; $i < 1000; $i++) { $r = rand(1, 60); $dt = date('Y-m-d H:i:s', strtotime("-$r days")); $data[] = "('$dt')"; } $db->exec("INSERT INTO production_status (submit_time) VALUES " . join(',', $data)); ## ## Now the bit you need ## $start_date = date('Y-m-d', strtotime('first day of this month')); $res = $db->prepare("SELECT date_format(submit_time, '%a %d-%b-%y') as date , COUNT(*) as total FROM production_status WHERE date(submit_time) >= ? GROUP BY date ORDER BY submit_time "); $res->execute([ $start_date ]); RESULTS +---------------+-------+ | date | total | +---------------+-------+ | Tue 01-Sep-20 | 14 | | Wed 02-Sep-20 | 24 | | Thu 03-Sep-20 | 15 | | Fri 04-Sep-20 | 21 | | Sat 05-Sep-20 | 15 | | Sun 06-Sep-20 | 24 | | Mon 07-Sep-20 | 17 | | Tue 08-Sep-20 | 17 | | Wed 09-Sep-20 | 15 | | Thu 10-Sep-20 | 11 | | Fri 11-Sep-20 | 15 | | Sat 12-Sep-20 | 16 | | Sun 13-Sep-20 | 13 | | Mon 14-Sep-20 | 11 | | Tue 15-Sep-20 | 21 | | Wed 16-Sep-20 | 21 | | Thu 17-Sep-20 | 15 | | Fri 18-Sep-20 | 17 | | Sat 19-Sep-20 | 17 | | Sun 20-Sep-20 | 14 | +---------------+-------+
  19. You can't pass identifiers (table/column names) as parameters
  20. If you are starting with this (which could be the result from a table subquery) ... +----+----------------------+------------+-----------------+----------+ | id | disciplina | moduloUfcd | idcpDisciplinas | anoTurma | +----+----------------------+------------+-----------------+----------+ | 58 | Comunicação Visual | 8599 | 49 | 11 | | 59 | Comunicação Visual | 133 | 49 | 11 | | 60 | Comunicação Visual | 134 | 49 | 10 | +----+----------------------+------------+-----------------+----------+ then this query ... SELECT group_concat(id separator ', ') as ids , disciplina , group_concat(moduloUfcd separator ', ') as mods , idcpDisciplinas , anoTurma FROM gmc GROUP BY idcpDisciplinas, anoTurma; gives ... +--------+----------------------+-----------+-----------------+----------+ | ids | disciplina | mods | idcpDisciplinas | anoTurma | +--------+----------------------+-----------+-----------------+----------+ | 60 | Comunicação Visual | 134 | 49 | 10 | | 58, 59 | Comunicação Visual | 8599, 133 | 49 | 11 | +--------+----------------------+-----------+-----------------+----------+
  21. Also note that the above code does not put the lastname into the $result variable.
  22. Seems to me that the best approach would be to fix the problem instead of disabling the warning.
  23. 1 ) You don't need the mysql_real_escape_string. That is for strings (the clue is in the name). $cat_id is an int. 2 ) The mysql_xxxx functions are obsolete. If you ever get around to upgrading you version of PHP to someting less than 5 years old, they won't work. Switch to using PDO class.
  24. Not knowing what data you have, I'm going to guess that is correct. Now do the same in your php code.
  25. If $cat_id contains "" then the query will fail with a syntax error. But we don't know what's in it, nor do we know what's in your table - and we certainly have no idea what "don't work" means unless you tell us. Check if your query gave an error message.
×
×
  • 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.