Jump to content

Selecting a category to browse


graham23s

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/55957-selecting-a-category-to-browse/
Share on other sites

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
?>

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.

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

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

 

 

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 :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.