A simple category search from mysql table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

A simple category search from mysql table?

Post by Netroxy »

Hello. I am having a hard time building my own search results page for my mysql database. Basically I am trying to search for game categories from the fldCategory field in the table, and display 15 results at a time in an HTML table with the Image of the games (fldIconMedium), and the titles of the games (fldTitle) placed in to the table. Also, at the bottom of the table would have links with numbers that you can click to go to the next set of search results in the same page.

here is a link to show what I mean: http://netroxy.com/51274581336/tablehelp.htm
Last edited by Netroxy on Sun Apr 24, 2011 8:44 am, edited 1 time in total.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: A simple category search from mysql table?

Post by fugix »

to grab a certain category of game, you will need to make a query stating the string name of the category that you want to pull out
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A simple category search from mysql table?

Post by Celauran »

What have you got so far?
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

Re: A simple category search from mysql table?

Post by Netroxy »

Hello friends. Sorry for not responding sooner. For the code I am using, I had help from another expert. I thank him, but here is the code I have so far.

===========================
===========================

Code: Select all

<?php
$cat=1;
$nor=15;
$a=1;
if(isset($_GET['cat']))
{
}
else
{
$_GET['cat']=1;
}
echo $_GET['cat'];
$con=mysql_connect("host","nusername","password")
or die ("error");
$db=mysql_select_db("database",$con)
or die("Database not found!");
$result=mysql_query("SELECT * FROM games")
or die ("query error");
echo "<table border='1'><tr><th>ImageIcon</th><th>Title</th></tr>";
$r=mysql_num_rows($result);
echo $r."<br/>";
$p=ceil($r/$nor);
//echo $p;
while($rows=mysql_fetch_array($result))
{
if($a>($_GET['cat']-1)*$nor)
{
echo "<tr><td>".$rows['fldIconMedium']."</td><td>".$rows['fldTitle']."</td></tr>";
}
$a=$a+1;
}
echo "</table>";
for($i=1;$i<=$p;$i++)
{
if($i==$_GET['cat'])
{
echo $i;
}
else
{
echo "<a href='gameslist.php?cat=".$i."'>".$i."</a>";
}
}

?>
===========================
===========================

To see this code in action, heres the link http://netroxy.com/gameslist.php?cat=skills.

I am expecting the games search results page to look like this: http://netroxy.com/51274581336/search.htm

So basically its:

- viewing how many games found for game category, ex: Skills. Or Action, Mind, etc..

- the URL containing 'gameslist.php?cat=' with the given ID category at the end should search for that given category and display at least 15 games per results, along the number bar bellow, which counts how many are found from that category, which automatically increases or decreases the number bar bellow depending how many games from that specific category was found. I also forgot to mention that I did not add the Previous and Next button, which i dont know how :(

- As for the 15 results per page, its basically 15 images (fldIconMedium) of different games of the same category, with their Titles (fldTitle) underneath them and both image and title should have the LINK to that specific game. When those 15 results per page are displayed, the link 'http://www.blabla.com/games.php?id=' should be the link for all the title Text links and the image links, and it should add the ID (fldID) next to 'http://www.blabla.com/games.php?id=' for each game category found.
Last edited by Netroxy on Thu Apr 28, 2011 1:58 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A simple category search from mysql table?

Post by Celauran »

Netroxy wrote:

Code: Select all

$result=mysql_query("SELECT * FROM games") or die ("query error");
There's your problem. You aren't refining your query at all.
Netroxy wrote:Basically I am trying to search for game categories from the fldCategory field in the table, and display 15 results at a time in an HTML table with the Image of the games (fldIconMedium), and the titles of the games (fldTitle) placed in to the table.
Why not try something like this?

Code: Select all

$query  = "SELECT fldTitle, fldIconMedium FROM games WHERE fldCategory = {$_GET['cat']} LIMIT 15";
$result = mysql_query($query);
Note that this is just for the sake of example. Always sanitize your input before using it in a query.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: A simple category search from mysql table?

Post by fugix »

please use code tags netroxy
Post Reply