-
Posts
24,609 -
Joined
-
Last visited
-
Days Won
832
Everything posted by Barand
-
If you want a recursive approach it is far more efficient to read all your data into a suitably structured array then process the array recursively.
-
Your subtractive array is far from exhaustive. You have IX and VI so why not XI and IV. And II as in XIIX? This recursive solution implements the above approach giving XIIX 18 IXCX 101 XICX 99 MCMIL 1949 MCMXLIX 1949 MCMXXXXVIIII 1949 MDCCCCXXXXVIIII 1949 Code <?php // Test values $nums = ['XIIX', 'IXCX', 'XICX', 'MCMIL', 'MCMXLIX', 'MCMXXXXVIIII', 'MDCCCCXXXXVIIII' ]; echo '<pre><br>'; foreach ($nums as $rn) { printf( "\t%-20s %4d<br>", $rn, ad_decimo($rn) ); } echo '</pre>'; function ad_decimo($str) { $M = 1000; $D = 500; $C = 100; $L = 50; $X = 10; $V = 5; $I = 1; foreach (str_split(strtoupper($str)) as $r) { if (!isset($$r)) return 0; $ra[] = [ 'r' => $r, 'v' => $$r, 's' => 1 ]; } $d = 0; // // check each value and flag if it is subtractive // then process contiguous subtractive values as a sub-numeral (recursive) // foreach ($ra as $p => $a) { $ra[$p]['s'] = subtracto($ra, $p, $a['v']); } $substr = ''; foreach ($ra as $p => $a) { if ($a['s'] == 1) { $d -= ad_decimo($substr); // recursive call to evaluate the substring of consecutive subtractive letters $substr = ''; $d += $a['v']; } else { $substr .= $a['r']; } } $d -= ad_decimo($substr); // process remaining group return $d; } function subtracto($rarr, $pos, $val) { // a value is subtractive if followed by a higher value for($i=$pos+1, $k=count($rarr); $i<$k; $i++) { if ($rarr[$i]['v'] > $val) return -1; } return 1; } ?>
-
Looks like solution is to look for substrings of lower value number prior to an even larger one IX C X |___| | -9 100 10
-
PS However, I think IXCX should give 101 so I still have some work to do! IXCX = -9 + 100 + 10 = 101
-
I wouldn't say XICX was any more invalid than XIIX. Unusual, yes, XICX = -10 - 1 + 100 + 10 = 99 The rule being any smaller number occuring before a larger number is subtracted.
-
Just for fun ... echo ad_decimo('XIIX'); // 18 (used by Roman 18th legion) echo ad_decimo('MCMIL'); // 1949 echo ad_decimo('MCMXLIX'); // 1949 function ad_decimo($str) { $M = 1000; $D = 500; $C = 100; $L = 50; $X = 10; $V = 5; $I = 1; foreach (str_split(strtoupper($str)) as $r) { if (!isset($$r)) return 0; $ra[] = [ 'r' => $r, 'v' => $$r ]; } $d = 0; foreach ($ra as $p => $a) { $d += $a['v'] * subtracto($ra, $p, $a['v']); } return $d; } function subtracto($rarr, $pos, $val) { // is there a higher value letter following this one? for($i=$pos+1, $k=count($rarr); $i<$k; $i++) { if ($rarr[$i]['v'] > $val) return -1; } return 1; }
-
group multiple inputs with the same ID into a single row in html
Barand replied to sashavalentina's topic in MySQL Help
I suspect your notification insert may be failing with an "unknown column" error. (You appear to have a space added in one of the column names) Implement database error checking. I also suspect that the concept of data normalization is completely foreign to you. You currenty have +-------------------+ +-------------------+ +-----------------------+ | order_price | | ordered_items | | notifications | +-------------------+ +-------------------+ +-----------------------+ | id |-------+ | id | | id | | order_id | +------| order_id |------+ | notification_title | +-------------------+ | user_id | | | cn_notification_title | | seller_id | | | m_notification_title | | product_name | | | notification_date | | quantity | | | notification_text | | purchase_price | | | cn_notification_text | +-------------------+ | | m_notification_text | | | user_id | | | seller_id | +-------| order_id | | product_id | | user_notify | | seller_notify | | admin_notify | +-----------------------+ Basically Data should be stored in one place only (with exception of ids which are used for the relational joins) Derived data should not be stored Your notifications table, with the exception of the date and final three (repeated) columns is either duplicated or derived. I would suggest the following model +-------------------+ +-------------------+ +----------------+ | order | | order_item | | product | +-------------------+ +-------------------+ +----------------+ | order_id |------+ | id | +-------| product_id | | user_id | +------<| order_id | | | product_name | | order_date | | | product_id |>-----+ | seller_id | +-------------------+ | | quantity | | purchase_price | | +-------------------+ +----------------+ | | | +-------------------+ +-----------------------+ | | notification_log | | notification_title | | +-------------------+ +-----------------------+ | | | | id | | | id | +------| who_notified | |------<| order_id | | | notification_title | | who_notified |>------+ +-----------------------+ | date_notified | +-------------------+ Also, you should be using prepared statements for those inserts instead of putting data values directly into the queries. Consider using transactions when inserting multiple records at one time. -
Doesn't sound like a good idea. That would rule out, for example, MCM = 1900
-
-
Here's my attempt DATA mysql> select * from ajoo -> order by user, recno; +-------+----------+---------+---------+ | recno | user | v_score | rollavg | +-------+----------+---------+---------+ | 6 | mina1111 | 4 | 3.2500 | | 7 | mina1111 | 3 | 3.2000 | | 8 | mina1111 | 2 | 3.2000 | | 9 | mina1111 | 4 | 3.4000 | | 10 | mina1111 | 5 | 3.6000 | | 11 | mina1111 | 0 | 2.8000 | | 12 | mina1111 | 1 | 2.5000 | | 13 | mina1111 | 1 | 1.7500 | | 14 | mina1111 | 1 | 0.7500 | | 1 | nina1234 | 3 | NULL | | 4 | nina1234 | 3 | 2.5000 | | 5 | nina1234 | 4 | 3.0000 | | 15 | nina1234 | 5 | NULL | | 17 | nina1234 | 2 | 2.0000 | | 22 | nina1234 | 2 | NULL | +-------+----------+---------+---------+ QUERIES -- -- create temp table a -- CREATE TEMPORARY TABLE temp_a SELECT a.recno , a.v_score , @count := CASE WHEN user = @prevu THEN @count+1 ELSE 1 END AS reccount , @prevu := user AS user FROM ajoo a JOIN (SELECT @count:=0, @prevu:=NULL) AS init ORDER BY user, recno ; -- -- create temp table b -- (copy of temp_a) -- CREATE TEMPORARY TABLE temp_b SELECT * FROM temp_a ; -- -- get results -- SELECT av.user , avg5 , tot3 FROM ( SELECT user , AVG(v_score) as avg5 FROM ( SELECT a.user , v_score FROM temp_a a JOIN ( SELECT user , COUNT(*) AS maxrec FROM ajoo GROUP BY user ) max ON a.user = max.user AND a.reccount > max.maxrec - 5 ) tots GROUP BY user ) av JOIN ( SELECT user , SUM(v_score) as tot3 FROM ( SELECT b.user , v_score FROM temp_b b JOIN ( SELECT user , COUNT(*) AS maxrec FROM ajoo GROUP BY user ) max ON b.user = max.user AND b.reccount > max.maxrec - 3 ) tots GROUP BY user ) tot USING (user) ; RESULTS +----------+--------+------+ | user | avg5 | tot3 | +----------+--------+------+ | mina1111 | 1.6000 | 3 | | nina1234 | 3.2000 | 9 | +----------+--------+------+
-
https://dev.mysql.com/doc/refman/5.7/en/temporary-table-problems.html
-
versus result = "XYZ4"
-
Once you learn the difference between a string value and a variable name, you should crack the problem. echo juneteenth(2021); function juneteenth($yr) { $res = new DateTime($yr.'-06-19'); switch ($res->format('w') ) { case 6: return $res->modify("-1 days")->format('Y-m-d'); case 0: return $res->modify("-2 days")->format('Y-m-d'); default: return $res->format('Y-m-d'); } }
-
Isn't that tomorrow on the 19th?
-
That's a lot better IMO And the buttons still work
-
It would be better if the toolbar switched to two rows instead of losing half of it.
-
-
Rotate the phone to landscape position. The <> button appears on wider screens.
-
See number of different types of booking per organisation
Barand replied to Adamhumbug's topic in MySQL Help
I'lll state my assumptions 1 ) The data looks like this prs_o prs_op prs_pr +------+-----------+ +-------+---------+ +-------+----------+------------+--------------+ | o_id | o_name | | op_id | op_o_id | | pr_Id | pr_op_id | pr_ptd_id | pr_ip2ptd_id | +------+-----------+ +-------+---------+ +-------+----------+------------+--------------+ | 1 | Company A | | 1 | 1 | | 1 | 1 | 641 | NULL | | 2 | Company B | | 2 | 1 | | 2 | 2 | 641 | NULL | | 3 | Company C | | 3 | 1 | | 3 | 3 | NULL | 1101 | +------+-----------+ | 4 | 1 | | 4 | 4 | NULL | 1101 | | 5 | 2 | | 5 | 5 | NULL | 1101 | | 6 | 2 | | 6 | 6 | NULL | 1101 | | 7 | 2 | | 7 | 7 | NULL | 1101 | | 8 | 3 | | 8 | 8 | NULL | 1101 | | 9 | 3 | | 9 | 9 | NULL | 1101 | | 10 | 3 | | 10 | 10 | NULL | 1101 | | 11 | 3 | | 11 | 11 | NULL | 1101 | | 12 | 3 | | 12 | 12 | 641 | NULL | | 13 | 3 | | 13 | 13 | 641 | NULL | +-------+---------+ +-------+----------+------------+--------------+ 2 ) The table and join structure looks like this +--------------+ +--------------+ +--------------+ | prs_o | | prs_op | | prs_pr | +--------------+ +--------------+ +--------------+ | o_id |-----\ | op_id |------\ | pr_id | | o_name | \---<| op_o_id | \-----<| pr_op_id | +--------------+ +--------------+ | pr_ptd_id | | pr_ip2ptdid | +--------------+ Query SELECT o_id , o_name , SUM(pr_ptd_id = 641) as Tot641 , SUM(pr_ip2ptd_id = 1101) as Tot1101 FROM prs_pr pr JOIN prs_op op ON pr.pr_op_id = op.op_id JOIN prs_o o ON op.op_o_id = o.o_id GROUP BY o.o_id; Results +------+-----------+--------+---------+ | o_id | o_name | Tot641 | Tot1101 | +------+-----------+--------+---------+ | 1 | Company A | 2 | 2 | | 2 | Company B | | 3 | | 3 | Company C | 2 | 4 | +------+-----------+--------+---------+ -
See number of different types of booking per organisation
Barand replied to Adamhumbug's topic in MySQL Help
That is precisely the problem I hve been having - there is no "pr_o_id" in the structure you gave me ... -
See number of different types of booking per organisation
Barand replied to Adamhumbug's topic in MySQL Help
Sorry, but with those typos I don't know which ones are correct and which aren't. ( therefore which columns should really be used for the join betwen prs_op and prs_pr). All those op_id and o_id names are confusing. I don't know if you really mean o_id when you say op_id or vice versa. -
As any suggested changes could be inferred to be criticisms of your code, you have effectively gagged us and painted yourself into a corner as far as getting help is concerned. Also, and this is a criticism of the post, not the code, you have told us or shown us what you are trying to do when a link is clicked.
-
See number of different types of booking per organisation
Barand replied to Adamhumbug's topic in MySQL Help
A dump of the structure and test data would have benn appreciated. Now, having created the tables from the structures you provided and then having entered the data manually from that useless picture of the data, I now find that when I run your queries, some of the column names in your structures do not match those used in your queries. Aaaaargh! I am rapidly losing patience with this problem. It may take longer than expected. Much longer