Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Age calculating is much simpler with DateTime class. function age($dob) { $d1 = new DateTime($dob); return $d1->diff(new DateTime())->y; } echo age('1980-09-16') ; // 34
  2. Now if the car were a mini-bus with 10 seats and you wanted to know how many different ways you could seat the ten passengers, then I'd agree with you. But that's another problem.
  3. @grissom, my apologies for the image.
  4. See reply #6 ^ by requinix
  5. (Each of the 10 cars can have each of the 10 drivers. 10 x 10 = 100)
  6. If you have 10 cars and 10 drivers there are 100 different combinations, not 10. Table : car (car_id | carname) Table : driver (driver_id | drivername) To get all combinations SELECT carname, drivername FROM car CROSS JOIN driver
  7. 2/10 for formatting Order by "landlord" and, when processing the results, output the landlord when it changes to a new name
  8. That error is a sign that your query failed and so $result contains"false" instead of a valid result set. outputting mysql_error() after calling the query will tell you why. It may be because you are using $_SESSION data without calling session_start() at the top of the script and so the connection fails.
  9. That's a fairly small db. Just because table is on the large side mysql> select count(*) from result; +----------+ | count(*) | +----------+ | 2557024 | +----------+ doesn't mean slow queries mysql> select count(*), avg(analysisresult) as average -> from result -> where usercode = 1; +----------+------------+ | count(*) | average | +----------+------------+ | 25570 | 12.9759093 | +----------+------------+ 1 row in set (0.16 sec)
  10. When user 1 completes item 2 you write a record for user 1 to the item_completed table. When user 2 completes it you do the same for user 2 so you get +---------+---------+---------------------+ | item_id | user_id | completed | +---------+---------+---------------------+ | 2 | 1 | 2015-05-14 10:59:15 | | 2 | 2 | 2015-05-15 21:59:15 | +---------+---------+---------------------+
  11. It doesn't have to be there at all since you would only write to that table when a user flags a task as completed. I thought the date would be useful should you ever need to know, say, who completed in the fastest/slowest time period or who has been doing it the longest as still hasn't finished (they may need help).
  12. "\" is the escape character so you may need "\\" in the server name (or try "/" )
  13. while() loops will loop until the condition is no longer true. In the case of mysqli results, mysqli_fetch_* functions all return false when they reach the end of the result set. foreach() is for loop through arrays. $key in your code is an array of the columns in the current row. You need $key[0], $key[1] etc to echo the individual fields. Or you can use field names as the key (but I don't know what they are as you use select * ). When you come back to the code in a few months you won't know what it's selecting either. Specify just the columns you want and not * .
  14. That is exactly what my code is doing, showing all the items and which ones you ($user) have already ticked off. If you run it for user 2 (who has completed 3 and 4) you get
  15. The processing you want to do may involve sorting by dates. The date format you are using cannot be sorted, Always use date format of YYYY-MM-DD in data. You can always reformat it for presentation. As I have demonstrated already, some array structures are more useful than others for certain tasks. Another advantage of external data is that you can filter the data when loading and load into a suitable array structure for the task.
  16. For a start, unless your email address is "deleted_for_public_posting_purposes" then it's going nowhere. I suggest you look up mail in the manual paying attention to the required arguments and structure of headers.
  17. How many more time times do you intend to post this same question (in contravention of forum rules)? You have answers here: http://forums.phpfreaks.com/topic/296354-need-some-help-please/
  18. Instead of changing your program every week it would be much better to put your data into a database table each week CREATE TABLE club ( `club_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `clubname` varchar(50) ); CREATE TABLE rider ( `rider_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `club_id int, `ridername` varchar(50), KEY `idx_club` (`club_id`), ); CREATE TABLE `race_time` ( `race_time_id` int(11) NOT NULL AUTO_INCREMENT, `race_date` date DEFAULT NULL, `rider_id` int DEFAULT NULL, `club_id` int DEFAULT NULL, `time` time DEFAULT NULL, PRIMARY KEY (`race_time_id`), KEY `idx_club` (`club_id`), KEY `idx_rider` (`rider_id`) ); You can then either use SQL queries to calculate postions, points etc, or load your arrays from the database and calculate If you don't like the idea of a database then at least store your results in a text file, appending new results each week and load your arrays from there. Or use a spreadsheet and export a csv each week. Whichever way you go, you shouldn't be continually changing the program code.
  19. Alternative to regex $a = '(141442872453(2)) NV4852-SD (2)'; $b = str_replace(['(',')'], ' ', $a); $data = sscanf($b, ' %s %d %s %d'); echo '<pre>',print_r($data, true),'</pre>'; giving Array ( [0] => 141442872453 [1] => 2 [2] => NV4852-SD [3] => 2 )
  20. Where are you getting the results from?
  21. Use file() $allowed_users = file('user.txt', FILE_IGNORE_NEW_LINES);
  22. Let's suppose you item and item completed tables contain mysql> SELECT * FROM item; +---------+--------------+ | item_id | item_descrip | +---------+--------------+ | 1 | Item 1 | | 2 | Item 2 | | 3 | Item 3 | | 4 | Item 4 | | 5 | Item 5 | | 6 | Item 6 | | 7 | Item 7 | | 8 | Item 8 | | 9 | Item 9 | | 10 | Item 10 | +---------+--------------+ mysql> SELECT * FROM item_completed; +---------+---------+---------------------+ | item_id | user_id | completed | +---------+---------+---------------------+ | 2 | 1 | 2015-05-14 10:59:15 | | 3 | 2 | 2015-05-15 21:59:15 | | 4 | 2 | 2015-05-16 11:59:15 | | 5 | 1 | 2015-05-17 14:59:15 | +---------+---------+---------------------+ User 1 has completed tasks 2 and 5. If we run this query which matches the tasks against the completed tasks for user 1 SELECT i.item_id , i.item_descrip , c.user_id FROM item i LEFT JOIN item_completed c ON i.item_id = c.item_id AND c.user_id = 1 ORDER BY i.item_id we get this, where the completed tasks are shown by a non-null value in the user_id column +---------+--------------+---------+ | item_id | item_descrip | user_id | +---------+--------------+---------+ | 1 | Item 1 | NULL | | 2 | Item 2 | 1 | <-- completed | 3 | Item 3 | NULL | | 4 | Item 4 | NULL | | 5 | Item 5 | 1 | <-- completed | 6 | Item 6 | NULL | | 7 | Item 7 | NULL | | 8 | Item 8 | NULL | | 9 | Item 9 | NULL | | 10 | Item 10 | NULL | +---------+--------------+---------+ With this code you will get the output shown below <?php function showTasks(mysqli $db, $user) { $sql = "SELECT i.item_id , i.item_descrip , c.user_id FROM item i LEFT JOIN item_completed c ON i.item_id = c.item_id AND c.user_id = ? ORDER BY i.item_id"; $out = "<table>"; $stmt = $db->prepare($sql); $stmt->bind_param('i', $user); $stmt->execute(); $stmt->bind_result($id, $desc, $uid); while ($stmt->fetch()) { $cbox = $uid ? "&check;" : "<input type='checkbox' name='complete' value='$id' />"; $out .= "<tr><td>$desc</td><td>$cbox</td></tr>\n"; } $out .= "</table>\n"; return $out; } ?> <html> <body> <?=showTasks($db, $_SESSION['user'])?> </body> </html>
  23. In which case I concur with Ch0cU3r.
  24. Why does your array look like $array = [ "riderName" => "Example Rider", "round1" => "20", "round2" => "17", "round3" => "18", "round4" => "19", "round5" => "16", "round6" => "20", "round7" => "18", "round8" => "10", "round9" => "17", "round10" => "0", "round11" => "0", "round22" => "18" ]; When it would make more sense to be like $array = [ "Example Rider" => [ "20", "17", "18", "19", "16", "20", "18", "10", "17", "0", "0", "18" ] ]; Then all that is required is foreach ($array as $rider => $rounds) { $totalRides = count(array_filter($rounds)); // count non-zero values rsort($rounds); $totalHigh8 = array_sum(array_slice($rounds,0,); // get sum of highest 8 echo "Rider: $rider<br>Total rides: $totalRides<br>Highest 8: $totalHigh8<br><br>"; } giving Rider: Example Rider Total rides: 10 Highest 8: 147
×
×
  • 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.