Flatfile or Array or DB?

Sorry for the beginner questions, but I’m finding it very hard to search for help when I don’t really know the terms I’m looking for.

Basically I’m trying to create a list of highest mountains in my state, which can be re-ordered based on what’s most important to you (difficulty of climb / height / region etc) and I was hoping to store all that in a flat file database. That’s looking more difficult that i expected, so can i store it within the php file as an array ? I’m hoping to avoid using a DB just to keep it simple. It could run to 500+ entries if I ever list everything.

Is there a better way of doing this or should I just make the database already ?

More than 500 mountains?

Assuming the data is unlikely to change during our lifetime I think you might be able to get by using a CSV (Comma Separated Variables) file.

1 Like

Thanks, that looks like what I’m after !

Yes, more than 500 peaks, maybe not all considered mountains. There’s 160 on the main list, but many others people are interested in.

If you want to keep things simple then you should never ever use a car but walk everywhere on your foot.

Database is the only choice.

The sooner you understands that, the more time you’ll save yourself, instead of wasting it on the “simpler” solutions.

Besides, for the life of me I won’t understand what could be simpler that a sentence in almost natural English, that says something like

SELECT * FROM mountains WHERE region='WI' and name LIKE 'Mont%' ORDER BY height DESC

that will give you your results for which you will need to write several pages of code using PHP array.

I agree that deciding to not use a database could come back to bite.

For example, if later “admission fee” or “hours of operation” or “adjoining States” were wanted maintaining a CSV file could quickly become a maintenance nightmare.

But if the data is, and always will be, unchanging like
Peak name, Region, Elevation, Difficulty
I don’t think a database would be needed. A viable option and more flexible, but not needed.

Every application tend to grow.

Trust me, in no time you would like to have editabe text descriptions, linked regions, new mountains updated and other stuff. It’s good to see at least one step ahead.

1 Like

SELECT * FROM mountains WHERE region=‘WI’ and name LIKE ‘Mont%’ ORDER BY height DESC

that will give you your results for which you will need to write several pages of code using PHP array.

The ability to search in just that fashion is important. Thanks, that changes things. I have no interest in making this any bigger, nor can I imagine how it will grow, but I can always create and maintain the database by hand.

I’m already way out of my coding depth on this, but it’ll be a fun project !

1 Like

Certainly a database. For such simple and low-volume data a good solution is SQLite - this is a database based on a single file but providing SQL capabilities like the ones mentioned above by other posters. Whenever you are considering using a flat file as a simple database - you should probably use SQLite. Of course, you can also use a full featured database like MySQL, nothing wrong with that. It’s just that setting up an SQLite database is simpler since it’s built-in into PHP and no configuration is required apart from creating an empty file and configuring the file path.

I agree with colshrapnel.

Software should be built to facilitate growth. In this case that would be using a database instead of flat file storage. Not to mention you will learn something new in the process.

While I don’t disagree with eventually learning how to interface with a sql database there is a bit of a learning curve which could be a distraction if you just want to get something working. Furthermore, there are also non-sql databases (often called NoSql) which are becoming more popular.

You might consider using Yet Another Markup Language (YAML) as a starting point. A YAML file is a text file that is perhaps a bit easier to read and update than a csv file.

Your database might look like:

# mountains.yml
  name: Mount Lookatthat
  height: 86000 feet
  name: Mount Rushmore
  height: 2000 feet

It’s easy to convert your yaml file into a php array:

use Symfony\Component\Yaml\Yaml;

class MountainRepository {
  private $mountains;
  public function __construct() {
    $this->mountains = Yaml::parse(file_get_contents('mountains.yml'));
  public function findByName($name) ...
  public function findByHeightRange($height1,$height2) ...

So the idea is to use a repository to interface with your data store. At some point if you do decide to move to a sql database then you just implement a different repository. Your controllers and views can pretty much stay the same.

The yaml format can also make it a bit easier to design tree type databases. Let’s say you want to list the wild life available:

  name: Mount Oz
    ~ Lions
    ~ Tigers
    ~ Bears
  name: Mount Fluffy
    ~ Bunnies
    ~ Squirrels

You can do these kinds of one to many relations in sql but again there is a bit of a learning curve and it’s easy to get bogged down with the details when all you really want to do is to display some information.

Just a suggestion of course.

For a beginner I think that is the more complex approach to simply connecting to MySQL and running some queries.

For a beginner I think that is the more complex approach to simply connecting to MySQL and running some queries.

Just my opinion of course but I feel that opening a text editor is not significantly more complex than installing and configuring a database provider, creating and populating one or more database tables and then mapping the results of a query into something useful.

Ha, I just thought the same - “a repository to interface with your data store”, “controllers and views”, “yaml format” - doesn’t sound to me simpler than learning to use a database, perhaps of similar complexity, however I think learning an SQL language is a more valuable and universal skill as a starting point, not to mention that querying a database with sorting and searching is much faster than doing the same on php arrays.

Fair enough

However, the first few chapters of any PHP book will probably teach some basic MySQL stuff. However, they aren’t very likely to go into dependency management via composer, Symfony components and connecting it all together. That is what I formed my opinion on.

Thanks for all the input guys.

I’ll use MySQL, but only because I’ve spent time editing other scripts so I’m at least a little familiar with the structure, queries, and stuff like phpMyAdmin.

I should see this as a stepping stone to other scripts I could write to automate stuff and make useful web tools !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.