Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Is the name of your table really "table"?
  2. Given the obsolete nature of the code it's surprising it lasted this long. If it's a hosted site it probable that errors are being written to the error log instead of being displayed. Check the log.
  3. Here's how I'd do it without the windowing functions Note: if you have A 98 B 98 C 95 the A and B a re ranked 1, C is ranked 3. $grade_class = 'JS1'; $grade_name = 'EMMANUEL OKONJI'; $grade_term = 'First Term'; $grade_year = '2022'; $student_class_position = $conn->prepare( "SELECT rank FROM ( SELECT ord.grade_id , @seq := @seq+1 as seq , @rank := CASE WHEN ord.grand_total = @prev THEN @rank ELSE @seq END as rank , @prev := ord.grand_total as grand_total FROM ( SELECT grade_id , g.grand_total FROM student_grade g ORDER BY grand_total DESC LIMIT 18446744073709551615 ) ord JOIN (SELECT @seq:=0, @rank:=0,@prev:=0) init ) ranked JOIN student_grade g ON g.grade_id = ranked.grade_id WHERE class = ? AND name = ? AND term = ? AND year = ? "); $student_class_position->bind_param('ssss', $grade_class, $grade_name, $grade_term, $grade_year); $student_class_position->execute(); $student_class_position->bind_result($position); $student_class_position->fetch(); echo $position; I used a prepared statement so user data is not inserted into the query (SQL injection attack prevention)
  4. It isn't even your thread and, just in case you think we are prepared to host your personal Python blog, I'm closing it.
  5. Go to your profile, click account settings. Signature is in the menu on the left.
  6. Store them presized to the size you want to display. Don't download images whose sizes are 4800x3600 only to display them as, say, 960x720 - it's going to be unnecessarily slower to download them.
  7. Also your WHERE clause syntax looks incorrect. And why are you storing the userFullName when it is easily derived from the first and last names when required? Unnecessary duplication. EG SELECT concat(userFName, ' ', userLName) as fullname FROM guests;
  8. My full explanation... Let's go right back to basics. If you have an array of numbers $arr = [ 100, 200, 300 ]; the to get the sum of those numbers you would start with a 0 total then loop through the array adding each of the numbers to the total. $total = 0; foreach ($arr as $num) { $total += $num; } echo $total; // 600 $expression1 is a similar array but this time each item of the children array is an expression which has to be evaluated. $expression1 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "number", "value"=> 200 ], [ "type" => "number", "value"=> 300 ] ] ]; So we start with a basic evaluate() function which knows how to evaluate a number experession and how to handle $expression1 (an add expression)... function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break; } return $result; } We can now call echo evaluate($expression1); This initial call to the function looks at the expression type and sees it is "add", so it sets the result to 0 and loops through the children to add each one to the result. To do this it has to call evaluate($child) for each one to get the number value to be added. The multiply is very similar to the add except we must start with a result of 1 (otherwise we just multiply everything by 0, resulting in 0). This is the full evalute() function... function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break; case 'multiply': $result = 1; foreach ($exp['children'] as $child) { $result *= evaluate($child); } break; case 'fraction': $result = evaluate($exp['top']) / evaluate($exp['bottom']); break; } return $result; } giving Expression 1 evaluates to 600 Expression 2 evaluates to 200 Expression 3 evaluates to 1.1
  9. OK - here's the "add" case... case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break;
  10. Have a look at the page source and see if there are any error messages.
  11. If your table structure is like this a int not null auto_increment primary key, b varchar(50), c varchar(50) d timestamp not null default CURRENT_TIMESTAMP then you only need to insert values for b and c $stmt = $db->prepare("INSERT INTO mytable (b, c) VALUE (? ?);
  12. In addition to being recursive, your evaluate() function needs to be able to deal with each expression type and calculate thier results. Something like this to get you started (I've done the type='number' case for you) function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = ???; break; case 'multiply': $result = ???; break; case 'fraction': $result = ??? break; } return $result; }
  13. I don't know what the original problem is that your code was supposed to solve, so I have no solution.
  14. Only the first 4 lines of your function will ever execute - a function exits on meeting a return statement and returns that value.
  15. There must be some pages missing from the PDO section of my copy of the manual.
  16. If there are N rows, each row r comprises (N-r) spaces followed by "* " (star-space) repeated r times.
  17. I ask because your output is showing the symptoms of using single quotes around the string instead of double quotes. That is $link->link_url='details.php?id={$id}'; intead of $link->link_url="details.php?id={$id}";
  18. Are you sure that is your actual code?
  19. Slightly different approach then. Store the category query results in an array. $stmt = $pdo->query("SELECT categoryID , categoryName FROM tbl_categories WHERE categoryActive = 1 ORDER BY categoryID "); $cat_options = []; /// store cat data in array foreach ($statement as $row) { $cat_options[$row['categoryID']] = $row['categoryName']; } // main loop here while ($row = $stmt->fetch()) { echo "<select name='categoryID' class='select200px' required> <option value=''>Select...</option>"; foreach ($cat_options as $id => $cat) { $sel = $row['categoryID'] == $id ? 'selected' : ''; echo "<option $sel value='$id'>$cat</option>"; } echo "</select>"; }
  20. Like this $stmt = $pdo->query("SELECT categoryID , categoryName FROM tbl_categories WHERE categoryActive = 1 ORDER BY categoryID "); $cat_options = ''; foreach ($statement as $row) { $cat_options .= "<option value='{$row['categoryID']}'>{$row['categoryName']}</option>"; } // main loop here while (..) { echo "<select name='categoryID' class='select200px' required> <option value="">Select...</option> $cat_options </select>"; }
  21. No one is suggesting you hard code them, just that they should not be created inside the loop. Create them as you do now, but before the loop, and store the options to use inside the loop.
  22. Create the option lists before the loop $options = "<option value='1'>One</option> <option value='2'>Two</option> <option value='3'>Three</option>"; while ... { ... ... echo "<select ... > $options </select>"; ... }
  23. You create a statement object $stmt and loop through the results. While you are looping through these results you create another statement object $stmt thus overwriting the one you are currently processing. Running any queries inside a loop is a bad idea to start with, this takes it to another level.
  24. Not with my method, which sorts them numerically. Original order +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | The Bionic Woman" (1976) {Rancho Outcast (#2.5)} | +-----------------------------------------------------------+ Sorted (...ORDER BY SUBSTRING_INDEX(title, '#', -1)+0) +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | The Bionic Woman" (1976) {Rancho Outcast (#2.5)} | +-----------------------------------------------------------+
  25. The obvious question is "Why not store the #x.xx in a separate column?". But given where you are... SELECT title FROM movie; +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | "The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | "The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | "The Bionic Woman" (1976) {Rancho Outcast (#3.21)} | +-----------------------------------------------------------+ SELECT title FROM movie ORDER BY SUBSTRING_INDEX(title, '#', -1)+0; +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | "The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | "The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | "The Bionic Woman" (1976) {Rancho Outcast (#3.21)} | +-----------------------------------------------------------+
×
×
  • 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.