Jump to content

bit of help with arrays please


richardjh

Recommended Posts

arrays confuse the hell out of me so i need some help please...

 

I have a very simple hits counter which stores each hit in sql using the following details:

 

table name = daily_hits

story ID (sid)

IP (users ip address)

date (e.g 24/10/08)

 

Now as you can imagine this database table will get awfully long very quick so I want to create a script which will be called by cron. This script will query the database table (daily_hits) and count each row from each of the sid's in the table. So for instance it might query and find that there are 10 seperate sid's which the script has to count up many rows EACH sid has to arrive at something that could be echoed as e.g.

sid 12 = 13 rows (hits)

sid 21 = 61 rows

sid 26 = 14 rows

sid 33 =121 rows... etc

 

then this info can be inserted into a table called weekly_hits at the end of each week (using cron)

 

Now I know that arrays are the thing needed to archive this but I could do with someone telling me where to start please.

 

any help would be very welcome.

 

thanks

R

Link to comment
Share on other sites

If i understand you correctly you don't need arrays at all ... you could simply

write the script to insert the info 'weekly_hits' as the query executes ie:

$query = mysql_query(your count query);
while($hits = mysql_fetch_row($query)){
mysql_query(your insert query)
}

 

but since you asked about arrays one way to do it would be (but this is a serious memory hog)

is

 

arrayVar[sid] = hits;

 

ie.

arrayVar[12] = 13;

arrayVar[21] = 61;

 

but there this creates a large essentially empty array unless your sid's are closer in frequency IE sid 1 sid 2 sid 3

Link to comment
Share on other sites

 

arrayVar[sid] = hits;

 

ie.

arrayVar[12] = 13;

arrayVar[21] = 61;

 

but there this creates a large essentially empty array unless your sid's are closer in frequency IE sid 1 sid 2 sid 3

 

Not so.

 

<?php
$arrayVar = array();

$arrayVar[1] = 13;
$arrayVar[999] = 61;

echo '<pre>';
var_dump ($arrayVar);
echo '</pre>'  
?>

-->
array(2) {
  [1]=>  int(13)
  [999]=> int(61)
}

 

ie 2 array elements, not 999

Link to comment
Share on other sites

I think arrays are a bit out of my scope guys. However, I've hit upon this idea that I thought might work. This bit of code should enter the hit into daily_hits (if the IP is unique). At the same time it should look into the master_hits table for a row matching the sid - if it can't find one it creates it with a starting count of 1 (so far so good). If it does find a matching sid row it should increment the counter for that row (not so good as it don't increment with the code I cobbled together below :(

 

$date = date( "j/m/Y" );
$ip = $_SERVER['REMOTE_ADDR'];

$c_query = "SELECT * FROM daily_hits WHERE sid='$val' AND ip='$ip'";
$counter = mysql_query($c_query) or die(mysql_error());
if(mysql_num_rows($counter) < 1) {
mysql_query("INSERT INTO daily_hits VALUES ('$val','$date','$ip', '')");

$master_query = "SELECT * FROM master_hits WHERE sid='$val'";
$m_counter = mysql_query($master_query) or die(mysql_error());
if(mysql_num_rows($m_counter) < 1) {
mysql_query("INSERT INTO master_hits VALUES ('$val', '1')");
} else {
$new_count = $m_counter['counter'] + 1;
mysql_query( "UPDATE master_hits SET counter='$new_count' WHERE sid='$val'" );
}

}

 

Could ask you fellows why the $new_count doesn't want to increment please?

 

If I (with your help) can make this increment then in theory I can periodically dump the daily_hits table before if gets huge and still keep a hits count tally for each 'sid' (story)

 

thanks for your help so far

R

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.