elhama Posted August 10, 2006 Share Posted August 10, 2006 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:123Category 2: 123(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] Quote Link to comment Share on other sites More sharing options...
dsartain Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 10, 2006 Share Posted August 10, 2006 Your question doesnt make since... What exactly are you tryin to pull from the database? And how do you want it displayed? Quote Link to comment Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
elhama Posted August 10, 2006 Author Share Posted August 10, 2006 Okay, I'm sorry..I'll explainYou 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 11..etc..10Category 21....15Next Page>> Quote Link to comment Share on other sites More sharing options...
DylanBlitz Posted August 10, 2006 Share Posted August 10, 2006 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 itcategories tablecatid, catname, parent_idAll 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, 02, Category 2, 03, Category 3, 04, Item 1, 15, Item 2, 16, Item 1, 27, Item 2, 28, Item 3, 2etcDo 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 Quote Link to comment Share on other sites More sharing options...
elhama Posted August 10, 2006 Author Share Posted August 10, 2006 Bump, still need help. Quote Link to comment Share on other sites More sharing options...
wannabephpdude Posted August 10, 2006 Share Posted August 10, 2006 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. ;) Quote Link to comment Share on other sites More sharing options...
elhama Posted August 10, 2006 Author Share Posted August 10, 2006 You can't put them seperatly, because then you can't put the 25 per page limit on the query Quote Link to comment Share on other sites More sharing options...
wannabephpdude Posted August 10, 2006 Share Posted August 10, 2006 Perhaps you can build a file for each catagory (with your limits [25]) and use include() or require() for each.// cat 1include('file1');//cat 2include('file2');//cat 3include('file3');Just a thought... Quote Link to comment Share on other sites More sharing options...
elhama Posted August 10, 2006 Author Share Posted August 10, 2006 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.. Quote Link to comment Share on other sites More sharing options...
wannabephpdude Posted August 10, 2006 Share Posted August 10, 2006 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.:) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.