chasethemetal Posted May 24, 2011 Share Posted May 24, 2011 My code as below will echo all the entries from the current day. Now instead of echoing a list of dates, I need to echo a number that counts these results. Any help again would be appreciated I am slowly become not a n00b.<?php $data = mysql_query("SELECT * FROM dates ORDER BY id DESC") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { $date = $info['date']; $today = date('m/d/y'); if ($date == $today) echo ''.$date.'<br />'; else echo''; } ?> Link to comment Share on other sites More sharing options...
jeffman Posted May 24, 2011 Share Posted May 24, 2011 echo mysql_num_rows($data);?? Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 Well that would work if I wanted to count allll of the rows. But the idea is to just count the number of rows that have a date that matches "todays" date. Link to comment Share on other sites More sharing options...
ShadowMage Posted May 24, 2011 Share Posted May 24, 2011 Use a counter to keep track: $count = 0;.....while (...) { ... if ($date == $today) { echo $date.'<br />'; $count++; }}echo 'Total records: '.$count; Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 A simple counter might be the easiest solution <?php$data = mysql_query("SELECT * FROM dates ORDER BY id DESC") or die(mysql_error());$counter = 0;while($info = mysql_fetch_array( $data )) { $date = $info['date']; $today = date('m/d/y'); if ($date == $today){ echo $date . '<br />'; $counter++; echo "dates matching todays date => $counter <br>"; }; }; Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 This is great. We are getting close. This counts everything so I end up getting a list with 1, 2, 3 ect... How do I get it to echo just the last number and thats it. Link to comment Share on other sites More sharing options...
ShadowMage Posted May 24, 2011 Share Posted May 24, 2011 This is great. We are getting close. This counts everything so I end up getting a list with 1, 2, 3 ect... How do I get it to echo just the last number and thats it.Try my solution. (In other words, just move the statement that echos the count outside of the loop) Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 ShadowMage you are a true mage. Thank you again. hi Five. Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 There is one last thing I can't figure out... Is there a simple way to manipulate the above solution for getting a count of fields by day, and change it so that you can get a count of fields by week?For everyone's info I am just creating a little stat thing for my site. Basically so I can see a count of visit's by day, by week, by month and so forth. I know freebee stat counters exist and are great like Google Analytics, but something feels better about having my own little half arse version running too. Link to comment Share on other sites More sharing options...
ShadowMage Posted May 24, 2011 Share Posted May 24, 2011 Well, you'd have to start by first determining the date range of the week (ie, this week would be 5/22 - 5/28)You can do that by using PHP's date() function and formatting the date using the 'w' code to find the current day of the week and add or subtract the appropriate number of days to the current date to find the start and end of the week. For example, to find this weeks range, you start by formatting the current date: $currDay = date('w'); That gives you 2. (0 is Sunday, 6 is Saturday)So, you need to subtract 2 ($currDay) days from the current date to find Sunday's date, and add 4 (6-$currDay) days to the current date to get Saturday's date. (Try Googling this if you don't know how to do it, it's quite a common task and the answer should be easy to find)Once you have the range, you just check if your date falls between the two: if (($date > $weekStart) && ($date < $weekEnd)) { $weekCount++;} Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 I'm blowing it.I'm sure this code is wrong because it's rendering me a "0"I am trying to count all fields that the dates fall in-between the week. So I don't want to count the weeks. More so count all the fields in that week.Aka I've had this many visits this week echo "that count number"heres my n00b code.<?php$data = mysql_query("SELECT * FROM location ORDER BY id DESC") or die(mysql_error());$count = 0;while($info = mysql_fetch_array( $data )) { $date = $info['date']; $currDay = date('w'); $weekStart = strtotime('-2',$currDay);$weekEnd = strtotime('+4',$currDay); if (($date > $weekStart) && ($date < $weekEnd)) { $count++; }; };echo "$count";?> Link to comment Share on other sites More sharing options...
justsomeguy Posted May 24, 2011 Share Posted May 24, 2011 What is $date? Have you printed it out to see the values you're comparing with $weekStart and $weekEnd? Have you printed $weekStart and $weekEnd, for that matter? Link to comment Share on other sites More sharing options...
chasethemetal Posted May 24, 2011 Author Share Posted May 24, 2011 date = MM/DD/YYYY in that format. Every time someone visits my site, their IP is logged and a date is attached in that format.When I echoed weekStart and weekEnd I got strange 4 # length numbers like 7564.I know I'm missing something big. And I just can't seem to find a solution on the web. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 24, 2011 Share Posted May 24, 2011 Go through your code line-by-line and make sure you understand what each line does. Look up the functions you're using in the manual if you don't know what they do. It's important to understand what you're trying to do, what you're using, what it does, and why it does it. Searching for code examples online isn't the way to do that, you need to do research about the things you're actually telling the computer to do, and then figure out if that's what you really want it to do.http://www.php.net/manual/en/function.date.phphttp://www.php.net/manual/en/function.strtotime.php Link to comment Share on other sites More sharing options...
Ingolme Posted May 25, 2011 Share Posted May 25, 2011 Edit: I posted too quickly. Looking through the topic I see there's a new problem which seems to be dealt with already.____I think a proper SQL query might be able to retrieve the information you want. $data = mysql_query('SELECT COUNT(*) AS total FROM dates HAVING date = NOW()');if($info = mysql_fetch_array( $data )) { echo $info['total'];} Link to comment Share on other sites More sharing options...
chasethemetal Posted May 25, 2011 Author Share Posted May 25, 2011 Thanks Ingolme but unfortunately that doesn't seem to work.And justsomeguy. I understand how every line of code works. Except the ability to define the range, and then the method of counting all fields in that range.Going through manuels never really illustrates how to use the functions unless you have some fundamental schooling background in php. which i dont. I'm an amateur seeking advice through working examples so I can get up on my feet in PHP to one day be able to help individuals like myself. But however thanks for pointing out those references.To the world of w3schools. I am still stumped. Any examples of a simple count # of fields by week would be awesome! Link to comment Share on other sites More sharing options...
ShadowMage Posted May 25, 2011 Share Posted May 25, 2011 I suspect these lines are the culprits:$weekStart = strtotime('-2',$currDay);$weekEnd = strtotime('+4',$currDay);The second argument expects a timestamp. You are passing it a preformatted date string (In this case, the number of the day of the week). So today, one of those lines would look like this:$weekStart = strtotime('-2', 3);The timestamp is 3, which (I think) is the number of seconds since the epoch (1970, don't remember month or day). That is obviously not the current date. If you just leave out the second argument, it will default to the current date.Also, you probably want to calculate your interval to add/subtract on the fly, instead of hardcoding it.Try something like this:$weekStart = strtotime('-'.$currDay.' days');$weekEnd = strtotime('+'.(6-$currDay).' days');EDIT:Also, just realized you're trying to compare a string to a timestamp. $date is a string and $weekStart/$weekEnd are timestamps. You should probably convert $date to a timestamp:Change:$date = $info['date'];to:$date = strtotime($info['date']); Link to comment Share on other sites More sharing options...
thescientist Posted May 25, 2011 Share Posted May 25, 2011 Going through manuels never really illustrates how to use the functions unless you have some fundamental schooling background in php. which i dont. I'm an amateur seeking advice through working examples so I can get up on my feet in PHP to one day be able to help individuals like myself. But however thanks for pointing out those references.Well then how would you expect to learn what a function does? What its arguments are and what it returns? If you had read the manual, very clearly in the first few lines it explains what type of data it expects as it' arguments, and as ShadowMage pointed out, you are passing in the wrong type of data to the function; a string, not a timestamp. I understand you are learning, but a lot of other people ask questions about functions where the basic info is right there at the top of the manual page. It's pretty synonymous with most other technical things in this world. Manuals and documentation are there for the benefit of developers/users. They may be dry and boring, but they give you everything you need to know if you just give it a chance. Link to comment Share on other sites More sharing options...
chasethemetal Posted May 25, 2011 Author Share Posted May 25, 2011 Thanks ShadowMage. The scientist, I was never knocking on manuels. I was just illustrating how I personally learn from example rather than from scratch.Well this one Is closed. Got it working. And have manipulated it to work for month and year under the same principal. Thanks everyone for your help. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2011 Share Posted May 25, 2011 And justsomeguy. I understand how every line of code works.I believe you think you understand it, but I'm not sure you actually do. The reason I think that is because you were trying to add days to the output of date('w'). Using the w flag returns a number for the current day, so date('w') returns a number from 0 to 6, and then you're trying to use that with strtotime to add or subtract to that. That will result in a date around 1/1/1970, not the current date. Things like that made me think that you weren't really understanding what you were telling the computer to do, even though you thought you did understand. Most of those functions work with Unix timestamps. The time function gets the current timestamp:$now = time();with the current timestamp, you can add or subtract however many days you want. There are 86400 seconds in a day, so you can easily calculate the timestamps for one week prior and one week later:$previous = $now - (86400 * 7);$next = $now + (86400 * 7); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.