Jump to content

Barand

Moderators
  • Posts

    24,613
  • Joined

  • Last visited

  • Days Won

    834

Everything posted by Barand

  1. what is table1? what is table2? what is table3? what is a "pick"? what is a "confidence value"? Perhaps you show some sample data for each table?
  2. Which bit are you having a problem with? Show your current code
  3. here's a replacement "combine indexes" section of the code // // combine indexes // uasort($filtered, function($a,$b) {return count($b) - count($a);}); foreach ($filtered as $i=>$a) { foreach ($filtered as $j=>$b) { if ($i==$j) continue; if (count($a)<2 || count($b)<2) continue; if (array_intersect($a, $b)) { $filtered[$j] = array_unique(array_merge($a,$b)); } } } foreach ($filtered as $k => $kwarr) { if (count($kwarr) == 0) { $otheritems[] = $k; } elseif (count($kwarr) > 1) { $newkw = join(' & ', $kwarr); $occurs = []; foreach ($kwarr as $kw) { if (isset($kwindex[$kw])) { $occurs = array_merge($occurs, $kwindex[$kw]); // combine individual lists unset($kwindex[$kw]); // then remove them } } sort($occurs); $newkw = join(' & ', $kwarr); $kwindex[$newkw] = array_unique($occurs); // add the combined index } } $kwindex = array_filter($kwindex);
  4. or $arr = [5,3,2]; // orig array $rem = [3,2]; // array of items to be removed $arr = array_diff($arr,$rem);
  5. should be __construct() 2 underscores
  6. +1. I always generate a pdf if printed output is required. (Has the advantage that it can be printed, saved, emailed or just viewed).
  7. $p2=0; $found = 0; while(($p1 = strpos($str, '<mofish', $p2)) !== false) { $p2 = strpos($str, '/>', $p1); $x = (substr($str,$p1,$p2-$p1+2)); if (strpos($x, 'id="number2"')) { $found = 1; break; } } if ($found) { $newstr = str_replace($x, $replace, $str); }
  8. try $str = '<div class="text"> <mofish id="number1" type="number1" title="true" /> </div> <div class="text"> <mofish id="number2" type="number2" title="true" /> </div>'; $replace = 'whatever'; $p2=0; while(($p1 = strpos($str, '<mofish', $p2)) !== false) { $p2 = strpos($str, '/>', $p1); $x = (substr($str,$p1,$p2-$p1+2)); if (strpos($x, 'id="number2"')) { break; } } $newstr = str_replace($x, $replace, $str);
  9. by changing the code to meet the new requirements
  10. what is wrong with number_format ?
  11. Yes. Also note, in part answer to you other post, there is a NULL-safe equality operator http://dev.mysql.com/doc/refman/5.6/en/comparison-operators.html#operator_equal-to
  12. The query I gave you in a previous post was to populate your dropdown menu. (http://forums.phpfreaks.com/topic/297658-how-to-select-two-tables-and-compare-two-columns-and-determine-or/?do=findComment&comment=1518346) It did NOT contain "WHERE b.book_id = $posted_id". You seem to be confusing issues. The WHERE clause is for selecting the selected book from you database on the subsequent page.
  13. Dislike++ Not to mention unnecessarily creating them as (evil) GLOBALS
  14. the query would be SELECT MONTH(due_date) as month , SUM(cust_order_total) AS order_total_sum FROM orders WHERE YEAR(due_date) = 2014 GROUP BY month
  15. My 0.02 worth <?php $narray[]="1 bla blb ala bla bla facebook dfg"; $narray[]="2 b la bl twitter ba la bla bl dfg a"; $narray[]="3 bla sdf asd fb la fg dfg blb ala bla bla clinton"; $narray[]="4 b lad fg bl obama ba la dfg clinton dsf bla bla"; $narray[]="5 bla blb dfg dfg ala bla bla ds fg mircosoft"; $narray[]="6 b la bl Obama bd fg sdf a la bla bla"; $narray[]="7 db la dbl obama bd dfg sdf ad la bla bla"; $narray[]="8 bla df gd sfg blb ala bla bla twitter"; $narray[]="9 s ons ti ges sdf about as df"; $narray[]="10 Twitter s ons ti ges sdf as df"; $narray[]="11 s ons ti ges Obama sdf as df"; $narray[]="12 s Clinton ons ti ges sdf as df"; $filtered = filter_my_array($narray); // keywords only array $kwindex = index_keywords($filtered); // index of keywords $keywords = array_keys($kwindex); $otheritems = []; // // combine indexes // foreach ($filtered as $k => $kwarr) { if (count($kwarr) == 0) { $otheritems[] = $k; } elseif (count($kwarr) > 1) { $newkw = join(' & ', $kwarr); $occurs = []; foreach ($kwarr as $kw) { if (isset($kwindex[$kw])) { $occurs = array_merge($occurs, $kwindex[$kw]); // combine individual lists unset($kwindex[$kw]); // then remove them } } sort($occurs); $kwindex[$newkw] = array_unique($occurs); // add the combined index } } // // create highlighting replacement textss // $replace = []; foreach ($keywords as $kw) { $replace[] = "<span class='hi'>$kw</span>"; } // // create output of the indexed list // ksort($kwindex); $output = ''; foreach ($kwindex as $kw => $items) { $output .= "<h4>$kw</h4><ul>"; foreach ($items as $i) { $output .= "<li>" . str_ireplace($keywords, $replace, $narray[$i]) . "</li>\n"; } $output .= "</ul>\n"; } if (count($otheritems) > 0) { $output .= "<h4>Non-keyword items</h4><ul>"; foreach ($otheritems as $i) { $output .= "<li>{$narray[$i]}</li>\n"; } $output .= "</ul>\n"; } /******************************************************************************* * helper functions ********************************************************************************/ function filter_my_array($array) { // reduces the lines of text to arrays of the keywords in the line $results = []; foreach ($array as $k => $str) { $str = strtolower($str); $a = array_filter(explode(' ', $str), 'remove_noise'); $results[$k] = $a; } return $results; } function remove_noise($x) { $stopWords = array('about','an','and','are','as','at','be','by','com','de','en','for','from', 'how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where', 'who','will','with','und','the','www'); return strlen($x) > 3 && !in_array($x, $stopWords); } function index_keywords($array) { // gets the line numbers containing each keyword $results = []; foreach ($array as $k => $kwarr) { foreach ($kwarr as $kw) { $results[$kw][] = $k; } } return $results; } ?> <html> <head> <title>Keyword Index</title> <style type='text/css'> .hi { font-weight: 700; color: red; } </style> </head> <body> <?=$output?> </body> </html> Results <html> <head> <title>Keyword Index</title> <style type="text/css"> .hi { font-weight: 700; color: red; } </style> </head> <body> <h4>facebook</h4><ul><li>1 bla blb ala bla bla <span class="hi">facebook</span> dfg</li> </ul> <h4>mircosoft</h4><ul><li>5 bla blb dfg dfg ala bla bla ds fg <span class="hi">mircosoft</span></li> </ul> <h4>obama & clinton</h4><ul><li>3 bla sdf asd fb la fg dfg blb ala bla bla <span class="hi">clinton</span></li> <li>4 b lad fg bl <span class="hi">obama</span> ba la dfg <span class="hi">clinton</span> dsf bla bla</li> <li>6 b la bl <span class="hi">obama</span> bd fg sdf a la bla bla</li> <li>7 db la dbl <span class="hi">obama</span> bd dfg sdf ad la bla bla</li> <li>11 s ons ti ges <span class="hi">obama</span> sdf as df</li> <li>12 s <span class="hi">clinton</span> ons ti ges sdf as df</li> </ul> <h4>twitter</h4><ul><li>2 b la bl <span class="hi">twitter</span> ba la bla bl dfg a</li> <li>8 bla df gd sfg blb ala bla bla <span class="hi">twitter</span></li> <li>10 <span class="hi">twitter</span> s ons ti ges sdf as df</li> </ul> <h4>Non-keyword items</h4><ul><li>9 s ons ti ges sdf about as df</li> </ul> </body> </html>
  16. Find users not = logged user and match using left join against friends of logged user and see which are missing SELECT u.id , u.name as non_friend FROM user u LEFT JOIN friend f ON u.id = f.friend_id AND f.user_id = $loggedUser WHERE u.id <> $loggedUser AND f.friend_id IS NULL ORDER BY id
  17. exactly what I told the OP five days ago http://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/?do=findComment&comment=1518615
  18. $query contains a result object, not the the value of the ip_add column. You need to fetch a row from the result then check the ip_add column
  19. try select p.id , p.name from products p inner join ( SELECT product_id as id FROM properties WHERE `key` = 'Format' AND value = 'Book' ) as book USING (id) inner join ( SELECT product_id as id FROM properties WHERE `key` = 'Format' AND value = 'Audio' ) as audio USING (id) inner join ( SELECT product_id as id FROM properties2 WHERE continent = 'Asia' AND country = 'India' ) as india USING (id) inner join ( SELECT product_id as id FROM properties2 WHERE continent = 'Asia' AND country = 'China' ) as china USING (id);
  20. What is the structure of the two tables? What conditions are you searching for?
  21. . . . or fraudulently call it their site.
  22. ... or you could put the connection credentials in a separate file which you include() in your scripts so they only need change one place. This applies to procedural and OOP.
  23. This method allocates 15% of the planets to level 1, 15% of the remaining planets to level 2, and so on. The results for 10,000 planets were Level 25 : 30 Level 24 : 36 Level 23 : 42 Level 22 : 49 Level 21 : 58 Level 20 : 68 Level 19 : 81 Level 18 : 95 Level 17 : 111 Level 16 : 131 Level 15 : 154 Level 14 : 181 Level 13 : 213 Level 12 : 251 Level 11 : 295 Level 10 : 347 Level 9 : 409 Level 8 : 481 Level 7 : 565 Level 6 : 665 Level 5 : 783 Level 4 : 921 Level 3 : 1083 Level 2 : 1275 Level 1 : 1676 TOTAL :10000 Time taken : 17.430 seconds The code <?php include("db_inc.php"); // defines HOST, USERNAME and PASSWORD try { $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); } catch(Exception $e) { die("DB connection error"); } $t1 = microtime(1); // start the clock /******************************************************************** * CREATE THE TEST DATA planet TABLE (uncomment if required) ********************************************************************* $db->query("DROP TABLE IF EXISTS planet"); $sql = "CREATE TABLE planet ( planet_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, planetname VARCHAR(30), level INT )"; $db->query($sql); $sql = "INSERT INTO planet (planetname) VALUES (?)"; $stmt = $db->prepare($sql); $stmt->bind_param('s', $pn); for ($i=1; $i<=10000; $i++) { $pn = "Planet $i"; $stmt->execute(); } $stmt->close(); *********************************************************************/ /******************************************************************** * define the %age distribution of the levels *********************************************************************/ $distrib_pcent = 15; /******************************************************************** * how many planets (N)? *********************************************************************/ $sql = "SELECT COUNT(*) FROM planet"; $res = $db->query($sql); list($N) = $res->fetch_row(); $planet_levels = []; // define empty array $planets = range(1,$N); shuffle($planets); // planet ids in random order /******************************************************************** * fill planet_level array with levels to be applied to each planet *********************************************************************/ $key = 0; $n = $N; for ($level = 1; $level <= 25; $level++) { $k = floor($n * $distrib_pcent / 100); // how many planets get this level? $n -= $k; // $n = remaining planets if ($n < 0) { die( $level . ' level reached<br>'); // check we don't run out of planets } for ($i=0; $i<$k; $i++) { $planet = $planets[$key++]; // next random planet id $planet_levels[$planet] = $level; // set its level } } /****************************************************************************** * update the planets with their assigned levels *******************************************************************************/ $db->query("UPDATE planet SET level=1"); // set default level $sql = "INSERT INTO planet (planet_id, level) VALUES (?,?) ON DUPLICATE KEY UPDATE level = VALUES(level)"; $stmt = $db->prepare($sql); $stmt->bind_param('ii',$pid,$level); foreach ($planet_levels as $pid=>$level) { if ($level != 1) $stmt->execute(); // EDIT: only need update those != 1 } $t2 = microtime(1); // stop the clock; /****************************************************************************** * check the results *******************************************************************************/ $sql = "SELECT level, COUNT(*) as tot FROM planet GROUP BY level ORDER BY tot"; $res = $db->query($sql); $tdata = ''; $totalPlanets = 0; while (list($lev, $tot) = $res->fetch_row()) { $tdata .= sprintf("Level %2d : %4d\n", $lev, $tot); $totalPlanets += $tot; } ?> <html> <head> <title>Planet levels</title> </head> <body> <pre> <?php echo $tdata; printf("<b></b>%8s :%5d</b> Time taken : %6.3f seconds\n", 'TOTAL', $totalPlanets, $t2-$t1); ?> </pre> </body> </html>
  24. Then use whatever you get that is unique to a customer (name/postcode ?). How were you planning to get round the problem with your array merge?
×
×
  • 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.