Displaying only the top 4 in each categary

Discussion in 'PHP' started by DRoP, Jun 30, 2007.

  1. #1
    Hi there, on my site (link in my sig), all the games show on one page...

    I was wondering if it's possible to have it show just the first X games in each categary, and then perhaps have an ajax? dropdown to show the rest?

    any advice would be appreaciated...

    here is the code

    <?php
    /* Date | GamesPlayed */
    require("db.cnf.php");
    require("templates.tpl.php");
    
    class GamesSystem {
    
    	var $gamedata;
    	var $imgdir;
    	var $swfdir;
    	var $loaded;
    	var $template;
    	var $am;
    
    	function GamesSystem($admin_mode = 0){
    		if($admin_mode != 1){
    			global $templates;
    			$this->template = $templates;
    		}
    		$this->gamedata = array();
    		$this->imgdir = './images';
    		$this->loaded = false;
    		$this->swfdir = './swf';
    		$this->am = $admin_mode;
    	}
    
    	function vQ($info){
    		if($this->am == 1){
    			return $info.' != \'1\'';
    		} else {
    			return '';
    		}
    	}
    
    	function Load(){
    		$getCategories = "SELECT cId,cName FROM categories WHERE cVisible = '1' ".$this->vQ('OR cVisible')." ORDER BY cOrder ASC";
    		if($cats = @mysql_query($getCategories)){
    			while($category = @mysql_fetch_assoc($cats)){
    				$this->gamedata[$category['cId']] = $category;
    				$this->gamedata[$category['cId']]['games'] = array();
    			}
    			$this->gamedata[0] = array('cId' => 0, 'cName' => 'Other Games', 'games' => array());
    			$getGameData = "SELECT g.gId, g.gDescription, g.gSwfFile, g.gVisible, g.gInCategory, g.gThumb, g.gName, p.Played FROM
    			games as g, playstats as p WHERE g.gId = p.pgId AND g.gVisible = '1' ".$this->vQ('OR g.gVisible')."
    			ORDER BY gOrder ASC, p.Played DESC";
    			if($games = @mysql_query($getGameData)){
    				while($game = @mysql_fetch_assoc($games)){
    					if(!isset($this->gamedata[$game['gInCategory']])){
    						$game['gInCategory'] = 0;
    					}
    					$this->gamedata[$game['gInCategory']]['games'][$game['gId']] = $game;
    				}
    				$this->loaded = true;
    				return true;
    			} else {
    				return false;
    			}
    		} else {
    			return false;
    		}
    	}
    
    	function addPlay($gid){
    		$quickcheck = "SELECT Date FROM gamestoday WHERE Date = '".date("dmy")."'";
    		if(@mysql_num_rows(mysql_query($quickcheck)) < 1){
    			@mysql_query("INSERT INTO gamestoday VALUES ('".date("dmy")."', 0)");
    		}
    		$update = "UPDATE playstats SET Played = (Played+1) WHERE pgId = '".$gid."'";
    		$update2 = "UPDATE gamestoday SET GamesPlayed = (GamesPlayed+1) WHERE Date = '".date("dmy")."'";
    		if(@mysql_query($update) && @mysql_query($update2)){
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	function loadPlay($gid,$cid){
    		if($cid == 0){
    			$load = "SELECT g.gId,g.gSwfFile,g.gName,g.gInCategory,g.gDescription,g.gWidth,g.gHeight FROM
    			games as g WHERE g.gId='".$gid."'";
    		} else {
    			$load = "SELECT g.gId,g.gSwfFile,g.gName,g.gInCategory,g.gDescription,g.gWidth,g.gHeight,c.cName FROM
    			games as g, categories as c WHERE g.gId='".$gid."' AND g.gInCategory = c.cId";
    		}
    		if($data = @mysql_query($load)){
    			$data = @mysql_fetch_assoc($data);
    			if($cid == 0){
    				$data['cName'] = 'Other Games';
    			}
    			return $data;
    		} else {
    			return false;
    		}
    	}
    
    	function isLoaded(){
    		if($this->loaded == true){
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	function makeGamesList(){
    		if(!$this->isLoaded()){
    			$this->Load();
    		}
    		$output = "";
    		foreach($this->gamedata as $category){
    			$games = "";
    			$gamedata = "";
    			if(count($category['games']) > 0){
    				$count = 0;
    				foreach($category['games'] as $game){
    					if(($count % 2) == 1 || count($category['games']) == 1){
    						eval("\$games .= \"".$this->template['game']."\";");
    						eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    						$games = "";
    					} else {
    						eval("\$games .= \"".$this->template['game']."\";");
    					}
    					$count++;
    
    					if(count($category['games']) != 1 && (count($category['games']) % 2) == 1 && ($count == count($category['games']))){
    						eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    					}
    				}
    				eval("\$output .= \"".$this->template['category']."\";");
    			}
    		}
    		return $output;
    	}
    
    	function doMostPlayed($showonly = 10){
    		$list = "";
    		$action = "Top Games";
    		$getRecent = "SELECT pcId,pgId FROM playstats ORDER BY Played DESC LIMIT ".$showonly;
    		if($data = @mysql_query($getRecent)){
    			while($statdata = @mysql_fetch_assoc($data)){
    				$stat = $this->gamedata[$statdata['pcId']]['games'][$statdata['pgId']];
    				$extra = ' [ '.$stat['Played'].' plays ]';
    				eval("\$list .= \"".$this->template['list_repeat_all']."\";");
    			}
    			eval("\$return = \"".$this->template['list_wrapper_all']."\";");
    			return $return;
    		} else {
    			return false;
    		}
    	}
    
    	function doNewestGames($limit = 10){
    		$action = 'Newest Games';
    		$list = "";
    		$getRecent = "SELECT gId,gInCategory FROM games ORDER BY gId DESC LIMIT ".$limit;
    		if($data = @mysql_query($getRecent)){
    			while($statdata = @mysql_fetch_assoc($data)){
    				$stat = $this->gamedata[$statdata['gInCategory']]['games'][$statdata['gId']];
    				$extra ='';
    				eval("\$list .= \"".$this->template['list_repeat_all']."\";");
    			}
    			eval("\$return = \"".$this->template['list_wrapper_all']."\";");
    			return $return;
    		} else {
    			return false;
    		}
    	}
    
    	function makeGameHtml($id,$cid){
    		$game = $this->loadPlay($id,$cid);
    		eval("\$html = \"".$this->template['game_play']."\";");
    		return $html;
    	}
    
    	function makeOptionList(){
    
    		$opt = "";
    		$sortorder = array();
    		foreach($this->gamedata as $category){
    			foreach($category['games'] as $game){
    				$sortorder[$game['gName']] = array($category['cId'], $game['gId']);
    			}
    		}
    		ksort($sortorder);
    		foreach($sortorder as $game){
    			$opt .= '<option value="'.$_SERVER['PHP_SELF'].'?act=play&id='.$this->gamedata[$game[0]]['games'][$game[1]]['gId'].'&cid='.$this->gamedata[$game[0]]['games'][$game[1]]['gInCategory'].'">'.$this->gamedata[$game[0]]['games'][$game[1]]['gName'].'</option>';
    		}
    
    		return $opt;
    
    	}
    
    	function getPlaysToday(){
    		$get = "SELECT GamesPlayed FROM gamestoday WHERE Date = '".date("dmy")."'";
    		if($data = @mysql_query($get)){
    			$num = mysql_fetch_row($data);
    			return $num[0];
    		} else {
    			return '';
    		}
    	}
    
    }
    
    ?>
    
    PHP:
    on the index.php

    		<?php
    
    		if(!isset($_GET['act']) || $_GET['act'] != 'play'){
    			echo $sys->makeGamesList();
    		} else {
    			if(isset($_GET['id'])){
    				echo $sys->makeGameHtml($_GET['id'], $_GET['cid']);
    			} else {
    				echo '<div style="margin: 30px;">
    				<strong>Error: </strong>Invalid Input<br /><br />
    				<a href="'.$_SERVER['PHP_SELF'].'">Click here to return Home</a>
    				</div>';
    			}
    		}
    ?>
    
    PHP:
    thanks in advance
     
    DRoP, Jun 30, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    in the class under load(), change

    
    $getGameData = "SELECT g.gId, g.gDescription, g.gSwfFile, g.gVisible, g.gInCategory, g.gThumb, g.gName, p.Played FROM
                games as g, playstats as p WHERE g.gId = p.pgId AND g.gVisible = '1' ".$this->vQ('OR g.gVisible')."
                ORDER BY gOrder ASC, p.Played DESC";
    
    PHP:
    to

    
    $getGameData = "SELECT g.gId, g.gDescription, g.gSwfFile, g.gVisible, g.gInCategory, g.gThumb, g.gName, p.Played FROM
                games as g, playstats as p WHERE g.gId = p.pgId AND g.gVisible = '1' ".$this->vQ('OR g.gVisible')."
                ORDER BY gOrder ASC, p.Played DESC LIMIT 4";
    
    PHP:
    all i did was simply add the limit 4. change it to 'n' and voila. you have 'n' results returned.

    i believe this is where it retrieves the data after doing a quick scan of the code. if not, let me know and i will look more into it.
     
    ansi, Jun 30, 2007 IP
  3. DRoP

    DRoP Peon

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks very much for replying, i really appreaciate it.

    i tried your suggestion, at what it seems to do it, just display 4 games in total, what i am trying to achieve, really, is a way of displaying games in different categarys, and display just the first x in each cat on the index page..

    if you have any suggestions, i would love to hear them...

    and thanks again for the help... rep+
     
    DRoP, Jul 1, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    try this. not 100% sure on it though but it's worth a try. make sure you back up what you have now though before doing this.

    
    function makeGamesList(){
            if(!$this->isLoaded()){
                $this->Load();
            }
            $output = "";
            foreach($this->gamedata as $category){
                $games = "";
                $gamedata = "";
                if(count($category['games']) > 0){
                    $count = 0;
    				for($x=0;$x<=3;$x++)
    				{
    					//foreach($category['games'] as $game){
                        if(($count % 2) == 1 || count($category['games']) == 1){
                            eval("\$games .= \"".$this->template['game']."\";");
                            eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
                            $games = "";
    
                        } else {
                            eval("\$games .= \"".$this->template['game']."\";");
                        }
                        $count++;
    
                        if(count($category['games']) != 1 && (count($category['games']) % 2) == 1 && ($count == count($category['games']))){
                            eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
                        }
                    }
                    eval("\$output .= \"".$this->template['category']."\";");
                }
            }
            return $output;
        }
    
    PHP:
     
    ansi, Jul 1, 2007 IP
  5. DRoP

    DRoP Peon

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm, looks like it's getting there, it shows them pretty much how i wanted, but the data (thumbnail, info & swf) are missing... here's how it looked...

    [​IMG]

    It's a shame i know so little php... I will get there eventually... Maybe I should get reading.. or I suppose I might have to pay for a new scipt...

    I don't know if this info helps at all? It was in the help file...

    For Reference, here are all the fields in the three tables used, and a description of what they each are:

    categories
    cId - Unique Id for category
    cName - Name of category
    cOrder - Order number for category
    cVisible - Visibility of category, 1 = visible, 0 = invisible

    games
    gId - Unique Id for game
    gSwfFile - Name of game (SWF) file
    gName - Game Name
    gOrder - Order number for game
    gThumb - Game thumbnail Image
    gVisible - Visibility of game, 1 = visible, 0 = invisible
    gWidth - Width of game
    gHeight - Height of game
    gDescription - Description of Game

    playstats
    pgId - Game Id
    pcId - Category of game
    Played - The number of plays

    .......................................................

    Thanks again for the help ansi, you've definatly given me hope, much appreaciated.. :)
     
    DRoP, Jul 1, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    alright, let's try this:

    
    	function makeGamesList(){
            if(!$this->isLoaded()){
                $this->Load();
            }
            $output = "";
    
    		for($x=0;$x<=3;$x++)
    		{
    
    			foreach($this->gamedata as $category){
    				$games = "";
    				$gamedata = "";
    				if(count($category['games']) > 0){
    					$count = 0;
    					foreach($category['games'] as $game){
    						if(($count % 2) == 1 || count($category['games']) == 1){
    							eval("\$games .= \"".$this->template['game']."\";");
    							eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    							$games = "";
    						} else {
    							eval("\$games .= \"".$this->template['game']."\";");
    						}
    						$count++;
    
    						if(count($category['games']) != 1 && (count($category['games']) % 2) == 1 && ($count == count($category['games']))){
    							eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    						}
    					}
    					eval("\$output .= \"".$this->template['category']."\";");
    				}
    			}
    
    		}
            return $output;
        }
    
    PHP:
     
    ansi, Jul 1, 2007 IP
  7. DRoP

    DRoP Peon

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This seems to make all the categarys appear 3 times... :D
     
    DRoP, Jul 1, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    lol yeaah, thought about that after i posted it.


    try this....

    
    	function makeGamesList(){
            if(!$this->isLoaded()){
                $this->Load();
            }
            $output = "";
    
    			foreach($this->gamedata as $category){
    				for($x=0;$x<=3;$x++)
    				{
    				
    					$games = "";
    					$gamedata = "";
    					if(count($category['games']) > 0){
    						$count = 0;
    						foreach($category['games'] as $game){
    							if(($count % 2) == 1 || count($category['games']) == 1){
    								eval("\$games .= \"".$this->template['game']."\";");
    								eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    								$games = "";
    							} else {
    								eval("\$games .= \"".$this->template['game']."\";");
    							}
    							$count++;
    
    							if(count($category['games']) != 1 && (count($category['games']) % 2) == 1 && ($count == count($category['games']))){
    								eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    							}
    						}
    						eval("\$output .= \"".$this->template['category']."\";");
    					}
    
    				}
    			}
    
            return $output;
        }
    
    PHP:
    that "should" work i hope.
     
    ansi, Jul 1, 2007 IP
  9. DRoP

    DRoP Peon

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It looks although it's definatly possible, I suppose it's just a case of trial & error, I really appreaciate this help btw...

    I tried this, and what it does is display the last categary 3 extra times :)
     
    DRoP, Jul 1, 2007 IP
  10. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #10
    last time if not i give, sorry. it's almost 4am hehe

    
    	function makeGamesList(){
            if(!$this->isLoaded()){
                $this->Load();
            }
            $output = "";
    		foreach($this->gamedata as $category){
    			$games = "";
    			$gamedata = "";
    			if(count($category['games']) > 0){
    				$count = 0;
    				foreach($category['games'] as $game){
    					for($x=0;$x<=3;$x++)
    					{
    						if(($count % 2) == 1 || count($category['games']) == 1){
    							eval("\$games .= \"".$this->template['game']."\";");
    							eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    							$games = "";
    						} else {
    							eval("\$games .= \"".$this->template['game']."\";");
    						}
    						$count++;
    
    						if(count($category['games']) != 1 && (count($category['games']) % 2) == 1 && ($count == count($category['games']))){
    							eval("\$gamedata .= \"".$this->template['gdoublewrapper']."\";");
    						}
    					}
    					eval("\$output .= \"".$this->template['category']."\";");
    				}
    
    			}
    		}
            return $output;
        }
    
    PHP:
     
    ansi, Jul 1, 2007 IP
  11. DRoP

    DRoP Peon

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It displays the categarys many times, but thanks alot for the help...

    You've at least given me starting point, and it's much appreaciated.... :)
     
    DRoP, Jul 1, 2007 IP
  12. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #12
    well i didn't help much apparently but you're welcome :)
     
    ansi, Jul 1, 2007 IP