Jump to content

Barand

Moderators
  • Posts

    24,602
  • Joined

  • Last visited

  • Days Won

    830

Everything posted by Barand

  1. I have been following this topic (I say "following" but that is an exaggeration) and I admit I totally clueless about what the inputs to this process are what the goal of the process is what the rules are to achieve the goal and what the constraints are what T1, T2, shift3, shift4 are Apart from that...
  2. it is a link to the PHP reference manual's section on arrays. Read it.
  3. https://www.php.net/manual/en/language.types.array.php
  4. How many topics do you intend creating for this problem? (there is already one too many).
  5. Sounds a simple problem involving a couple of arrays and a loop. What have you tried so far?
  6. "In general", don't write code which relies on a variable having been previously defined without first defining that variable or checking if the variable has been defined before you attempt to reference it.
  7. I assume this post is just so you can have a rant. If it were a serious request for help then you should have provided some information about the problem, the error message you are receiving and the code that is giving error Then, perhaps, someone can help. BTW, coincidentally, I used the same syntax myself earlier today (though not to that extreme without any regard for where and how the variable are to be used, and I am not happy about that $row[] in there) when setting default input values for a form
  8. Why so much wasted space in topic headings in default theme/grid view ? and, in same grid view using dark theme, grid columns are 100% wide insted of 25%
  9. Barand

    group by

    That's what I thought.
  10. Barand

    group by

    If I am understanding the question correctly, the query to populate Box 1 is SELECT DISTINCT title FROM xmas ORDER BY title; On your page, use an onchange handler to send an AJAX request which sends the title then receives the results of this second query to populate Box 2 SELECT artist FROM xmas WHERE title = ?
  11. I take it that the user->asset relationship is 1 to many, but an asset can only be assigned to one user? Hence two tables. You need somethng like this $q = "SELECT t1.hid , t1.description , t2.name FROM assets t1 LEFT JOIN users t2 ON t1.uid = t2.uid WHERE t1.hid = ? "; (I'll move this to the MySQL forum)
  12. Yes, change the file name from xxx.html to xxx.php.
  13. 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.
  14. 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 | +----------------+
  15. 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 | +------------+
  16. 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"
  17. Your json has been truncated and therefore invalid.
  18. Ask yourself "Why are you running that query?"
  19. Define "allows a connection"
  20. 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
  21. 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 ] );
  22. 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?
  23. 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 | +----------------+------------+
  24. Correction to my above code <input type="checkbox" name="fruit['apple']" id="apple" value="1"> ^ ^ | | remove quotes in input names
  25. I've added the tags for you - this time. Next time, use the "code" icon
×
×
  • 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.