Jump to content


Photo

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


  • Please log in to reply
11 replies to this topic

#1 elhama

elhama
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 10 August 2006 - 02:17 AM

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.. Note: This is only testing/example code of what I'm meaning
<?
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>";
?>


#2 dsartain

dsartain
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 10 August 2006 - 02:40 AM

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.

#3 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 10 August 2006 - 03:15 AM

Your question doesnt make since...  What exactly are you tryin to pull from the database?  And how do you want it displayed?
Why doesn't anyone ever say hi, hey, or whad up world?

#4 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 05:34 AM

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.

#5 elhama

elhama
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 10 August 2006 - 05:43 AM

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

#6 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 05:54 AM

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

#7 elhama

elhama
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 10 August 2006 - 08:31 AM

Bump, still need help.

#8 wannabephpdude

wannabephpdude
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationDallas

Posted 10 August 2006 - 08:53 AM

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.
;)

#9 elhama

elhama
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 10 August 2006 - 08:54 AM

You can't put them seperatly, because then you can't put the 25 per page limit on the query

#10 wannabephpdude

wannabephpdude
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationDallas

Posted 10 August 2006 - 08:58 AM

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

#11 elhama

elhama
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 10 August 2006 - 09:02 AM

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

#12 wannabephpdude

wannabephpdude
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationDallas

Posted 10 August 2006 - 09:08 AM

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.

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users