...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
June 29, 2017

$list = $tab_one['special_days']; // import list
$list = explode(PHP_EOL, $list); // split after new line

// At this point I will assume that you have a line like
// '06/28 > 5:00-23:00' in each list item 

$finalArray = []; // To store the array

foreach($list as $item) {
    $item = explode('>', $item); // split after ">"
    $finalArray[$item[0]] = explode(',', $item[1]);
}

var_dump($finalArray);

Note that the items exploded from $item[1] have not been trimmed so might have whitespaces.

Read more
June 28, 2017

Auto version your JavaScript or Stylesheet files using .htaccess

Usually, developers can maintain the cache they maintain themselves at the backend but cannot control the cache held by the browser as freely as they want unless there is proper version control. Here is a quick drop in solution in `.htaccess` file until you have proper versioning.

Most of the applications have one or many layers of cache for their system. The cache that application developer can maintain is at the backend (the server) level. However, you cannot control the cache held by the browser as freely as you want unless you version the files as well.

Versioning the files is definitely the best way to solve this problem because it is a very efficient solution which guarantees that browser will fetch the new resource when you want it to. But there are cases where this is not an easy step to be taken, for example, in a legacy app which resources are included in line the page.

So, is there a quick way to fix the problem until you go to proper versioning?

Yes! there is.

Using .htaccess we can auto version the files and force the browser to fetch files down to every second if we need to.

Here is how:

We can use server variables such as TIME_YEAR, TIME_MONTH to create an automatic version of your resource. These are variables that the web server provide to be used where suitable. And now, let’s see how to do this.

RewriteCond %{QUERY_STRING} !(v=(.<em>))
Rewriterule ^(js|scripts|css|styles)(.</em>)$ /$1$2?v=%{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} [r=302,nc]`

Open your .htaccess files and paste those two lines in. What these are doing is:

  • If a request comes to the server that starts with js, scripts, css or styles then rewrite the request by appending an auto-created version at the end.
  • IF a request comes with the version already in the request then don’t do anything because we don’t want it to keep rewriting the request.

Simple as that. So for example: if the request comes to https://abc.com/js/main.js it gets served as https://abc.com/js/main.js?v=2017062811. Same goes for any request coming to other paths as well. This example ensures that browser will fetch the resource again every hour. But if you add variables like TIME_MINUTE or TIME_SECOND or TIME browser will keep fetching the content more frequently.

To see what other server variables can be used visit https://httpd.apache.org/docs/trunk/expr.html#vars

Read more
June 13, 2017

enum is a reserved keyword so you should not use such reserved keywords.

Find a working example of your code below:

var t_enum = {
    aitem: {
        Desc: 'A description',
        Value: 0,
        Group: 'A'
    },
    bitem: {
        Desc: 'b description',
        Value: 1,
        Group: 'B'
    },
    bitem: {
        Desc: 'c description',
        Value: 2,
        Group: 'C'
    }
}
var neededGroup = 'A';
var neededValues = [];

Object.keys(t_enum).forEach(function (x) {
    var item = t_enum[x];
    if (item.Group == neededGroup){
        neededValues.push(item.Value);
        return;
    }
})
console.log(neededValues);

Demo

Its an alternate approach from what Toddo is suggesting.

Read more
June 6, 2017

You cannot change the content the label itself, but you can control the content of the pseudo element after and before. You can add the logic of square/circle with something that can represent check and uncheck.

input[type='checkbox'] {
  width: 0;
  height: 0;
}

input[type='checkbox']:checked + label:after {
  content: "checked";
}

Demo

To answer your question in the comment

How they make funky circle buttons and put a checkbox inside it when one clicks?

You can use CSS animations. The label will already have the check symbol in it but won’t have shown in the unchecked state of the input box. And when the element is checked, it will change the opacity to 1, showing the checkbox in an animated way.

input[type='checkbox'] {
  width: 0;
  height: 0;
}

input[type='checkbox'] + label:after {
  content: "checked";
  opacity: 0;
  -webkit-transition: all 0.50s; 
  -moz-transition: all 0.50s;
  -o-transition: all 0.50s;
  transition: all 0.50s;
}

input[type='checkbox']:checked + label:after {
  opacity: 1;
}

Demo

Read more
March 22, 2017

Why doesn’t `knife upload roles` upload any roles?

Josh Gagnon’s Question:

I followed the OpsCode quickstart guide and things went fairly smoothly, but now I’m running into issues experimenting with roles. I have a “starter” role in my repo that was provided by OpsCode. When I follow the first official documentation I could find and run the following from the top of my chef repo:

knife upload roles

it simply returns and nothing happens. I get no role (new or updated) on my chef server. The following line does work for creation and updating:

knife role from file .rolesstarter.rb

but I find it overly cumbersome and I would prefer something that just pushes my entire repo up to the server (or at least all of the roles) to make me more confident that everything is up to date.

Yeah, in this case Chef is a little bit inconsistent. I use the next 3 lines to push by whole repo to chef-server (of course after the tests have passed):

knife cookbook upload --all
knife role from file roles/*.rb
knife data bag from file --all

Suggested solution from Draco, didn’t work for me to upload the data_bags. Here is what I did.

knife cookbook upload --all
knife role from file roles/*.rb
knife upload data_bags/

Read more
March 4, 2017

MAX in PHP returning wrong value

Krish’s Question:

I have an array with key and value pair. I’m building this array dynamically and below is the code.

$x[] = array('type_name' => $value->name,
            'percentage'=> intval($percentage));

My intention is to get the maximum value and for that I do

max($x);

However it is returning the wrong value actually the lowest value. Following is my array. Any help would be awesome.

Thanks is advance.

You need to read how the max compares against different types of data. In your case, you are trying to compare against one of the array item i.e. percentage inside one of the item so the function max does not know to do this.

There is an example by Revo in the manual which shows you how to do this.

Read more
October 24, 2016

MySQL about constraint

J.Doe’s Question:

Good afternoon,

Can anyone tell me what’s wrong with my code on PHP MY ADMIN, I’m trying to write a CONSTRAINT and create values for the car color in the beginning ( with table creation)

CREATE TABLE T_CAR
(CAR_ID                INTEGER       NOT NULL PRIMARY KEY,
 CAR_MARK            CHAR(32)      NOT NULL,
 CAR_MODEL            VARCHAR(16),
 CAR_NUMBER   CHAR(10)      NOT NULL,
 CAR_COLOR           CHAR(16)      CHECK (VALUE IN ('white', 'black', 'red', 'green', 'blue')))

The problem is with the last line (error message syntax not known).
Thanks in advance.

MySQL ignores check expression.

Manual: Create Table

The CHECK clause is parsed but ignored by all storage engines.

Try Enum:

CREATE TABLE T_CAR (
    CAR_ID INTEGER NOT NULL PRIMARY KEY,
    CAR_MARK CHAR(32) NOT NULL,
    CAR_MODEL VARCHAR(16),
    CAR_NUMBER CHAR(10) NOT NULL,
    CAR_COLOR ENUM('white', 'black', 'red', 'green', 'blue') NOT NULL
)
Read more
October 20, 2016

How can I let a table cell take in the full width of the table with css?

Jim Peeters’s Question:

For some reason the cells in my second row in my table are changing the width of the cells in the row above. I have no idea why this is the cause. I don’t want the width of the first cell in the first row to be changed. I have reproduced the problem in jsfiddle to make it clear what I mean.

FiddleJS link:

https://jsfiddle.net/bpyrgsvc/1/

HTML:

<div class="table">
  <div class="row">
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
  </div>
  <div class="row">
    <div class="cell">this changes the width of the cell above</div>
  </div>
</div>

CSS:

.table {
  display:table;
}

.row {
  display: table-row;
}

.cell {
  display: table-cell;
  padding: 5px;
  border: 1px solid black;
}

I don’t see anything wrong with the results. In a div set to be displayed as table and table-row, it is behaving as tables.

To get the result you want, close the first table and start another.

<div class="table">
  <div class="row">
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
    <div class="cell">test</div>
  </div>
</div>
<div class="table">
  <div class="row">
    <div class="cell cell-full">this changes the width of the cell above</div>
  </div>
</div>

https://jsfiddle.net/bpyrgsvc/4/

Read more
October 17, 2016

Change width of child div element using JQuery

Bodzilla’s Question:

I have an element in my html with this markup:

<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" role="tabpanel" style="width: 98px; display: block;">

I would like to know how to remove the width attribute using jQuery

You can set the width to be auto

$("selector").css('width', 'auto);
Read more
...

Please fill the form - I will response as fast as I can!