Jump to content


  • Posts

  • Joined

  • Last visited

  • Days Won


Posts posted by ignace

  1. But is it okay to "just do things right", are there things a real good database designer could do which would make the programmer's life better?

    Usually the DB admin will create VIEWs and PROCEDUREs. It makes it easier to query data and as a bonus adds a layer of security.


    Say you have three tables: teachers, students, and parents, and each have a first name, last name, username, email, bla, bla, bla, and also a couple of other unique to each fields. Would you create a separate table for each?

    A teacher, a student, and a parent all would be in the same table. It's possible to create additional dependent tables for each if they have information specific to them. For the parent to signify the type of parent/family relation: dad, mom, ..


    For the teacher this would be for additional relations to courses he teaches. For the student to which courses he is enrolled etc.. So in this case the extra dependent tables are required as you need it to create additional relations.


    Do we get fancy (i.e. complicated) and have the application create the query?

    Keep it simple. Start with something that could possibly work. Is one table sufficient? Then start there. Do you have extra use-cases, then keep refactoring your DB design until it satisfies all use-cases. Never go creating tables because maybe in the future you may need them. You Aint Gonna Need It (YAGNI).

  2. A database design has never implications on your PHP code. A good database design ensures the data and it's constraints are met no matter the programming language used. Otherwise use NoSQL, this is the easiest way to store your array's. No more worries about join's or SQL for that matter.


    Also if added_by, deleted_by should be on the m2m table then person_has_document does not exist. Because then a stranger can add documents to you? Or even delete your documents? Also in my example above this should have been person_owns_document. Which is basically the same as added_by. So the table is useless.

  3. It's never a good idea to simply write table1, table2, table3 as it becomes more difficult to understand what each thing is and developers then start to abstract it to document/type.


    If the tables represent person, company, and project. Then write that. Each of these entities have more columns then just id and data. So they should have their own table. As such they should also have their own m2m tables as in:






    This means your first design is correct. In the second design you assume there is a shared PK and columns between project, company, and person. In a natural PK DB design there is no shared PK, for a company this is the vat number, for a person his ssn number, and for a project the internal company code. If you use a surrogate PK like a GUID this is possible but it would have no shared columns (and no name is not a shared column for a person this is the first_name/last_name, a company is registered_name, and for project this is the title). So the first design remains the way to go.


    added_by, deleted_by, added_at, deleted_at should not be in the m2m table but instead be on the document table and be a FK to the person table. I would also add a is_deleted field instead of checking deleted_by IS NOT NULL. That way you can constrain the deleted_by field to ON DELETE SET NULL when you remove a person from the system and your document remains deleted.

  4. Okay I changed the code a bit:


    function changeColor(color) {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0);
        var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        var data = imgData.data, red, green, blue, alpha;
        var rgb = HexToRGB(color);
        var hsl = rgbToHsl(rgb.R, rgb.G, rgb.B);
        for (var i = 0, l = data.length; i < l; i += 4) {
            red = data[i];
            green = data[i + 1];
            blue = data[i + 2];
            alpha = data[i + 3];
            if (alpha < 200) continue;
            var imgHsl = rgbToHsl(red, green, blue);
            var newRgb = hslToRgb(hsl.h, hsl.s, imgHsl.l);
            data[i] = newRgb.r;
            data[i + 1] = newRgb.g;
            data[i + 2] = newRgb.b;
            data[i + 3] = 255;
        ctx.putImageData(imgData, 0, 0);
    This works for some reason well, I can now create blackish and brownish cars. White however I can't get to work. Because I maintain the luminance of the original image (which is red) the black or brown also appears lighter, so I somehow would need to adjust the luminance of the car according to the color I selected. For dark colors, make it darker, for lighter colors make it lighter.


    I also can't help but feel that this sort of thing has to be in some sort of game library but I tried EaselJS and THREE but none appear to have this feature.

  5. Hi,


    I am building a 2D car configurator and I need some help with the coloring. Currently I am using this technique to color the car:



    But now I am wondering how can I select a color using a color picker like this:


    <form action="" method="post">
      <label for="color">Pick color</color>
      <input type="color" name="color" id="color">
    And then transform the selected color to match the car's (thus keeping the reflection). I am not looking specifically for code more the name of the algorithm or technique to get me on my way.


    I am guessing it's something with taking the RGB value of a pixel of the car and then somehow closely represent this color in the other color. Entering that in Google returns squat. Maybe one of you have experience in this sort of thing?

  6. If you want to assign energy points to players every 5 minutes you'll need something like a cronjob that runs a script that awards energy points every 5 minutes. Something like:


    */5 * * * * mysql -uMyUsername -pMyPassword -e 'UPDATE players SET energy_points += 1' mydbname
    To train a specific stat you can then create a simple form where they select a stat and that stat gets 'trained'.


    That's basically it.

  7. If you have the finances you can find someone to develop it for you but then of course you need to be sure of a return on investment. Otherwise it becomes a blackhole where you throw money at. There are more efficient ways to make money with money (investing et al).


    If you want to learn how to develop a game go to https://unity3d.com/learn/tutorials. It contains tutorials on how to get started.


    If you need models you can also buy them here:



    Though I would keep it 'free' for as long as possible (having 2 squares fight each other is fine for a prototype). A game that costs you thousands but never provide any returns isn't a good investment even if you have a great cashflow.

  8. If the problem persists to elude you. You can take the agile approach and use iterative development to get to the solution. What this means is that you start with very simple code that could work. For example a simple array with a mapping between products and tags/categories:


    $tags = array(
      'woman' => 'Woman (16 - 35)',
      'infant' => 'Infant (0 - 1)',
      'child' => 'Child (1 - 4)',
    return array(
      11 => array( // assume 11 = product baby cream
    Then in your code:


    class ShoppingCartEvaluatorImpl implements ShoppingCartEvaluator {
      public function __construct(array $mappings) {
        $this->mappings = $mappings;
      public function evaluateShoppingCart(ShoppingCart $cart) {
        // ..
    // in your shopping cart
    $evaluator = new ShoppingCartEvaluatorImpl(require '/path/to/product-mappings.php');
  9. OT: It's less work and more precise to simply link your products to possible advertising directly. Instead of using pseudo-meta information that has several conceptual flaws. Like requinix pointed out that baby cream could be linked to either a mother between the age of 16 and 35 and an old man of 60+. This couldn't be more far apart.


    I understand this is homework and you won't have any say in the matter but nonetheless. Requinix already gave you the solution though I wouldn't generalize. So instead of simply stating Woman as a profile. I would add 'Mother (16 - 35)', 'Elderly man (60+)' etc..


    Then for each new product that gets added like diapers for example will only contain 'Mother (16 - 35)' and do a array_intersect. So that 'Elderly man (60+)' gets excluded. As where Cane for example would exclude Mother.


    I wouldn't simply drop the excluded tags but keep them in a separate array as a second source of meta information (hard versus soft tags).


    For baby cream and diapers:

    Hard tags: Mother (16 - 35), Baby (0 - 4)

    Soft tags: Elderly man (60+)

  10. Your application should only be database aware, not be the database. Define your database with proper referential integrity, events, views, and procedures as the database is optimised for this type of task.


    Your application should only know it's interface like every other REST API out there. If you use a (powerful) ORM you'll be able to translate these procedures and views to entities using a ResultSetMapper. Just like you would map an entity from a request when interfacing with a REST API.


    Now that you know this on to your question. ON DELETE [CASCADE|RESTRICT|SET NULL] depends on how the relations between your rows exists:



    You use this when it makes no sense to keep the child row if the parent row is removed. For example an OrderLine and an Order. If you don't have the Order, then having the OrderLine's makes no sense.



    Like was already explained it simply prohibits you to delete a parent if it has childs. Just like you can't delete a directory if it contains files. You need to move or delete the dependent rows first in order to remove the parent. This is the default behaviour and you'll get a "Cannot add or update a child row: a foreign key contraint fails" if you do.



    This is useful for optional relationships. For example between a Customer and a Cart. If the Customer currently has a Cart the field contains a FK to the Cart.Id. If the Customer on the other hand does not have a Cart this is simply NULL and a Cart can be assigned. The Customer can exist regardless if he has a Cart or not.


    In order to impose theses restrictions you need to know how the relationships are between your data.

  11. I think most people simply misunderstood MVC because it's so dead simple. Only the View and Model part are significant, the Controller is nothing more but glue between them. In my career I never met anyone who understood MVC (and I simply assumed I understood by assuming that if their controllers had more code then their models, they were wrong).


    Even in the academic sphere when I was taught Java and .NET, their controllers were littered with model logic and their models mere data placeholders. One of those teachers even went as far as simply making every class static and just call them anywhere, everywhere. At that point I knew that self-taught was the way to go.


    And not all frameworks break the rules (I think none of them do). The one I show above is Symfony. Of course anyone can abuse a framework and put their model logic in their controllers. But I get why you think that, because a framework caters to all. And when you create something very simple there's no need to be all architect about it.

  12. Should validation be handled by your controller? No, because depending on the number of systems there may be more then 1 controller accepting the same data. Is validation part of the model? Yes. Don't get caught up on details.


    - View is what shows the response to a request of the user.

    - Controller is what accepts requests and acts upon them.

    - Model is everything else.


    That said, you should keep in mind to keep your controller code to a minimum. Because too much code in the controller means that you'll be copy-pasting a lot between controllers. Encapsulate (that means put it in a class) if you write something more then 2 times.


    You'll probably have come across services (if you are into SOA, (pun side note: in Belgium this is short for STD)). These services are a Facade to the model and mostly accept scalars (think productId).


    class UserController {
      public function registerAction(Request $request) {
        $service = $this->get('service.registration');
        $form = $this->createForm('signup_form'); // another service
        if ($form->isValid()) {
        return $this->render('register.html'); // return Response object
    Suppose that at some time you switch to AngularJS and use a REST back-end your code won't look much different:


    class RESTUserController {
      public function postUserAction(Request $request) {
        $service = $this->get('service.registration');
        $form = $this->createForm('signup_form');
        if ($form->isValid()) {
        return $this->redirect('users_list');
    I say the $form is a service here, even though conceptually it's a client thing, it models how a client enters information into the system. It validates and turns the scalar input of the client into a model your system can work with.
  13. You don't need to worry about someone copying your code. We all suffer from Not Invented Here syndrome, so rest assured. And I am the guy who constanly keep telling everyone they SHOULD use other people's code (if available, well-tested, and adheres to some standard).


    Like minded people created an entire tool for plagiarists to do plagiarism and then sell it off as their own:

    $ composer require your/code
  14. So you want to school us on security. Let me school you how this works:


    1. This website is owned by this guy and hasn't been around like forever and we have basically zero access to the server.

    2. We are volunteers and do not get paid (that means people like you or those that cook soup for the homeless)

    3. This is a forum for which the license costs 175$ which we need to pay for, while all profits of the ads go directly to no-show.

    4. Be grateful you piece of sh*t instead of trying to school professionals that have lives just like you and donate their time and money to help people like you ALL FREE OF CHARGE!!!


    You are that homeless guy that complains when he finds a fly in his soup!

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.