Jump to content

Barand

Moderators
  • Posts

    24,565
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Nothing there resembles what you had in your original post, which was
  2. Post the raw data that you get in $order before you start your processing and we'll see if we can help with your formatting
  3. The normal order is high to low and a number to that precedes a higher value is subtracted. The first "III" are therefore subtracted from the following V, giving 5 + 2 + 1 + 1 + 1 = 10
  4. As far as I know, it became "open source" as soon as I posted it here. But if a small toke of appreciation would ease your conscience if you use it ....
  5. This illustrates the way my algorithm works ... IXCM = 1000 - IXC = 1000 - ( 100 - IX ) = 1000 - ( 100 - ( 10 - 1 ) ) = 1000 - ( 100 - 9 ) = 1000 - 91 = 909
  6. 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.
  7. 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; } ?>
  8. Looks like solution is to look for substrings of lower value number prior to an even larger one IX C X |___| | -9 100 10
  9. PS However, I think IXCX should give 101 so I still have some work to do! IXCX = -9 + 100 + 10 = 101
  10. 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.
  11. 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; }
  12. 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.
  13. Doesn't sound like a good idea. That would rule out, for example, MCM = 1900
  14. You could use CSS, for example #table1 { transform : scale(0.5); } Simple example Code
  15. 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 | +----------+--------+------+
  16. https://dev.mysql.com/doc/refman/5.7/en/temporary-table-problems.html
  17. versus result = "XYZ4"
  18. @akashmishra - Brilliant! - exactly what @requinix said in his reply 19 hours earlier.
  19. 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'); } }
  20. Isn't that tomorrow on the 19th?
  21. That's a lot better IMO And the buttons still work
  22. It would be better if the toolbar switched to two rows instead of losing half of it.
  23. https://www.php.net/manual/en/language.variables.external.php
  24. You can always add code tags manually
  25. Rotate the phone to landscape position. The <> button appears on wider screens.
×
×
  • 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.