Define pages within category

Hi, if I want to sepcify certain elements to a category:

<?php if (category_id == 3) { ?>Content for that paticular category.<?php } ?>

How would I set it for the pages within the category? - I can do it like so:

<?php if (category_id == 3 || page_id == 4 || page_id == 5) { ?>Content for that paticular category.<?php } ?>

But that means everytime I add a new page within that category I would have to manually add the new page_id to the list.

Is there a way to write it so it can display category_id and all pages within?

Any help would be great, thanks.

Paul

Do pages belong to categories?

I think most cases would involve your data model storing this information.


pages_table
=========
id
page_name
category_ref

So that when you extract a page, you fetch its category with it.

ps in your example variables like category_id should be $category_id is that an oversight?

This is the main variable:

<?php $category_id = isset($_GET['category_id']) && is_numeric($_GET['category_id']) ? (int) $_GET['category_id'] : 0; ?>

In the database the category is listed for each page, I always assumed if you just chose the category it would select it for all the relevant pages.

Each page_id is defined to a category_id - not sure how to do it though to pick up all the pages as well as the category index page.

Lets say your first goal is to get a list of “all pages in this category” (all content in this category)

When the page loads, does it know its own id and category somehow?


<?php

$page_id = 23 ;
$category_id = 1 ;

// content of page
?>

If so then you need to have an sql query which does the equivalent of this:


SELECT
  page_id
, page_name 
FROM pages_table
WHERE category_ref = $category_id

and then loop through those results.

The page knows which page_id and category_id it is … not sure on the looping process though.

Thought could so some thing with the <?php (category_id == 3) ?> solutuon… :slight_smile:

I have written a basic function:

function get_pages($category_id) {
	$result = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['config']['db']['prefix'] . "pages WHERE category_id = $category_id AND status = 1 AND default_page = 0 ORDER BY title");
	return fetch_array($result);
}

Could I use this do you think? - something like <?php getpages(3); { ?> … but I am unsure on how to write the getpage function like to call the data for the pages?

Well get_pages is returning an array, so you could loop through that array in your template (is it?)


<?php
foreach( get_pages(3) as $row){
echo '<a href=page.php?id=' . $row['id'] . '>' . $row['page_name'] . '</a><br /> ' ;
}
?>

fetch_array isn’t a native function IIRC, does/can it return more than one row?

Bit lost lol - Cups, what you did looks good but I am not worried about pulling page id’s - I want to echo Content which will display in the category, and all the pages within that category :slight_smile:

Cheers

Just so we’re clear :stuck_out_tongue:

Are you wanting to generate a page structure similar to…


<h1>Category</h1>

<div class="page">
  <h3>
    Page Title
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=1">
    Read more
  </a>
</div>

<div class="page">
  <h3>
    Page Title
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=2">
    Read more
  </a>
</div>

Where each of these 2 pages, belong in the same category?

Nope - I want to write an if statement syaing " if you’re on category 3 then show:" (<?php if (category_id == 3) { ?> content here <?php } ?> … but I want all the pages within the category to also show the same content, rather than stating if page_id == 1, page_id == 2 and so on … its just so if I add a new page to a certain category I don’t have to go and manually update the page with the new page_id - I want it so all content within the specific category, also does the same for the pages within the category. I hope that makes sense :slight_smile:

Cheers

No, it doesn’t.

For a moment, forget you know PHP. No ifs, no ids, nothing - pretend you’re a client. :stuck_out_tongue:

Show me the HTML (or the output), you would like to see for 2 pages in the same category and 2 pages in a different category. This will show us what changes, and what doesn’t.

For example:-


<h1>Sausages</h1>

<div class="page">
  <h3>
    Blugh, sausages are wierd!
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=1">
    Read more
  </a>
</div>

<div class="page">
  <h3>
    Sausages, the food of the poor.
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=2">
    Read more
  </a>
</div>


<h1>Bacon</h1>

<div class="page">
  <h3>
    We like Bacon, what about you?
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=1">
    Read more
  </a>
</div>

<div class="page">
  <h3>
    Bacon rocks, mroe so with Brown Sauce.
  </h3>
  <p>
    Some page content...
  </p>
  <a href="page.php?id=2">
    Read more
  </a>
</div>

Ok, in one category I want a menu relevant to that category shown, and in another category I want the menu relevant to that category shown… I have 3 menus.

First one is default to home page/ and sub pages such as contact/ about etc.

The other 2 menus are for Country 1, and County 2. - they of course have different pages relevant to the county (cities, museums etc).

Cheers

What does that ‘menu’ contain?

I had surmised, perhaps wrongly, that you wanted a list of pages in the same category.

Example - Category 1 menu:

Home
Link 1
Link 5
Contact

Category 2 menu:

Home
Link 3
Link 4
Contact

Category 1 and subpages to display the relevant menu and the same for Category 2 to display the relevant menu.

Well every menu contains home and contact, and as long as you have set up your database so that Link 1 and Link 5 are associated with category 1, then something like this should do it:


 
<ul class-"menu">
<li><a href="/home.php"></a></li>
<?php

foreach( get_pages(1) as $row){
echo '<a href=page.php?id=' . $row['id'] . '>' . $row['page_name'] . '</a><br /> ' ;
}

?>
<li><a href="/contact.php"></a></li>
</ul>


How have you proved to yourself that get_pages(1) actually returns the values Link 1 and Link 5.

ps Naming variables soon becomes a critical issue, but so does naming your test data.

Using memorable test values, like Anthony’s Sausages and Bacon is a really good idea because it might make it obvious when we are getting conflicting problems, whereas Link 1 and Link 3 do not immediately throw into high relief any error you might have caused somewhere else.

It also makes it easier to explain, and provides a bit of fun too.

For the sake of argument lets say your 2 menus were Meat products and Vegetables?

Ok, I think we’re still not on the same wavelength.

The menu is a set menu - I do not want to add to the menus - I just want the menus to display on the category page, as well as the sub pages.

So if the Meat category has an index page and also has 2 pages, Bacon and Sausages - I would like those 3 pages to display the same menu.

OK, from where, or what, is that “set menu” actually derived?

Are you controlling that menu by hand or can it be “smart” are derived from your data model?

The menu is just a tpl.php file - when I add a new page to a category I go to the menu.tpl.php file and add page_id == (number) for the relevant menu.

So you keep a list of “pages” in a database, but you maintain a list of pages manually as well?

This is not the best idea, if your menus could be generated automatically by the computer.

Is that what you want to happen?

Otherwise just keep a straight html file with the menu written as an UL or whatever, and just include that file in the corresponding part of you tpl file.