ericp Posted April 14, 2013 Share Posted April 14, 2013 Hi all, The chapter 18, pursue #7 says: 'If you’ve added the last_login field, use it to print a message on the home page as to how many users have logged in in the past, say, hour or day." For my situation, I have finsihed the first 6 number successfully so far. Then, I really do not understand this pursue very clearly. 1/ Does it want us to code how to list/ show the number of users having registered so far? 2/ Does it want us to code to how to show the registered users who are now going live on the website? 3/ Does it ask us to code to how to show the number of users who has logged in at leat once since the date they registered? Can you help me? Thanks Eric Link to comment Share on other sites More sharing options...
HartleySan Posted April 14, 2013 Share Posted April 14, 2013 It means the following: Count how many users have logged in in the past hour (or whatever unit of time you want to use). For example, let's say that the following users last logged in at the following times: User 1: 12:04 AM User 2: 4:52 AM User 3: 8:15 AM User 4: 8:19 AM User 5: 8:52 AM And then let's say that you log in at exactly 9 AM. In that case, if you're counting how many users logged in in the past hour, because users 3-5 all logged in after 8 AM (one hour before 9 AM), then "3" should be reported back by the DB query. Link to comment Share on other sites More sharing options...
ericp Posted April 15, 2013 Author Share Posted April 15, 2013 Hi all, This is my code: <?php //Count the logged in users in the last 600 minutes:require ('includes/config.inc.php');$page_title = 'Count Logged in users in the last 60 minutes';include ('includes/header.html'); require (MYSQL);// Define the query:$q="SELECT COUNT(*) FROM users WHERE last_login > DATE_SUB(NOW(), INTERVAL 600 MINUTE)";$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));$row = @mysqli_fetch_array ($r, MYSQLI_NUM);// Count the number of returned rows:$num = mysqli_num_rows($r);if ($num > 0) { // If it ran OK, display the records.echo "<p>There are <strong> $num </strong> active users in the last 600 minutes: </p>\n";// Table header: echo '<table align="center" cellspacing="3" cellpadding="3" width="75%"> <tr> <td align="left"><b>User\'s First Name</b></td> <td align="left"><b>Last Log-In</b></td> </tr>';// Fetch and print all the records: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<tr> <td align="left">' . $row['first_name'] . '</td> <td align="left">' . $row['last_login'] . '</td> </tr> '; } echo '</table>';mysqli_free_result($r);} else { // If no records were returned. echo '<p class="error">There are no active users in the last 600 minutes.</p>';}mysqli_close($dbc);include ('includes/footer.html');exit(); // quit the script?> and this is the output: http://hiteachers.com/count_logged_in_users.php Which returns zero logged in users though it counted 1 (one) active users from the last_login row in users table. Can you help me with this? Thanks Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 When you use an aggregate function like COUNT, you will always get one row back from the DB, regardless of the actual count (be it 0, 21, or 54,209). As such, $num will always be 1. As for $row['first_name'] and $row['last_login'], I'm not 100% sure, but I don't think anything will be output because you didn't ask for those columns in your query (and actually, I'm surprised an error doesn't occur when you tried to output them). 1 Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 That's exactly the case: $row['first_name'] does not exist because the query only selected COUNT(*). Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 That's what I was thinking, but what I can't understand is why an error isn't output on the page that ericp linked to. Anyway, thanks for confirming that, Larry. Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 I would assume that display_errors is either disabled or error reporting is set to a lower level. Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 Makes sense. Thanks for the quick response as always. Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 Thank YOU for all the help you provide in these forums. By the way, I have ebooks of my "Modern JavaScript" and my "PHP Advanced" to giveaway. Do you have both already? Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 You already gave me a free digital copy of PHP Advanced and I have a hard copy of your Modern JS book. Thanks anyway though. Perhaps your next book will pique my interest though. By the way, what's the plan after the Yii book is done (or I should ask, is the Yii book already done)? Link to comment Share on other sites More sharing options...
ericp Posted April 15, 2013 Author Share Posted April 15, 2013 Oop... I haven't got them yet. BTW, have you got any code for this to share? Loving is caring, right? Ericp Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 Do you mean for your original question? How about: <?php $q = "SELECT COUNT(*) AS num_log_in FROM users WHERE last_login > DATE_SUB(NOW(), INTERVAL 600 MINUTE);"; $r = mysqli_query($dbc, $q); $row = mysqli_fetch_array($r, MYSQLI_ASSOC); echo 'Number of users logged in in the last 10 hours: ' . $row['num_log_in']; Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 Or since you want to both show a count and the results, you could just do SELECT first_name, last_name ..., then do the mysqli_num_rows() to get your count, then fetch and display the results. Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 You already gave me a free digital copy of PHP Advanced and I have a hard copy of your Modern JS book. Thanks anyway though. Perhaps your next book will pique my interest though. By the way, what's the plan after the Yii book is done (or I should ask, is the Yii book already done)? Yii book is not done yet. Taking a while. I'll finish the first edition hopefully next month. Then I have to pursue translations, a print edition, and an update for Yii 2. The order of those will depend upon Yii 2. This is not public knowledge yet, but I should be doing the second edition of my "Effortless E-commerce" book in late summer, early fall. Other than that, I have no idea what my next new title or my next self-published book would be. Either would be 2014, which sounds crazy to talk about in April 2013... Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 Well, I guess it's public knowledge now. I imagine a lot of people will be excited about that, as your e-commerce book seems to be the second most popular behind your PHP/MySQL book (but I could be wrong). Just a thought, but a book about all the new HTML5 stuff could be really cool. It's perhaps not your forte, and a lot of it is very graphics-centric, but could be exciting. Just a few things you could cover: - CSS3 (animations, transforms, shadows, etc., etc.) - 2D canvas and 3D (WebGL) - WebSocket (one of my favorites) - WebRTC - Local storage - History API (for Ajax with URL swapping) - Mobile and responsive design - Worker API (i.e., multithreading) There're a lot more too, but those are the things that crossed my mind. Link to comment Share on other sites More sharing options...
Larry Posted April 15, 2013 Share Posted April 15, 2013 I'm glad to be doing a second edition of the e-commerce book, as there were lots of things I wanted to add to the 1st edition but didn't get the space for. Roughly speaking, the "PHP & MySQL" and "Modern JavaScript" books are the top 2 sellers. The "PHP for the Web", "PHP Advanced", and the e-commerce book round out the top 5, in various orders. Thanks for the suggestion about a more advanced HTML5+JavaScript book. I appreciate it. The problem for me is I don't do much with the graphical components, which I think would have to be covered as well to make it practical. I like the thought, though. Link to comment Share on other sites More sharing options...
HartleySan Posted April 15, 2013 Share Posted April 15, 2013 I didn't realize your JS book was so popular. Interesting to know though. Thanks. As you have stated many times in your books, I know you're not a graphics guy and it's not your specialty, but on the other hand, gauging by the questions I see on your forums a lot (especially on the JS board), a lot of people want to be able to rely on your books for anything and everything web development related, and people what to know about CSS3 and the new JS APIs, etc. As such, I think it's worth considering. And you know, you could always consult outside experts on the stuff you're not too familiar with/not as confident with. Some sort of collaborative book where you're the main author could be interesting, no? I only mention that because so many great web development websites these days are built on the backs of not one, but many individuals (e.g., Stack Overflow, HTML5 Rocks, etc.). Link to comment Share on other sites More sharing options...
Larry Posted April 16, 2013 Share Posted April 16, 2013 Sure thing. The fact of the matter is that as popular as PHP is, JavaScript is more popular. That's a book I wanted to write for years, so I'm glad it's being well received. I definitely know what you mean about a book on HTML5+. And I have no problems co-authoring (I did on my C and C++ books). I'll keep it in mind. Thanks again for the suggestion. Link to comment Share on other sites More sharing options...
Recommended Posts