PHP MySQL Categories

Umbreon

Paragon
Joined
Jul 22, 2009
Messages
1,551
Reaction score
2
FP$
1,032
Sorry for asking too many questions, im not so good with mysql and php.

How do I put my games into categories using mysql and php. There will be about 35 categories. When they click the Actions category, then it shows action games (that I set in the games table)

(can someone write a code for me, thanks)
 
Well im gonna make about 3 or 2 first and then ill continue to add more later.
 
What I mean is will a game be listed under more than one category? Just so I know which direction to go.
 
Alright, let's begin 🙂

First, you need to add an additional column to the table with your games called 'category'. Let's make it varchar(100). This will hold the categories that each game will be associated with. For each game that you have, you'll need to add the categories separated by a comma and without spaces. For example, if the game is Call of Duty 4 you can put 'shooter,fps,strategy' in the categories column.

On the webpage, you can do something like this to select the games on per each category:

Code:
  $result = mysql_query("SELECT * FROM table_name WHERE category LIKE '%$category%'");
   while($row = mysql_fetch_array($result))
    {
     // you can echo all of the game info from the table here
    }

So if $category = shooter, it will bring up each game that you have listed as a shooter. Will you have all of the categories displayed on one page, or one category per page?
 
Ah that makes it a little easier. I'll assume you want to get the category from the url as with the other topic. So if this category is 'shooter', it will bring up each game with the word 'shooter' in the category and display a link to that game.

Code:
        <?php

        require_once(mysql.php);
        mysql_select_db("ruko_fp", $con);

        $thispage = $_SERVER["REQUEST_URI"];
        $category = strstr("$thispage","=");
        $category = substr($category, 1);

        $result = mysql_query("SELECT * FROM table_name WHERE category LIKE '%$category%'");
        $num_rows = mysql_num_rows($result);
        if($num_rows == 0)
         {
           echo "That was not a valid category.";
           // You can make the above line whatever you wish, it just tells people that no games with that category were found
         }
        else
         {
           while($row = mysql_fetch_array($result))
            {
             echo <a href=\"viewgame.php?gid=".$row['gid'].">".$row['gametitle']."</a><br />";
             // generates a link to each game in this category and inserts a line break each time
            }
          }

        ?>

I'm not sure if you want to display the rating and whatnot alongside each game so I left that out, if you do just let me know
 
I tried refreshing my cache and test in different browsers and its still blank.

Code:
Code:
        <?php

        require_once('mysql.php');
        mysql_select_db("ruko_fp", $con);

        $thispage = $_SERVER["REQUEST_URI"];
        $category = strstr("$thispage","=");
        $category = substr($category, 1);

        $result = mysql_query("SELECT * FROM games WHERE category LIKE '%$category%'");
        $num_rows = mysql_num_rows($result);
        if($num_rows == 0)
         {
           echo "That was not a valid category.";
           // You can make the above line whatever you wish, it just tells people that no games with that category were found
         }
        else
         {
           while($row = mysql_fetch_array($result))
            {
             echo "<a href=\"ViewGame.php?gid=".$row['gid'].">".$row['gametitle']."</a><br />";
             // generates a link to each game in this category and inserts a line break each time
            }
          }

        ?>
 
Did you add the 'category' column in your games table and add the categories for each game?
 
Could you post or pm me a picture of the table from phpmyadmin? I'd like to see it.
 
Back
Top Bottom