Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. You could simplify that to function c($a,$b) { return ($a > $b); }
  2. Try $array1 = array( 'width' => 400, 'height' => 300, 'start' => 10, 'end' => 100 ); $array2 = array( 'width' => 400, 'height' => 30, 'start' => 20, 'end' => 10 ); foreach ($array1 as $k => $v) { if ($array2[$k] >= $v) { unset($array2[$k]); } } echo '<pre>',print_r($array2, true),'</pre>';
  3. I agree with Scootstah though, it would be better to have return empty($trimvalue) ? false : $trimvalue;
  4. function fieldExists($fieldValue){ $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue))); return empty($trimvalue) ? 'No' : $trimvalue; }
  5. Well if you don't know which table you are querying for the pagination, how do you expect us to know?
  6. Surely, all you need will be the userid to identify the user. Put the userid in a hidden field in the form with comments and pass that.
  7. Not sure to whom the "his" refers in that question. Player A can update Player A's score and Player B can update Player B's score. You should not be allowing a player to update any score that is not their own.
  8. I'd do it like this <?php session_start(); if (!isset($_SESSION['questions'])) { $_SESSION['questions'] = array( 'n1' => array(), 'n2' => array(), 'ans' => array() ); $_SESSION['count'] = 0; $numbers = range(1,30); // we want to make sure that questions are not repeated // pick 10 numbers at random $arr = array_rand($numbers, 10); foreach ($arr as $k) $_SESSION['questions']['n1'][] = $numbers[$k]; // remove the selected numbers from the array $numbers = array_diff($numbers, $_SESSION['questions']['n1']); // pick 10 more from the remaining numbers $arr = array_rand($numbers, 10); foreach ($arr as $k) $_SESSION['questions']['n2'][] = $numbers[$k]; } if (isset($_POST['ans'])) { $_SESSION['questions']['ans'][$_SESSION['count']] = $_POST['ans']; ++$_SESSION['count']; } ?> <html> <head> <title>Math</title> </head> <body> <form method='post' action=''> <?php $q = $_SESSION['count']+1; if ($q < 11) { echo <<<EOT <h2>+</h2> <h5>Question $q</h5> {$_SESSION['questions']['n1'][$_SESSION['count']]} + {$_SESSION['questions']['n2'][$_SESSION['count']]} <br><br> Your answer: <input type='text' size='3' name='ans'><br> <br><br> <input name='btnsubmit' type='submit' value='Next'> <input name='btnreset' type='reset' value='Delete'> EOT; } else { echo "<h5>Results</h5> <table border='1' cellpadding='3' style='border-collapse:collapse'>\n <tr><td></td><td>Question</td><td>Answer</td><td>Your answer</td><td>Correct</td></tr>\n"; for ($i=0; $i<10; $i++) { $qno = $i+1; $qtext = "{$_SESSION['questions']['n1'][$i]} + {$_SESSION['questions']['n2'][$i]}"; $ans = $_SESSION['questions']['n1'][$i] + $_SESSION['questions']['n2'][$i]; $yours = $_SESSION['questions']['ans'][$i]; $chk = $ans==$yours ? '&check;' : ''; echo "<tr><td>$qno</td><td>$qtext</td><td>$ans</td><td>$yours</td><td>$chk</td></tr>\n"; } echo "</table>\n<br><input name='btnsubmit' type='submit' value='New test'>"; unset($_SESSION['questions']); } ?> </form> <script type='text/javascript'> onload = function(){document.forms[0].ans.focus();} </script> </body> </html>
  9. Have a look at stored procedures and triggers http://dev.mysql.com/doc/refman/5.6/en/stored-programs-views.html
  10. seems to depend on where you do the rounding down $length = 1802; $pitch = 50; echo floor(floor(($length-10)/$pitch) - 0.; //---> 34
  11. What values are you feeding into the function?
  12. It seems like all you are interested in knowing is "how many records are there?". The most inefficient way to do this is fetch all the records then count them. To make it worse you are counting them all twice you loop through the records and set $counter to the record count you then call num_rows to get the count $sql = "SELECT COUNT(*) as count FROM members WHERE username = '" . $mysqli->real_escape_string($username) . "'"; if(!$result = $mysqli->query($sql)){ die('There was an error running the query [' . $db->error . ']'); } list($count) = $result->fetch-row(); $ready = $count==0;
  13. that's right
  14. $sql should just define the string. $sql = "SELECT 1 FROM members WHERE username = '" . mysqli_real_escape_string($mysqli,$username) . "'"; Note that the string value '$username' needs to be inside quotes in a query. You also need to pass the mysqli connection as the first parameter in mysqli_query()
  15. Plan B if you need an array of the totals $totals = array_fill_keys(range(1,12), 0); $query = "SELECT MONTH(viewDate) as month , COUNT(*) AS `month_count` FROM jobViews GROUP BY month"; $result = $db->query($query); while($row = $result->fetch_assoc()){ $totals[$row['month']] = $row['month_count']; } echo '<pre>',print_r($totals, true),'</pre>';
  16. Sorry, change COUNT(*) to COUNT(viewDate). COUNT(*) is counting the records and there is always at least 1
  17. But $row was not defined because the query failed (the undefined alias that I forgot)
  18. which is why I told him to remove the references to it
  19. Once you have the value of $row['col_1'] the processing of the status gif is exactly the same whether you use mysql or mysqli.
  20. No. String values need to be in quotes. $result = $mysqli->prepare("SELECT 1 FROM members WHERE username = '" . mysqli_real_escape_string($mysqli,$username) . "'"); Use output from $mysqli->error to find errors in your queries.
  21. try removing the "m" table alias $query = "SELECT month , COUNT(*) AS `month_count` FROM months LEFT JOIN jobViews ON month = MONTH(viewDate) GROUP BY month";
  22. You say the above code worked and that is using mysqli. You cannot mix mysql and mysqli functions using the same connection.
  23. try using mysqli_real_escape_string() as you are using a mysqli connection
  24. one way $sql = "CREATE TEMPORARY TABLE months (month int)"; $db->query($sql); $sql = "INSERT INTO months VALUES (1),(2),(3),(4),(5),(6),(7),(,(9),(10),(11),(12)"; $db->query($sql); $query = "SELECT m.month , COUNT(*) AS `month_count` FROM months LEFT JOIN jobViews ON m.month = MONTH(viewDate) GROUP BY month";
×
×
  • 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.