graham23s Posted June 17, 2007 Share Posted June 17, 2007 Hi Guys, i'm attempting to echo out all the categories i have in mysql then using $_GET to get the id of the specific category like: ## browse by category ########################################################### if($_GET['cat_id'] == 'cat_id') { echo 'SPECIFIC CATEGORY HERE!'; } ## browse by category ########################################################### echo '<a href="browse_files.php?catid='.$cat_id.'" />Category 1</a>'; is this the best way to get all the categories, i'm not to sure where to go from here. thanks for nay help Graham Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 17, 2007 Share Posted June 17, 2007 Yes for sure... it's a good way to set the category. But beware of the big monkey... you have to make that secure when you use Sql. And also look if the category is set: <?php if (isset($_GET['category_id'])) $cat_id = intval($_GET['category_id']); else $cat_id = 1; //rest of the code ?> Quote Link to comment Share on other sites More sharing options...
Nhoj Posted June 17, 2007 Share Posted June 17, 2007 Could do something like.... <?php $query = mysql_query('SELECT `id`, `name` FROM `categories` ORDER BY `name` ASC'); while ($category = mysql_fetch_assoc($query)) { echo '<a href="browse_files.php?catid='.$category['name'].'">Category 1</a><br>'; } ?> That will list all the categories for ya... Then to display whatever from the specific category you could do: <?php if ($_GET['cat_id']) { $_GET['cat_id'] = round(abs(mb_strcut($_GET['cat_id'], 0, 10))); $category = mysql_fetch_assoc(mysql_query('SELECT `name` FROM `categories` WHERE `id` = '.$_GET['cat_id'])); if (!$category) { echo 'No category was found with the information provided.'; } else { echo $category['name']; } } ?> That will clean the integer for you also so you don't have to worry about SQL injection. Quote Link to comment Share on other sites More sharing options...
graham23s Posted June 17, 2007 Author Share Posted June 17, 2007 Hi Guys, that is ideal exactly what i was after, is there a way i can limit the amount of categories per row that's displayed on the screen? i have 30 categories and they are all lined up 1 after each other can i limit them to say 10 per row? thanks guys Graham Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 17, 2007 Share Posted June 17, 2007 Do you mean you want a list of 10, with links underneath such as Previous and Next, or maybe page numbers? (rather like the likes to the various pages on this forum) In which case, look into using pagination. Take a look at these tutorials to help you out: http://www.phpfreaks.com/tutorials/43/0.php http://www.phpfreaks.com/tutorials/73/0.php Quote Link to comment Share on other sites More sharing options...
graham23s Posted June 17, 2007 Author Share Posted June 17, 2007 Hi Mate, i was thinkin more like: cat 1 cat 2 cat 3 cat 4 cat 5 cat 6 cat 7 cat 8 cat 9 cat10 instead of: cat 1 cat 2 cat 3 cat 4 cat 5 cat 6 cat 7 cat 8 cat 9 cat 10 cheers mate Graham Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 17, 2007 Share Posted June 17, 2007 Ah i see, try something like: <table border="1"> <?php $query = mysql_query('SELECT `id`, `name` FROM `categories` ORDER BY `name` ASC'); $number_in_row = 2;//edit to however many you want $cellwidth = 100/$number_in_row; $x = 0; while ($category = mysql_fetch_assoc($query)) { if($x % $number_in_row == 0) echo '<tr>'; echo '<td width="'.$cellwidth.'"><a href="browse_files.php?catid='.$category['name'].'">Category 1</a></td>'; if(($x+1) % $number_in_row == 0) echo '</tr>'; $x++; } //fill in with empty cells while(($x % $number_in_row != 0)){ echo '<td width="'.$cellwidth.'"> </td>'; $x++; } ?> </tr></table> I even made it easy for you to change the number in each row Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.