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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.