Jump to content

Archived

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

elhama

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

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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Your question doesnt make since...  What exactly are you tryin to pull from the database?  And how do you want it displayed?

Share this post


Link to post
Share on other sites
You aren't giving enough info, if you give at least your database table layout and maybe a bit of code of how you are showing the data it would help. We've got no clue how you have your categorization done in the database.

Share this post


Link to post
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>>

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
If the catagories just have to be separated, you may need to call a query for each. If not, sort by 'catagory' in your query.
;)

Share this post


Link to post
Share on other sites
You can't put them seperatly, because then you can't put the 25 per page limit on the query

Share this post


Link to post
Share on other sites
Perhaps you can build a file for each catagory (with your limits [25]) and use include() or require() for each.

// cat 1
include('file1');
//cat 2
include('file2');
//cat 3
include('file3');

Just a thought...

Share this post


Link to post
Share on other sites
Well then the total limit will be 75, plus maybe one of the categories are 0 and one of the categories are 10? The whole script will get messed up..

Share this post


Link to post
Share on other sites
Have you been able to get one catagory to display correctly? If so make a file to display each catagory. Then include() them into a master file.

:)

Share this post


Link to post
Share on other sites

×

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.