PDA

View Full Version : class making page blank


sabret00the
06-23-2005, 10:41 AM
this is my first ever class so i'm not sure why it's going wrong or where but hey it's the only way i'll learn right?

could someone tell me where i'm going wrong, it's based off of Boofo's stats serialize hack

<?php

class DoStats
{

var $prs_stats = array();
var $updatetime;
// var TIMENOW;

function DoStats($prs_stats, $updatetime)
{
$this->updatetime = $updatetime;
//$this->prs_stats = $prs_stats;

if (!$this->prs_stats['lastupdate'])
{
$this->prs_stats['lastupdate'] = 0;
}

if ($prs_stats['lastupdate'] == 0 OR ($prs_stats['lastupdate'] + ($this->updatetime * 60)) < TIMENOW)
{
$starttime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

// Total Pieces
$stats_totalpieces = @$DB_site->query_first("
SELECT COUNT(*) AS count
FROM prs_pieces
");

// Total Reviews
$stats_totalreviews = @$DB_site->query_first("
SELECT COUNT(*) AS count
FROM prs_reviews
");

// Average Rating
$stats_avgrate = @$DB_site->query_first("
SELECT AVG(rating) AS avg
FROM prs_rating
");

// Top Rated Piece
$stats_toprated = @$DB_site->query_first("
SELECT COUNT(prs_ratings.rating) AS count, *
FROM prs_ratings
LEFT JOIN prs_pieces ON (prs_pieces.pieceid = prs_rating.pieceid)
ORDER BY count
DESC LIMIT 1
");

// might need to change this into a quick query of select from piece 'review_count' order by review count
// Top Reviewed
$stats_topreviewed = @$DB_site->query_first("
SELECT COUNT(prs_reviews.pagetext) AS count, *
FROM prs_reviews
LEFT JOIN prs_pieces ON (prs_pieces.pieceid = prs_reviews.pieceid)
ORDER BY count
DESC LIMIT 1
");

// Top Viewed
$stats_topviewed = @$DB_site->query_first("
SELECT *
FROM prs_views
ORDER BY views
DESC LIMIT 1
");

// Top Poster
$stats_topposter = @$DB_site->query_first("
SELECT *
FROM prs_user
ORDER BY pieces DESC
LIMIT 1
");

// Top Reviewer
$stats_topreviewer = @$DB_site->query_first("
SELECT *
FROM prs_user
ORDER BY reviews DESC
LIMIT 1
");

$prs_stats['totalpieces'] = intval($stats_totalpieces[count]);
$prs_stats['totalreviews'] = intval($stats_totalreviews[count]);
$prs_stats['avgrate'] = intval(ceil($stats_avgrate[avg]));
$prs_stats['toprated'] = $stats_toprated[title];
$prs_stats['toprated_pieceid'] = intval($stats_toprated[pieceid]);
$prs_stats['topreviewed'] = $stats_topreviewed[title];
$prs_stats['topreviewed_pieceid'] = intval($stats_topreviewed[pieceid]);
$prs_stats['topviewed'] = $stats_topviewed[title];
$prs_stats['topviewed_pieceid'] = intval($stats_topviewed[pieceid]);
$prs_stats['topposter'] = $stats_topposter[username];
$prs_stats['topposterid'] = intval($stats_topposter[userid]);
$prs_stats['topreviewer'] = $stats_topreviewer[username];
$prs_stats['topreviewerid'] = intval($stats_topreviewer[userid]);

$prs_stats['lastupdate'] = intval(TIMENOW);

$DB_site->query ("
REPLACE INTO " . TABLE_PREFIX . "datastore(title, data)
VALUES ('prs_stats', '" . addslashes(serialize($prs_stats)) . "')
");

$this->DoStats = $prs_stats;
// should return the array prs_stats
}
else
{
$this->DoStats = $prs_stats;
//return array prs_stats;
}
return $this->DoStats;
}

function StatsFormat($prs_stats)
{
foreach($prs_stats AS $key => $value)
{
if (is_numeric($value))
{
$this->GetStats[] = vb_number_format($value);
}
else
{
$this->GetStats[] = $value
}
}
return $this->GetStats;
}
}

?>

Guest190829
06-23-2005, 05:46 PM
Dont you have to make an new instance of the class? I can be completly wrong...

sabret00the
06-23-2005, 05:54 PM
i don't think so, i.e. it's called in my page via $makestats = DoStats($prs_stats, 15);
$prs_stats = $makestats->StatsFormat($make_stats);

calorie
06-23-2005, 06:24 PM
function DoStats($prs_stats, $updatetime)
{
global $DB_site;
$this->updatetime = $updatetime;
...

sabret00the
06-23-2005, 06:41 PM
calorie, i am so so so so so grateful to you, you dont' know how much, this has been bothering me since yesterday, thank you very much for helping my first class come to life, unfortunately i never finished my database schema in order to test it properly, but it is throwing up the correct errors, thank you again :)

calorie
06-23-2005, 06:46 PM
Blush, no problem, glad I could help. :)

sabret00the
06-23-2005, 07:55 PM
Ok now i've finished the database scheme (i guess this is what you get from coding from the front-end)

i'm getting this when i do a print_r on the $makestats
$makestats = new DoStats($prs_stats, 15);
$prs_stats = $makestats->StatsFormat($makestats);

print_r($makestats);


it's giving me this DoStats Object ( [DoStats] => Array ( [totalpieces] => 1 [totalreviews] => 0 [avgrate] => 0 [toprated] => [toprated_pieceid] => 0 [topreviewed] => [topreviewed_pieceid] => 0 [topviewed] => [topviewed_pieceid] => 0 [topposter] => [topposterid] => 0 [topreviewer] => [topreviewerid] => 0 [lastupdate] => 1119559839 ) [GetStats] => Array ( [0] => Array ( [totalpieces] => 1 [totalreviews] => 0 [avgrate] => 0 [toprated] => [toprated_pieceid] => 0 [topreviewed] => [topreviewed_pieceid] => 0 [topviewed] => [topviewed_pieceid] => 0 [topposter] => [topposterid] => 0 [topreviewer] => [topreviewerid] => 0 [lastupdate] => 1119559839 ) [1] => Array ( [0] => Array ( [totalpieces] => 1 [totalreviews] => 0 [avgrate] => 0 [toprated] => [toprated_pieceid] => 0 [topreviewed] => [topreviewed_pieceid] => 0 [topviewed] => [topviewed_pieceid] => 0 [topposter] => [topposterid] => 0 [topreviewer] => [topreviewerid] => 0 [lastupdate] => 1119559839 ) ) ) )

it's got 4 arrays in the makestats instead of just overwriting it and giving me the last one, how would i remedy this?

the bit in question (making the $makestats->GetStats[$var]) is function StatsFormat($prs_stats)
{
foreach($prs_stats AS $key => $value)
{
if (is_numeric($value))
{
$this->GetStats[] = vb_number_format($value);
}
else
{
$this->GetStats[] = $value;
}
}
return $this->GetStats;
}

calorie
06-23-2005, 10:05 PM
See the attached...

sabret00the
06-23-2005, 10:33 PM
thanks though i managed to fix it by changing
return $this->GetStats;
into
return $this->GetStats['0']['1'];

but i reckon yours is the cleaner code.

though i have one more problem, sorry about this.

debugging the code i have
$prs_stats1 = unserialize($datastore['prs_stats']);
echo $prs_stats1 . "<--";
print_r($prs_stats1);
$makestats = new DoStats($prs_stats1, 15);
$prs_stats2 = $makestats->StatsFormat($makestats);

the problem is though, that $prs_stats1 is returning nothing, while $datastore is giving the correct array, it's like $prs_stats1 isn't unseralizing.

the reason $prs_stats1 is go relevant is because of the conditional in the first method of the class that saves me 8 queries

calorie
06-23-2005, 11:22 PM
Do a print_r on $datastore and see if there is a value for the prs_stats key.

sabret00the
06-23-2005, 11:29 PM
ahhh, you're right, it's giving me Array ( [cron] => 1119569760 [utt_store_act] =>...)for some reason it stops after that.

calorie
06-24-2005, 12:53 AM
It looks like you tried to print $prs_stats1 before the initial prs_stats related row had been inserted. Presumably after the next pass, $datastore['prs_stats'] should be available.

sabret00the
06-24-2005, 03:14 PM
thanks again calorie :)