Jump to content

Best way to structure a classifieds site?


kyle
 Share

Recommended Posts

I want to start using ProcessWire on larger projects, so I was going to start with a little personal project.

I am going to attempt to make a Craigslist clone, but I am not sure the best way to structure the site. There will be cities and within the cities there will be ads.

My original idea was this:

Main Ad Pages

-- Electronics

-- Furniture

-- etc..

If you are a registered user you have a city associated with your username.  When you visit one of the categories it gets your city and finds all of the ads from the same city.  I just don't know how scalable this would be.

------------------

Alternatively, my other idea was this:  

City 1

-- Electronics

-- Furniture

-- etc...

City 2

-- Electronics

-- Furniture

-- etc..

Which idea would be the best?  Or is there a different way to do this?  Ideally, I am looking for the most scalable solution. 

Link to comment
Share on other sites

@diogo

Thanks for the link.  I wasn't sure if it was applicable to my situation, because I am not sure if this would be a good use case for pagefields.  Do you think pagefields would work well with what I'm trying to accomplish? My main concern is that different ad types have different fields associated with them.

Link to comment
Share on other sites

@Kyle,

The principle is the same. Basic questions to ask yourself:

  • Can an Ad belong to more than one type (e.g. electronic, furniture, etc.)
  • Can an Ad apply to more than one City?

Remember, page fields give you access to ALL information about pages selected via the page field...e.g. $page->selected_single_page_field_name->title will give you the title of the page currently selected in that page field (here, single page reference field type)...You can do this for other fields belonging to the selected page, can iterate its other fields, etc.

Link to comment
Share on other sites

@kongodo

Thanks for your help again!  So to answers these questions: no, an ad can only belong to one category and one city.  So I guess I am having a little trouble understanding the fundamentals of page reference fields.

With this link: http://processwire.com/videos/page-fieldtype/ it goes about showing related pages.  So how would that work into sorting ads by categories and cities?

How is that an advantage to sorting things differently than:

 

City 1

-- Electronics

-- Furniture

-- etc...

City 2

-- Electronics

-- Furniture

-- etc..

Link to comment
Share on other sites

Kyle,

If an AD can only belong to one City and one Category, then I would probably do it like below (almost similar to what you have...). Cities would be parents of ADs. Categories would be children of Category...

City 1

AD 1 - C1

AD 2 - C1

City 2

AD 1 - C2

AD 2 - C2

AD 3 - C2

AD 4 - C2

City 3

AD 1 - C3

AD 2 - C3

Category

Electronics

Furniture

Books

This will keep my setup DRY. I do not have to repeat categories like you have in your example. ADs will have Single Page Reference Fields. I would restrict selection in this field to just children of Category (electronics, etc.).

Cities will have a "cities" template with minimal fields. ADs an "ads" template with all the necessary AD fields + a Single Page Reference Field to select one category. Categories would have "categories" template probably with just a title.

Searching:

Written quickly, there could be errors, etc :)

//find all ads of a city. 1007 is the ID of the city; you can use path or name of the city as well
$ads = $pages->get(1007)->children("sort=title, limit=50");

//find all ads of a city that belong to electronics category. "category" is the name of the single page reference field. "electronics" is the name (title works as well I think?) of the category page
$elecronicAds = $pages->get(1007)->children("sort=title, limit=50, category=electronics");

//find all ads belonging to electronics irrespective of city
$elecronicAds = $pages->find("category=electronics, limit=50");

//find all ads and sort them by city, then category
$allAds = $pages->find("template=ads, sort=parent, sort=category, limit=50");

There's probably better ways of doing this + you have other selectors at your disposal, but this should give you an idea :)

  • Like 6
Link to comment
Share on other sites

Edit: ignore this post. The reason is explained by kongondo three posts below  :)

I would do it in a different way. Since the city is associated with the user, makes less sense to have the ads as children of the cities. I would probably put the ads under the user himself. Like this you would have cities and categories as page fields.

users

-- user 1

---- ad 1

---- ad 2

---- ad 3

-- user 2

---- ad 1

---- ad 2

categories

-- electronics

-- furniture

-- books

cites

--city 1

--city 2

edit: the ability to have the ads as children of users depends a lot of how you will create the users. But you would get the same effect by having all the adds under a "ads" parent, and connect to the user with a page-field or simply using "$page->createdUser"

  • Like 2
Link to comment
Share on other sites

Aaah, I forgot the user bit. Thanks Diogo...However, the ADs belong to the City and I think they should be together. In addition, I don't think the City is associated with the user. I think it is the other way round. The user is associated with the City. If the user is no longer registered, the City remains :). Would it not be possible to restrict users to their Cities using the City template (access rights)? This way, we only have one instance of each AD but this can be accessed by multiple users. In your example, are those ADs repeating for each user? I would create users using normal PW access model. Maybe I am not reading this properly. 

  • Like 1
Link to comment
Share on other sites

Maybe the best would be like I told on the edit. To keep everything separated and connected with page-fields.

are those ADs repeating for each user?

No, where you read "ad 1" i mean "ad 1 from user 1" and "ad 1 from user 2" :)

Link to comment
Share on other sites

Maybe the best would be like I told on the edit. To keep everything separated and connected with page-fields.

No, where you read "ad 1" i mean "ad 1 from user 1" and "ad 1 from user 2" :)

I think this is where the confusion arises. I am assuming Kyle means "ADs for User 1, etc"...according to their City, but your example suggests "ADs by User 1, etc". Question is, who is posting the ADs?; these Users or others? :D

  • Like 2
Link to comment
Share on other sites

Ah thanks for the help this does give me some good direction.  So what I wanted to do was actually have it so if you're not logged it you see all the ads, and if you are logged in you can only see ads from your city.  So I guess the user would kind of belong to a city.

Link to comment
Share on other sites

 So what I wanted to do was actually have it so if you're not logged it you see all the ads, and if you are logged in you can only see ads from your city. 
if($user->isLoggedin()) {
  // display ads from your city
} else {
  // display all the ads
}
  • Like 2
Link to comment
Share on other sites

Complementing Ryans code. If you have a page field on the user template that points to one city page, and you have the ads as children of pages:



if($user->isLoggedin()) {
// display ads from your city
    foreach($pages->find("parent=$user->city" as $ad)){
        echo $ad->render();
    }
} else {
// display all the ads
    foreach($pages->find("template=ad" as $ad)){
        echo $ad->render();
    }
}



Of course it has to be even more complemented with pagination and those things... but it gives you an idea

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...