homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
foreach
Mr_Cat




msg:4413582
 5:08 pm on Feb 2, 2012 (gmt 0)

Hi folks,

I've been looking up foreach in the manual but I can't quite understand why the below doesn't work?

$whosonline = "SELECT log_activecheck FROM members_log WHERE log_isonline = 'y'"; // there are 3 members set to be online

$result = mysql_query($whosonline);
$row = mysql_fetch_array($result);

$numrows = mysql_num_rows($result);
echo $numrows . "<br>"; // has it got the right ammount? Yes, 3

foreach ($row as $activecheck) {

echo $activecheck . "<br>"; }

What am I failing to grasp here? That's just how it looks in the manual except they predfine the array with something like $row = array(1, 2, 3) instead of my 'fetch_array'.

The results I get are '3' from the row count which is right, then for the activecheck which displays a timestamp it displays two lines both containing the timestamp of the first db entry to meet the selection check of 'y'

 

rocknbil




msg:4413585
 5:22 pm on Feb 2, 2012 (gmt 0)

$numrows = mysql_num_rows($result);

What am I failing to grasp here?


You create an "object" and store that instance of the object in the variable $result.

The function mysql_num_rows() returns a scalar. A scalar is a flat single value, whether it be a string, integer, float, whatever, it is not a list like an array.

foreach ($row as $activecheck) {

So your foreach goes through the loop ONCE because there's only one value in $row (at this point,) 3. If you were to do this,

for ($i=1;$i<=$row;$i++) {
echo "$i \n";
}

It would indeed echo three times, but . . .$row is still 3. :-) To extract values from $result, you need to use one of the fetch methods.

Before you start, don't use the $rows as a counter to step through. It's not needed. Use a while on result.

while ($row=mysql_fetch_array($result)) {
// NOW $row is an array - but it's an array of the FIELDS you selected
$len = count($rows);
echo "<p>";
for ($i=0;$i<$len;$i++) { // Remembering arrays start at index 0 (zero)
echo " i: $i " . $row[$i] . " ";
}
echo "<p>\n";
}

Mr_Cat




msg:4413628
 7:40 pm on Feb 2, 2012 (gmt 0)

Wow, cool, thanks! :)

My actual end objective is to write a little possible cron job scrip to run every 20 minutes and check site members last activity time against '20 minutes ago' and mark them as offline where appropriate. In the aid of creating a little who's online feature.

I've put together this but all it does is set everyone to 'n' or offline as soon as the first runs out of time, despite others still being within the 20 mins.

$whosonline = "SELECT log_activecheck FROM members_log WHERE log_isonline = 'y'";
$result = mysql_query($whosonline);

$twmincheck = strtotime ( " -20 minutes " ); // the 20 mins ago maths in english, nice

if ($row = mysql_fetch_array($result)) {

do {
$activecheck1 = $row['log_activecheck']; // sql timestamp
$activecheck = strtotime($activecheck1); // php safe now

if ($twmincheck > $activecheck) {
$nowoffline = "UPDATE members_log";
$nowoffline.= " SET log_isonline = 'n'"; // outside 20 mins they're now offline
mysql_query($nowoffline);
} // end if

} // end do

while ($row = mysql_fetch_array($result));

}; //end if



I've been trying to bash this together in php but am not sure if there's a method it could just be done in some kind of query. I read that sql can do some handy things with dates if they're stored as datetime as opposed to php's crazy date numbers which you can store as an int, but what those handy things were wasn't really made clear as it was on the topic of converting betwen the two with strtotime() so I could do my 20 minute check easily.

Mr_Cat




msg:4413850
 11:27 am on Feb 3, 2012 (gmt 0)

Talk about a mammoth waste of time! There's always a sleek and simple answer somewhere isn't there, and sods law it's usually a one liner :D
There's many more handy things to sql than just insert, select, update and equals! I was expecting some complcated IS BETWEEN date style thing but noooo

$nowoffline = "UPDATE members_log SET log_isonline = 'n' WHERE log_activecheck < '" . $twminsago . "' AND log_isonline = 'y'";


magic :D

Possibly a mite off php topic but I wanted to post my answer...

rocknbil




msg:4413967
 5:57 pm on Feb 3, 2012 (gmt 0)

Nothing in which you learn something new is a waste. Here's another one. :-)

$twminsago

I'm sure you arrived at this via the PHP date/time functions. What happens if your mysql server's time differs from your PHP server's time? It happens.

Besides that, there is already date/time function directly in mysql [dev.mysql.com]. Have a look at this instead.

$nowoffline = "UPDATE members_log SET log_isonline = 'n' WHERE log_activecheck < date_sub(now(), interval 2 minute) AND log_isonline = 'y'";

This of course requires the field is a valid datetime format, either by timestamp or YYYY-MM-DD HH:MM:SS.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved