loop trouble

I am trying to fix a page on a blog and having trouble with the php code. Right now it is giving the stats for the blog by month in a table. What I don’t want it to do is cut off the page each year, but that is what it is doing. How do I modify the code to remove the year loop? The code is below. Any help is appreciated. Thanks!

[php]<?php

/**

  • Template Name: Stats

*/

get_header(); ?>

<?php $c = mysql_connect('localhost', 'bookmaster', 'flyingdutchm3n'); // Connect to MySQL mysql_select_db('read'); // Select database $year = preg_replace("/[^0-9]/", "", get_query_var( 'paged' )); if ($year > date("Y")|| $year < '2010') { $year = date("Y"); } $monthval['12'] = 'December'; $monthval['11'] = 'November'; $monthval['10'] = 'October'; $monthval['09'] = 'September'; $monthval['08'] = 'August'; $monthval['07'] = 'July'; $monthval['06'] = 'June'; $monthval['05'] = 'May'; $monthval['04'] = 'April'; $monthval['03'] = 'March'; $monthval['02'] = 'February'; $monthval['01'] = 'January'; $year_total = array(); foreach ($monthval as $key => $value) { if ($year.$key <= date("Ym")) { $x = 1; $total_books = 0; $total_pages = 0; $total_readers = 0; ?>

<?php echo $value; ?><?php echo ($year.$key == date("Ym"))?' ':''; ?> <?php echo ($year = date("Y"));?> Stats

<?php unset($query,$result,$row); $query = "SELECT name.meta_value AS reviewer, SUM(pages.meta_value) AS total_pages, COUNT(pages.meta_key) AS books FROM wp_posts post LEFT JOIN wp_postmeta name ON post.ID = name.post_id LEFT JOIN wp_postmeta pages ON post.ID = pages.post_id LEFT JOIN wp_postmeta date ON post.ID = date.post_id WHERE post.post_status = 'publish' AND post.post_type = 'post' AND name.post_id=post.ID AND name.meta_key='reviewer' AND pages.post_id=post.ID AND pages.meta_key='page_number' AND date.post_id=post.ID AND date.meta_key='date_read' AND date.meta_value LIKE '".$key."/%%/".$year."' GROUP BY name.meta_value ORDER BY reviewer ASC"; $result = mysql_query($query); if ($result) { ?> <?php while ($row = mysql_fetch_assoc($result)) { $total_books = $total_books + $row['books']; $total_pages = $total_pages + $row['total_pages']; $total_readers++; $year_total[$row['reviewer']]['books'] = $year_total[$row['reviewer']]['books'] + $row['books']; $year_total[$row['reviewer']]['pages'] = $year_total[$row['reviewer']]['pages'] + $row['total_pages']; ?>

<tr<?php echo ($x%2 == 0)?' bgcolor="#e2d2c3"':''; ?>>

<?php $x++; } ?>
Reviewer Books   Pages   Avg pages/book
<?php echo ($row['reviewer'])?$row['reviewer']:''; ?> <?php echo $row['books']; ?> <?php echo number_format($row['total_pages']); ?> <?php echo number_format(round($row['total_pages'] / $row['books'])); ?>

Total Books Read: <?php echo number_format($total_books); ?>

Total Pages Read: <?php echo number_format($total_pages); ?>

Total participants: <?php echo $total_readers; ?>

Average pages/book: <?php echo number_format(round(($total_pages / $total_books))); ?>


<?php }} $year_total_books = 0; $year_total_pages = 0; $year_total_readers = 0; foreach ($year_total as $r_name => $r_category) { $year_total_books = $year_total_books + $r_category['books']; $year_total_pages = $year_total_pages + $r_category['pages']; $year_total_readers++; }} ?>
<?php get_sidebar(); ?> <?php get_footer(); ?>[/php]

First I will make a few suggestions. A lot of that doe is not needed. For instance, getting the month is pretty simple much like the date(‘Y’) function.

This:
[php] if ($year > date(“Y”)|| $year < ‘2010’) { $year = date(“Y”); }[/php]

Unless I am reading it incorrectly will never be true. if ( 2016 > 2016 OR 2016 < 2016 ) for instance:

false | false

MySQL_ function have been removed from the current version of PHP due to being insecure. They should not be used.

Now, can you elaborate on what you mean by, “What I don’t want it to do is cut off the page each year”?

To remove the “year loop” just comment it out and see what happens.

Sponsor our Newsletter | Privacy Policy | Terms of Service