728x90

function content_print(){
     
                var initBody = document.body.innerHTML;
                window.onbeforeprint = function(){
                    document.body.innerHTML = document.getElementById('선택될 div id').innerHTML;
                }
                window.onafterprint = function(){
                    document.body.innerHTML = initBody;
                }
                window.print();    
            }           
         
 
===============================================================================================
 
<div id="content">
    내용 content_1
</div>
 
<div id="content_2">
   내용 content_2
</div>
<input type="button" value="print" onclick="javascript:content_print();">
================================================================================================
 
선택될 div id  이부분에 div id 를 삽입하면 된다.
print 버튼을 누르면 지정된 div 안의 내용이 프린트 된다.



출처: http://mkwilson.tistory.com/64 [wilson's story]

출처: http://mkwilson.tistory.com/64 [wilson's story]

출처: http://mkwilson.tistory.com/64 [wilson's story]

출처: http://mkwilson.tistory.com/64 [wilson's story]

728x90


I am trying to return a json encoded string from PHP script.

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
});

I tried to use alert function in callback to see returning value from PHP:

{"price":249,"discount":"0.00","total":249}

but value of #price and rest elements is "$undefined".

Please help.

It seems you just have to parse the JSON data into an object using parseJSON() :

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    data = $.parseJSON( data );
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
});


728x90

Have you tried running latest Apache 2.4 with PHP 7.0 on CentOS 6? Last time i ran into trouble when a customer asked me to update Apache from 2.2.X to 2.4.X running on CentOS 6 with addition of keeping the PHP operational. I’ve checked third party repositories for Apache 2.4 RPMs so i would not have to bother with building it myself and found it quickly. I’ve also found different PHP versions in the same repositories and thought to myself “there, there, i’m almost done!”.

But things got complicated and none of the PHP versions available would run with Apache 2.4.X i’ve installed. Been searching the web for over an hour, testing it out without success and that’s why i’ve decided to write a post on Running latest Apache 2.4 with PHP 7.0 on CentOS 6 to help my readers get by this faster than me 🙂

Procedure: First we need to build latest Apache 2.4 RPMs, once that done we can install Apache 2.4 and last but not least we can compile PHP 7.0 and make it work with our Apache instance.
Environment: I’m working on up2date fresh CentOS release 6.7 (Final) minimal installation with SELinux and IPTables enabled and running.

Running latest Apache 2.4 with PHP 7.0 on CentOS 6

1. Download and Install RPMForge Repository

RPMForge repository is required to download and install all of the dependencies we need to build Apache 2.4 RPMs.

[root@localhost ~]# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

2. Satisfy Software Dependencies

We need to install additional software to satisfy dependencies and development libraries while building Apache 2.4 RPMs and compiling PHP.

[root@localhost ~]# yum install wget rpm-build autoconf zlib-devel libselinux-devel libuuid-devel pcre-devel openldap-devel lua-devel libxml2-devel openssl-devel postgresql-devel mysql-devel sqlite-devel freetds-devel unixODBC-devel nss-devel expat-devel db4-devel mailcap libtool doxygen bzip2-devel libcurl-devel libjpeg-turbo-devel libpng-devel libXpm-devel freetype-devel gmp-devel libicu-devel libmcpp-devel gcc-c++ libmcrypt-devel aspell-devel readline-devel libtidy-devel libxslt-devel

3. Download and Build distcache RPM

Distcache package is also a requirement and  it’s quite hard to find so to make it easier you can download it from GeekPeek.Net.

[root@localhost ~]# wget https://geekpeek.net/wp-content/uploads/2016/04/distcache-1.4.5-23.src.rpm

Now build RPMs and install distcache and distcache-devel. Note that the newly build RPMs are located in different folder.

[root@localhost ~]# rpmbuild --rebuild distcache-1.4.5-23.src.rpm
[root@localhost ~]# rpm -ivh /root/rpmbuild/RPMS/x86_64/distcache-1.4.5-23.x86_64.rpm /root/rpmbuild/RPMS/x86_64/distcache-devel-1.4.5-23.x86_64.rpm

4. Download Latest Apache 2.4.X and Apr Sources

Current latest available version of Apache is 2.4.20.

[root@localhost ~]# mkdir apache24
[root@localhost ~]# cd apache24/
[root@localhost apache24]# wget http://www.apache.si/httpd/httpd-2.4.20.tar.bz2

We also need to download Apache Portable Runtime and Utility sources.

[root@localhost apache24]# wget http://www.apache.si/apr/apr-1.5.2.tar.bz2
[root@localhost apache24]# wget http://www.apache.si/apr/apr-util-1.5.4.tar.bz2

5. Build and Install Apache Portable Runtime and Utils RPMs

Firstly we need to build APR RPM and install apr and apr-devel packages

[root@localhost apache24]# rpmbuild -tb apr-1.5.2.tar.bz2
[root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/apr-1.5.2-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-devel-1.5.2-1.x86_64.rpm

Now we can proceed to build APR UTIL RPM and install apr-util, apr-util-devel and apr-util-ldap.

[root@localhost apache24]# rpmbuild -tb apr-util-1.5.4.tar.bz2
[root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/apr-util-1.5.4-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-util-devel-1.5.4-1.x86_64.rpm /root/rpmbuild/RPMS/x86_64/apr-util-ldap-1.5.4-1.x86_64.rpm

6. Build and Install Apache 2.4 RPMs

We can now proceed and build Apache 2.4.20 RPMs and afterwards install Apache.

[root@localhost apache24]# rpmbuild -tb httpd-2.4.20.tar.bz2

The process built quite a number of Apache RPMs:

Wrote: /root/rpmbuild/RPMS/x86_64/httpd-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/httpd-devel-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/httpd-manual-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/httpd-tools-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/mod_authnz_ldap-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/mod_lua-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/mod_proxy_html-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/mod_socache_dc-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/mod_ssl-2.4.20-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/httpd-debuginfo-2.4.20-1.x86_64.rpm

I will install all of them, you can choose just the ones you need.

[root@localhost apache24]# rpm -ivh /root/rpmbuild/RPMS/x86_64/httpd-* /root/rpmbuild/RPMS/x86_64/mod_*

7. Start Apache 2.4.20

Firstly allow access to this machine on port 80 TCP by adding the following line to /etc/sysconfig/iptables and reloading iptables service:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Now we can start Apache for the first time.

[root@localhost apache24]# /etc/init.d/httpd start
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
 [ OK ]

We can see that Apache start produced a warning since “ServerName” directive is not set, but Apache 2.4.20 started up successfully and is running.

[root@localhost apache24]# /etc/init.d/httpd status
httpd (pid 14317) is running...

We are one step closer to running the latest Apache 2.4 with PHP 7.0 on CentOS 6 🙂 ..continuing with PHP 7.0 installation.

8. Download and Unpack PHP 7

Download the latest version of PHP 7 and unpack it.

[root@localhost apache24]# cd ..
[root@localhost ~]# mkdir php7
[root@localhost ~]# cd php7
[root@localhost php7]# wget -O php-7.0.5.tar.gz http://de1.php.net/get/php-7.0.5.tar.gz/from/this/mirror
[root@localhost php7]# tar -xvzf php-7.0.5.tar.gz

9. Copy php.ini, Configure, Make and Install PHP

Move to the unpacked PHP directory, copy php.ini-production to /usr/local/lib directory and run configure on extracted php.

[root@localhost php7]# cd php-7.0.5
[root@localhost php-7.0.5]# cp php.ini-production /usr/local/lib/php.ini
[root@localhost php-7.0.5]# ./configure --with-libdir=lib64 --prefix=/usr/local --with-layout=PHP --with-pear --with-apxs2 --enable-calendar --enable-bcmath --with-gmp --enable-exif --with-mcrypt --with-mhash --with-zlib --with-bz2 --enable-zip --enable-ftp --enable-mbstring --with-iconv --enable-intl --with-icu-dir=/usr --with-gettext --with-pspell --enable-sockets --with-openssl --with-curl --with-gd --enable-gd-native-ttf --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-freetype-dir=/usr --with-libxml-dir=/usr --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-soap --with-xmlrpc --with-xsl --with-tidy=/usr --with-readline --enable-pcntl --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-shmop --enable-maintainer-zts

Once configure is done “make test” (not obligatory) and “make install”. Note that both these actions might take a while…

[root@localhost php7]# make test
[root@localhost php7]# make install

10. AddHandler for .php Files

We need to instruct PHP to handle files with .php extension and pass them to PHP interpreter. We can achieve this by adding two lines to global Apache config file with the commands below:

[root@localhost php-7.0.5]# echo "AddHandler php7-script .php" >> /etc/httpd/conf/httpd.conf
[root@localhost php-7.0.5]# echo "AddType text/html .php" >> /etc/httpd/conf/httpd.conf

11. Create PHP Info File

Create a new file in /var/www/html directory called info.php and add the following contents in it:

<?php
phpinfo();
?>

12. Restart Apache

We need to restart or reload Apache for configuration changes to become active.

[root@localhost php-7.0.5]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
 [ OK ]

13. Browse PHP Info File

To confirm everything works as expected open up the following URL in your browser: http://ip_address/info.php

Hopefully your server is in the same network and you will be able to access it. Change the ip_address with IP address of your server and PHP info page should open up holding all of the PHP information:

Running latest Apache 2.4 with PHP 7.0 on CentOS 6
Running latest Apache 2.4 with PHP 7.0 on CentOS 6

 

728x90

I have the following php. However when I see the index.php I get the following error message.

Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12

I am hoping someone can tell me how to fix the problem.

Thanks in advance.

index.php

// { common variables and functions
include_once('ww.incs/common.php');
$page=isset($_REQUEST['page'])?$_REQUEST['page']:'';
$id=isset($_REQUEST['id'])?(int)$_REQUEST['id']:0;
...

// { get current page id
if(!$id){
    if($page){ // load by name
        $r=Page::getInstanceByName($page);
        if($r && isset($r->id))$id=$r->id;
    }
    if(!$id){ // else load by special
        $special=1;
        if(!$page){
            $r=Page::getInstanceBySpecial($special);
            if($r && isset($r->id))$id=$r->id;
        }
    }
}

// { load page data
if($id){
    $PAGEDATA=(isset($r) && $r)?$r : Page::getInstance($id);
}
else{
    echo '404 thing goes here';
    exit;
}
...
...

ww.incs/common.php

<?php
require dirname(__FILE__).'/basics.php';
...
...

ww.incs/basics.php

session_start();
if(!function_exists('__autoload')){
    function __autoload($name) {
        require $name . '.php';
    }
}
...
...

Page.php

class Page{
    static $instances             = array();
    static $instancesByName     = array();
    static $instancesBySpecial   = array();
    function __construct($v,$byField=0,$fromRow=0,$pvq=0){
        # byField: 0=ID; 1=Name; 3=special
        if (!$byField && is_numeric($v)){ // by ID
            $r=$fromRow?$fromRow:($v?dbRow("select * from pages where id=$v limit 1"):array());
        }
        else if ($byField == 1){ // by name
            $name=strtolower(str_replace('-','_',$v));
            $fname='page_by_name_'.md5($name);
            $r=dbRow("select * from pages where name like '".addslashes($name)."' limit 1");
        }
        else if ($byField == 3 && is_numeric($v)){ // by special
            $fname='page_by_special_'.$v;
            $r=dbRow("select * from pages where special&$v limit 1");
        }
        else return false;
        if(!count($r || !is_array($r)))return false;
        if(!isset($r['id']))$r['id']=0;
        if(!isset($r['type']))$r['type']=0;
        if(!isset($r['special']))$r['special']=0;
        if(!isset($r['name']))$r['name']='NO NAME SUPPLIED';
        foreach ($r as $k=>$v) $this->{$k}=$v;
        $this->urlname=$r['name'];
        $this->dbVals=$r;
        self::$instances[$this->id] =& $this;
        self::$instancesByName[preg_replace('/[^a-z0-9]/','-',strtolower($this->urlname))] =& $this;
        self::$instancesBySpecial[$this->special] =& $this;
        if(!$this->vars)$this->vars='{}';
        $this->vars=json_decode($this->vars);
    }
    function getInstance($id=0,$fromRow=false,$pvq=false){
        if (!is_numeric($id)) return false;
        if (!@array_key_exists($id,self::$instances)) self::$instances[$id]=new Page($id,0,$fromRow,$pvq);
        return self::$instances[$id];
    }
    function getInstanceByName($name=''){
        $name=strtolower($name);
        $nameIndex=preg_replace('#[^a-z0-9/]#','-',$name);
        if(@array_key_exists($nameIndex,self::$instancesByName))return self::$instancesByName[$nameIndex];
        self::$instancesByName[$nameIndex]=new Page($name,1);
        return self::$instancesByName[$nameIndex];
    }
    function getInstanceBySpecial($sp=0){
        if (!is_numeric($sp)) return false;
        if (!@array_key_exists($sp,$instancesBySpecial)) $instancesBySpecial[$sp]=new Page($sp,3);
        return $instancesBySpecial[$sp];
    }
    up vote160down voteaccepted

    Your methods are missing the static keyword. Change

    function getInstanceByName($name=''){

    to

    public static function getInstanceByName($name=''){

    if you want to call them statically.

    Note that static methods (and Singletons) are death to testability.

    Also note that you are doing way too much work in the constructor, especially all that querying shouldn't be in there. All your constructor is supposed to do is set the object into a valid state. If you have to have data from outside the class to do that consider injecting it instead of pulling it. Also note that constructors cannot return anything. They will always return void so all these return false statements do nothing but end the construction.

      I think this may answer your question.

      Non-static method ..... should not be called statically

      If the method is not static you need to initialize it like so:

      $var = new ClassName();
      $var->method();

      Or, in PHP 5.4, you can use this syntax:

      (new ClassName)->method();

        return false is usually meant to terminate the object creation with a failure. It is as simple as that.

        If scope resolution :: had to be used outside the class then the respective function or variable should be declared as static

        class Foo { 
                //Static variable 
                public static $static_var = 'static variable'; 
                //Static function 
                static function staticValue() { return 'static function'; } 
        
                //function 
                function Value() { return 'Object'; } 
        } 
        
        
        
         echo Foo::$static_var . "<br/>"; echo Foo::staticValue(). "<br/>"; $foo = new Foo(); echo $foo->Value();

          Try this:

          $r = Page()->getInstanceByName($page);

          It worked for me in a similar case.

          use className->function(); instead className::function() ;

          Instead of using the instance with the scope resolution operator :: because it wasn't defined like static function.

          $r=Page::getInstanceByName($page);

          change it to :

          $r=Page->getInstanceByName($page);

          And it will work like a charm.

            protected by Community Dec 8 '17 at 14:20

            Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count). 

            Would you like to answer one of these unanswered questions instead?

            Not the answer you're looking for? Browse other questions tagged   or ask your own question.


            + Recent posts