Skip to main content

PHP/MySQL - store default text in database or in a file?



OK so I am creating a system where whenever a shop signs up, they get 10 default slides that they can then customize later. The default slide data is just a starting point for them. But I have to assume a lot of shops will not log in and update the information, they just want something up ASAP.





So say for the Dashboard slide, every shop starts out with the same heading and text until they go in and update it themselves. Do I insert it in the database, like so:







id | slide_id | user_id | headline | text

1 | 1 | 1000 | ... | ....

2 | 1 | 1001 | ... | ....







So the database will contain many duplicates of that headline and text since it is the default. Is that a good idea?





Or do I do something like this:







define("DEFAULT_TEXT", "The text goes here and just used once in a file.");

$text = ($stmt->num_rows < 1 ? DEFAULT_TEXT : $text);







so I have a query that searches through my slide_data table to see if anything exists for that slide id, and if it does, then just use that headline and text.. but if there are no rows, which means they have not added anything yet, then just use the constant DEFAULT_TEXT . And that way, I can have different language config files and have translations in there, so if the language is French then it will use the DEFAULT_TEXT_FR constant which will have the manual translation.








Which one do you recommend? I'm not sure if I have the right idea here, so looking for some other opinions and suggestions.





Thanks!


Comments

  1. I would define the default text outside of the database, storing it in the db creates unecessary duplicate entries which will ultimately effect performance over time (the larger a db table gets the longer it takes to quuery that table). Once you have it defined just run a query by user id to see if they created text for that slide, otherwise default to your predefined text

    Edit:

    $result = mysql_query(your check to see if slide exists);
    while ($row= mysql_fetch_assoc($result)) {
    $displayText = $row['text'];
    };

    if ($displayText != '') {
    $sql = 'update command';
    } else {
    $displayText = DEFAULT_TEXT;
    $sql = 'insert command';
    }

    mysql_query($sql);

    ReplyDelete
  2. The way you are doing it is not necessarily incorrect, but as you hint, you may end up with a large amount of duplicate data.

    You may want to consider wrapping these into an object such as ShopSlides which holds default values and configured values by Shop.

    This could in a very quick form be something like this:

    <?php
    class ShopSlides{

    protected $slides = array();
    protected $defaults = array(
    '1' => 'Headline',
    '2'=> 'Healine 2'
    ...
    );
    public function __construct($slides){
    $this->slides = $slides;
    }

    public function getSlide($id){
    if(array_key_exists($id,$this->slides)){
    return $this->slides[$id]);
    } elseif(array_key_exists($id,$this->defaults)){
    return $this->defaults[$id]);
    }
    return false;
    }
    }

    ReplyDelete
  3. I'd prefer the constants solution because you can avoid data redundance and you can easily customize default messages without the need to update a lot of records...

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.