안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2018년 2월 21일 수요일

PHP mssql nText 사용방법

Text로 cast 해서 사용하면 된다.

이걸 몰라서 몇시간을 헤매었다...

  • SELECT CAST(field1 AS TEXT) AS field1 FROM table

2018년 2월 13일 화요일

PHP 우측 검색 (마지막으로 나오는 문자 검색)

strrchr

(PHP 4, PHP 5, PHP 7)
strrchr — Find the last occurrence of a character in a string

Description ¶

string strrchr ( string $haystack , mixed $needle )
This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.

출처: http://php.net/manual/en/function.strrchr.php

PHP to JSON(PHP class를 JSON으로 변경하는 방법)

PHP to JSON- How to serialize PHP class to JSON format?

If you are using PHP to implement REST API, you will come across this question. The answer to this, in general, is really simple.
PHP comes with two functions json_encode and json_decode, for encoding a PHP object into JSON and JSON string into PHP object respectively.
Check out the manual at Php.Net for the full details of this function.
The call is simple $j = json_encode($v);
$j contains the JSON for $v which can be an array, object or scalar variable.
However, here are a few limitations to json_encode.
  1. If you have strings stored in the object your encoding, they have to be UTF-8 strings or you need to convert them to UTF-8 using utf8_encode before calling json_encode
  2. If you have member variables that are marked as private, json_encode can’t see them.
  3. Associative arrays will be represented as objects. Sequential arrays will be represented as arrays in JSON. You can force them to be represented as objects by either casting the variable to object or pass in the option JSON_FORCEOBJECT
Being able to expose private and protected variables is very important. This can be done in various ways. You can use an Iterator like an ArrayIterator and call json_encode on that. The solution I am going to outline involves writing your own custom encode function that uses json_encode.
As an example you take the following classes in PHP. JsonTest contains JSon_Test1 which in turn contains JSonTest_2.
class Marshall
{
public function propvalues()
{
return get_object_vars($this);
}
}
class JSonTest_2 extends Marshall
{
public $arr2;
public function __construct()
{
$this->arr2 = array(1 => “stest”, 4 =>”stest2″, 3=>5.1);
}
}
class JSonTest_1 extends Marshall
{
public $arr1;
public $j2;
private $jp2;
public function __construct()
{
$this->arr1 = array(“test”, “test2”);
$this->j2 = new JSonTest_2();
$this->jp2 = “jp2”;
}
public function propvalues()
{
return array(“arr1” =>$this->arr1, “j2″=>$this->j2->propvalues(), “jp2″=>$this->jp2);
}
}
class JsonTest extends Marshall
{
private $arrActuary = array();
public $jt1;
private $jt11;
private $arrTestM = array();
public function __construct()
{
$this->jt1 = new JSonTest_1();
$this->jt11 = new JSonTest_1();
$this->arrTestM = array(1 =>”value”, “test” =>100.21);
}
public function propvalues()
{
return array(“jt1″=>$this->jt1->propvalues(), “jt11″=>$this->jt11->propvalues(), “arrTestM”=>$this->arrTestM);
}
}
class JSon
{
static public function encode($o)
{
if(is_a($o, “Marshall”))
return json_encode($o->propvalues());
return json_encode($o);
}
}
The following code will call normal json_encode passing in $j and then call the special encode on $j.
$j = new JsonTest();
echo “normal encode”;
echo json_encode($j);
echo “special encode”;
echo JSon::encode($j);
This will echo the following two lines: (You can pretty print these to make them more readable etc.)
normal encode
{“jt1”:{“arr1”:[“test”,”test2″],”j2″:{“arr2”:{“1″:”stest”,”4″:”stest2″,”3″:5.1}}}}
special encode
{“jt1”:{“arr1”:[“test”,”test2″],”j2″:{“arr2”:{“1″:”stest”,”4″:”stest2″,”3″:5.1}},”jp2″:”jp2″},”jt11″:{“arr1”:[“test”,”test2″],”j2″:{“arr2”:{“1″:”stest”,”4″:”stest2″,”3″:5.1}},”jp2″:”jp2″},”arrTestM”:{“1″:”value”,”test”:100.21}}
I define a class called Marshall which when inherited automatically exposes all the public variables as an array. If you override the function propvalues in the inherited class as is done in the class JSonTest_1, you will be able to iterate over the private and protected variables too.
I also defined a helper class JSon with static function encode which calls json_encode on the object if it does not inherit from Marshall otherwise it will call propvalues function and json_encode the return value.
Here we make use of two properties of json_encode to achieve this. First one is the fact that json_encode recursively encodes the objects. Second one is that associative arrays are encoded as objects. The second property is the one that’s used in propvalues where we are constructing the associative array with the private, protected and public variables.
Note that propvalues itself can check if the contained class in this case JSonTest_2 is a Marshall class and call propvalues on it.
The code above has an advantage over normal json_encode function in that you can transform the class to be different from what it is in PHP when you serialize in JSON.. can be selective about which member variables in the class you want to add to JSON. You can also decide what to name them.
(Note – json_encode is available in 5.2 version or later and get_object_vars in PHP version 4.0 or later)

출처: https://mofintodev.wordpress.com/2013/07/09/php-to-json-how-to-serialize-php-class-to-json-format/

2018년 2월 12일 월요일

[PHP] Debug 방법

error_reporting(E_ALL); ini_set("display_errors", 1);
를 맨위에 붙이면 왜 에러가 났는지 Warning이 떴는지 출력이 된다.

PHP Debug에 꼭 필요하다.

2018년 2월 8일 목요일

인코딩 디코딩

인코딩encodeURI - 자바스크립트의 인코딩 함수로 일부 특수문자는 변환하지 않는다. 주로 URL을 인코딩 할 때 사용
encodeURIComponent - 자바스크립트의 인코딩 함수로 특수문자를 포함해서 다 인코딩 한다.
rawurlencode - PHP의 인코딩 함수로 특수문자 포함해서 다 인코딩 합니다.
 디코딩decodeURI - encodeURI의 반대
decodeURIComponent - encodeURIComponent의 반대
rawurldecode - rawurlencode의 반대 
위의 함수들은 서로서로가 짝을 이루고 있지만, 신경쓰지 말고 서로 혼합해서 써도 인코딩 디코딩이 잘 이루어집니다.
encodeURI로 인코딩 하고 rawurldecode로 디코딩 할 수 있다는 말이죠~


출처: http://sjpison.tistory.com/95 [비손의 블로그]

[angularjs] 컨트롤러 간에 데이터 공유(Sharing data between controllers)

출처: https://benohead.com/angularjs-sharing-data-controllers/
한글 번역본
너는 너의 것들을 decoupled 상태로 유지하려고 해야 하고 너의 directives, controllers 와 services들은 self contained(자체적으로 담겨있어야함) 에도 불구하고, 너는 controller들 사이에 data를 공유해야 될 때가 있다.
두 가지 메인 시나리오가 있다.
1. Parent와 child controller 간에 데이터 공유
2. two siblings 와 같은 연결성이 없는 controller간에 데이터 공유
Parent와 child간에 데이터 공유
아래와 같이 parent, child controller를 가지고 있다고 해보자.
<div ng-controller="ParentCtrl">
  <div ng-controller="ChildCtrl as vm">
  </div>
</div>
js에서는 아래처럼 정의 될 것이다.
var app = angular.module('sharing', []);

app.controller('ParentCtrl', function($scope) {
});

app.controller('ChildCtrl', function($scope) {
});
parent controller에 user name을 정의한다.
app.controller('ParentCtrl', function($scope) {
  $scope.user = {
    name: "Henri"
  };
});
Note that you shouldn’t define your variable as a primitive in the scope since this will cause shadow property to appear in the child scope hiding the original property on the parent scope (caused by the JavaScript prototype inheritance). So you should define an object in the parent scope and defined the shared variable as a property of this object.
There are now three ways to access this shared variable:
  1. using $parent in HTML code
  2. using $parent in child controller
  3. using controller inheritance

Using $parent in HTML code

You can directly access variables in the parent scope by using $parent:
Hello {{$parent.user.name}}

Using $parent in child controller

Of course, you could also reference the shared variable using $parent in the controller and expose it in the scope of the child controller:
app.controller('ChildCtrl', function($scope) {
  $scope.parentUser = $scope.$parent.user;
});
Then, you can use this scope variable in your HTML code:
Hello {{parentUser.name}}

Using controller inheritance

But since the scope of a child controller inherits from the scope of the parent controller, the easiest way to access the shared variable is actually not to do anything. All you need to do is:
Hello {{user.name}}


연관성이 없는 controller 간에 데이터 공유

parent-child 관계가 아닌 2 controller 간에 데이터를 공유하기 위해서는 $parent를 사용할 수도 없고 prototype inheritance를 사용하지도 못한다.
그런 경우 다음 세가지를 사용할 수 있다.
  1. Factory 나 Service에 공유 데이터를 넣는다.
  2. Root Scope에 공유 데이터를 넣는다.
  3. data 변경에 대해 다른 controller에게 알려 준다.

Factory 나 Service에 공유 데이터를 넣는 법

AngularJS facories(and services)는 methods(business logic) 과 properties(data)를 둘 다 가지고 있을 수 있고 다른 components(예: 너의 controller들) 에 inject 할 수 있다.
이것이 factory안에 공유 variable을 정의할 수 있도록 해주고, 양쪽 contoller에 이것을 inject 하면 이 factory data는 양쪽 controller 안에 scope variable로 bind 된다.
The first step is to define a factory holding a shared value:
app.factory('Holder', function() {
  return {
    value: 0
  };
});
Then you inject this factory in your two controllers:
app.controller('ChildCtrl', function($scope, Holder) {
  $scope.Holder = Holder;
  $scope.increment = function() {
    $scope.Holder.value++;
  };
});

app.controller('ChildCtrl2', function($scope, Holder) {
  $scope.Holder = Holder;
  $scope.increment = function() {
    $scope.Holder.value++;
  };
});
In both controllers, we bind the Holder factory to a scope variable and define a function which can be called from the UI and updates the vale of the shared variable:
<div>
  <h2>First controller</h2>
  <button>+</button>{{Holder.value}}
</div>
<div>
  <h2>Second controller</h2>
  <button>+</button>{{Holder.value}}
</div>
No matter which “+” button you press, both values will be incremented (or rather the shared value will be incremented and reflected in both scopes).

Holding the shared data in the root scope

Of course, instead of using a factory or a service, you can also directly hold the shared data in the root scope and reference it from any controller. Although this actually works fine, it has a few disadvantages:
  1. Whatever is present in the root scope is inherited by all scopes
  2. You need to use some naming conventions to prevent multiple modules or libraries from overwriting each other’s data
In general, it’s much cleaner to encapsulate the shared data in dedicated factories or services which are injected in the components which require access to this share data than making these data global variables in the root scope.

event를 사용해서 data 변경을 다른 controller에게 알리는 방법

factory data를 통해 양쪽 Scope에 bind 하지 않기를 원하는 경우 (예: one scope의 변경사항을 다른 scope에게 알려주기만을 원하는 경우), controller 간의 event notification을 사용해서 data를 sync할 수 있다. event를 handle하기 위해 angularjs 에서는 3가지 방법을 제공한다:
  • $emit은 event를 발생시키고 현재 scope 과 모든 parent scope에 전달한다.
  • $broadcast는 event를 발생시키고 현재 scope과 모든 child scope에 전달한다.
  • $on 은 the scope의 변경사항을 listen 하는데 이용한다.
Using $emit
Since $emit 은 event를 scope 계층 상단으로 전달하기에, 두가지 사용법이 있다.
  • parent controller들에게 event를 전달
  • root scope을 통해 연관 되지 않은 controller에 효율적으로 event를 전달              
첫번 째 시나리오, child scope에서 emit을 사용:
$scope.$emit("namechanged", $scope.name);
parent controller scope에서 listen:
$scope.$on("namechanged", function(event, name) {
  $scope.name = name;
});
두번째 시나리오, rootscope에서 emit:
$rootScope.$emit("namechanged", $scope.name);
그리고 rootscope에서 listen:
$rootScope.$on("namechanged", function(event, name) {
  $scope.name = name;
});
In this case there is effectively no further propagation of the event since the root scope has no parent scope. It is thus the preferred way to propagate events to unrelated scopes (and should be preferred to $broadcast in such scenarios).
There is one thing you need to consider when registering to events on the root scope: in order to avoid leaks when controllers are created and destroyed multiple times, you need to unregister the event listeners. A function to unregistered is returned by $emit. You just need to register this function as a handler for the $destroy event in your controller, replacing the code above by:
var destroyHandler = $rootScope.$on("namechanged", function(event, name) {
  $scope.name = name;
});

$scope.$on('$destroy', destroyHandler);
Using $broadcast
Theoretically, you could also use $broadcast to cover two scenarios:
  • Propagating events to child controllers
  • Propagating events to unrelated controllers through the root scope
Effectively, the second use case doesn’t make much sense since you would basically trigger an event on the root scope and propagate it to all child scopes which is much less efficient than propagating and listening to events on the root scope only.
In the first scenario, you broadcast an event on the parent controller scope:
$scope.$broadcast("namechanged", $scope.name);
And listen to this event on the child controller scope:
$scope.$on("namechanged", function(event, name) {
  $scope.name = name;
});
Similarities and differences between $emit and $broadcast
Both $emit and $broadcast dispatch an event through the scope hierarchy notifying the registered listeners. In both cases, the event life cycle starts at the scope on which the function was called. Both functions will pass all exceptions thrown by the listeners to $exceptionHandler.
An obvious difference is that $emit propagates upwards and $broadcast downwards (in the scope hierarchy). Another difference is that when you use $emit, the event will stop propagating if one of the listeners cancels it while the event cannot be canceled when propagated with $broadcast.

Demo

You can see the code from this post in action on plunker: