-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Best way to check if one array values less than another array?
Barand replied to RuleBritannia's topic in PHP Coding Help
You could simplify that to function c($a,$b) { return ($a > $b); } -
Best way to check if one array values less than another array?
Barand replied to RuleBritannia's topic in PHP Coding Help
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>'; -
I agree with Scootstah though, it would be better to have return empty($trimvalue) ? false : $trimvalue;
-
function fieldExists($fieldValue){ $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue))); return empty($trimvalue) ? 'No' : $trimvalue; }
-
Well if you don't know which table you are querying for the pagination, how do you expect us to know?
-
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.
-
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.
- 9 replies
-
- ajoo
- sql queryies in php
-
(and 2 more)
Tagged with:
-
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 ? '✓' : ''; 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>
-
Have a look at stored procedures and triggers http://dev.mysql.com/doc/refman/5.6/en/stored-programs-views.html
- 9 replies
-
- ajoo
- sql queryies in php
-
(and 2 more)
Tagged with:
-
Failing to replicate BASIC INT() division as PHP code
Barand replied to Witblitz's topic in PHP Coding Help
seems to depend on where you do the rounding down $length = 1802; $pitch = 50; echo floor(floor(($length-10)/$pitch) - 0.; //---> 34 -
Failing to replicate BASIC INT() division as PHP code
Barand replied to Witblitz's topic in PHP Coding Help
What values are you feeding into the function? -
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
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; -
that's right
-
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
$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() -
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>';
-
Sorry, change COUNT(*) to COUNT(viewDate). COUNT(*) is counting the records and there is always at least 1
-
But $row was not defined because the query failed (the undefined alias that I forgot)
-
which is why I told him to remove the references to it
-
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.
-
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
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. -
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
-
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";
-
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
You say the above code worked and that is using mysqli. You cannot mix mysql and mysqli functions using the same connection. -
Quick question. - Accessing Database with MySQL and PHP
Barand replied to GumbiRo's topic in PHP Coding Help
try using mysqli_real_escape_string() as you are using a mysqli connection -
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";