Jump to content

Multi-Category Page Management [PHP HELP][EDITED]


elhama

Recommended Posts

I've gotten into quite some trouble, see what I'm trying to do is put up the database data up in a list, but the problem is that the data is put up in categories like so:

Category 1:

1
2
3

Category 2:

1
2
3

(Total of 4 categories)
So the problem is now that I have to limit the data to a max of 25 per page, which is simple by itself, but also what I have to do is to keep it categorized, and I just can't seem to get through with it, can someone enlighten me please?

Here is some example code I've tried to fix, but it keeps messing up! The loops gets screwed up.. [B] Note: This is only testing/example code of what I'm meaning[/B]
[CODE]
<?
echo "<table border=1>";
if($_GET['page'] == ""){
        $numPage = 1;
}
else{
        $numPage = $_GET['page'];
}
$rowsPerPage = 2;
$offset = ($numPage - 1) * $rowsPerPage;
$testy = mysql_fetch_array(mysql_query("SELECT * FROM myfriends limit $offset, 1"));
$test = mysql_query("SELECT * FROM myfriends limit $offset, $rowsPerPage");
$category=$testy[category];
while(($category < 5) && ($t = mysql_fetch_array($test))){
        if($category==1)
        $cat_text = "Friends";
        if($category==2)
        $cat_text = "Family";
        if($category==3)
        $cat_text = "School";
        if($category==4)
        $cat_text = "The Rest";
        echo "<tr><td>$cat_text</td></tr>";
        $test3 = mysql_query("SELECT * FROM myfriends where category='$category' limit $offset, $rowsPerPage") or die(mysql_error());

                while($e = mysql_fetch_array($test3)){

                echo "<tr><td>$e[comment]</td></tr>";


                }
$category = $category+1;
}
echo "</table>";
$topage = $numPage +1;
echo "<a href=test.php?page=$topage>></a>";
?>
[/CODE]
Link to comment
Share on other sites

Granted, I'm no expert at this yet, but what I would do is have a different table for each category. 
I thought that I had a code xample but I don't...or can't find it anyway.

Basic idea is this:

Open a connection to the database, count the number of rows in each table (category) using different variables (obviously).  Then you can use a for loop to show each row (1, 2, 3) in the table.  When $var==$mysql_num_rows...not sure of syntax...then it will exit the loop and go onto the next table (category).  Hope this helps.  If you want me to take a look at what you've got I'd be more than willing to, email me at dsartain18@yahoo.com .  Later.
Link to comment
Share on other sites

Okay, I'm sorry..

I'll explain

You have a table with lets say 100 items. There are four categories and what you want to do is to put all of these items up on the website, but you want to keep a limit of max 25 items per page, so lets say one category has 10 items and one category has 15 items, that would mean it would look like this on the website, the thing is however I do it, it get messed up

Category 1
1
..etc..
10
Category 2
1
....
15

Next Page>>
Link to comment
Share on other sites

okay, but it depends on how your database is layed out, ie how your storing what item goes in what category. Here is is a simple way to do it

categories table
catid, catname, parent_id


All my main items have a parent_id of 0, all items in a cat have their associated parent_id. You see where I'm going? Easy to do it that way.

1, Category 1, 0
2, Category 2, 0
3, Category 3, 0
4, Item 1, 1
5, Item 2, 1
6, Item 1, 2
7, Item 2, 2
8, Item 3, 2

etc

Do a select where parent_id = 0 and get all the categories, then select the items where parent_id != 0, limit to 25, order by the parent_id. Hope that makes sense to you lol
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.