working on it ...

Filters

Explore Public Snippets

Sort by

Found 63k snippets

    public by simon_sourcecode modified May 2, 2018  533  0  3  0

    Konstanten durch neue Definition ersetzen

    PHP
    // Replace define with const
    [DEFINdefin]{6}[ ]*\(\'ARTRES_PROP_([A-Z_0-9]+)\',([\t 0-9]+)\)\;

    public by azazqadir modified Apr 11, 2018  480  1  5  0

    Using WooCommerce Rest API for Custom Dashboard

    Using WooCommerce Rest API (https://www.cloudways.com/blog/custom-dashboard-using-woocommerce-php-rest-api/ ) to create a Custom Dashboard
    <div class="container">
    
                                      <h2 class="sub-header">Customers List</h2>
    
                                      <div class='table-responsive'>
    
                                          <table id='myTable' class='table table-striped table-bordered'>
    
                                              <thead>
    
                                                  <tr>
    
                                                      <th>Email</th>
    
                                                      <th>Name</th>
    
                                                      <th>Billing Address</th>
    
                                                      <th>Total Orders</th>
    
                                                      <th>Total spent</th>
    
                                                      <th>Avatar</th>
    
                                                      <th>Actions</th>
    
                                                  </tr>
    
                                              </thead>
    
                                              <tbody>
    
                                                  <?php
    
                      foreach($customers as $customer){
    
    
    
                      echo "<tr><td>" . $customer["email"]."</td>
    
                                <td>" . $customer["first_name"].$customer["last_name"]."</td>
    
                                <td>" . $customer["billing"]["address_1"]."</td>
    
                                <td>" . $customer["orders_count"]."</td>
    
                                <td>" . $customer["total_spent"]."</td>
    
                                <td><img height='50px' width='50px' src='".$customer["avatar_url"]."'></td>
    
                                 <td><a class='open-AddBookDialog btn btn-primary' data-target='#myModal' data-id=".$customer['id']." data-toggle='modal'>Update</a>
    
                                 <a class='open-deleteDialog btn btn-danger' data-target='#myModal1' data-id=".$customer['id']." data-toggle='modal'>Delete</a></td></tr>";
    
                      }
    
    
    
                      ?>
    
                                              </tbody>
    
                                          </table>
    
                                      </div>
    
      </div>												

    public by DaveMorton modified Apr 2, 2018  434  2  5  1

    connect.php - Database Abstraction Functions

    <?php
    /* 
     *  %[Filename]=[connect.php]%
     * This is a complete set of PDO abstraction functions
     * to make connecting, reading from and writing to a
     * MySQL database faster and easier. It's meant to be
     * used as a snippet in RapidPHP/WeBuilder from Karlis
     * Bluementals, but can easily be used in many other
     * web development IDEs. Items with %[something]% or
     * %[something]=[value]% are replaceable parameters
     * in RapidPHP/WeBuilder and can be swapped out with
     * whatever you like
     */
    
    /* DB Credentials */
    
    $dbh    = '%[Hostname]=[localhost]%';
    $dbn    = '%[DB Name]%';
    $dbu    = '%[Username]=[username]%';
    $dbp    = '%[Password]=[password]%';
    $dbPort = '%[DB Port]=[3306]%';
    
    $db = db_open($dbh, $dbu, $dbp, $dbn, $dbPort);
    
    /**
     * function db_open()
     * Connect to the database
     *
     * @internal param string $dbh    - db host
     * @internal param string $dbu    - db user
     * @internal param string $dbp    - db password
     * @internal param string $dbn    - db name
     * @internal param string $dbPort - db port
     * @return resource $dbConn - the database connection resource
     */
    function db_open($dbh, $dbu, $dbp, $dbn, $dbPort=3306)
    {
        try {
            $dbConn = new PDO("mysql:host=$dbh;dbname=$dbn;charset=utf8", $dbu, $dbp);
            $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbConn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            $dbConn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
        }
        catch (Exception $e)
        {
            exit('The application has encountered a problem with connecting to the database. With any luck, the following message will help: ' . $e->getMessage());
        }
        return $dbConn;
    }
    
    /**
     * function db_close()
     * Close the connection to the database
     *
     * @internal param resource $dbConn - the open connection
     *
     * @return null
     */
    function db_close()
    {
        return null;
    }
    
    /*
     * function db_fetch_all
     * Fetches all matching rows of data from the database
     *
     * @param (string) $sql - The SQL query to execute
     * @param (mixed) $params - either an array of placeholder/value pairs, or null, for no parameters
     * @param (string) $file - the path/filename of the file that the function call originated in
     * @param (string) $function - the name of the function that the function call originated in
     * @param (string) $line - the line number of the originating function call
     *
     * @return (mixed) $out - Either the row of data from the DB query, or false, if the query fails
     */
    
    function db_fetch_all($sql, $params = null, $file = 'unknown', $function = 'unknown', $line = 'unknown')
    {
        global $dbConn;
        try
        {
            $sth = $dbConn->prepare($sql);
            ($params === null) ? $sth->execute() : $sth->execute($params);
            return $sth->fetchAll();
        }
        catch (Exception $e)
        {
            //error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\n", 3, _LOG_PATH_ . 'badSQL.txt');
            $pdoError = print_r($dbConn->errorInfo(), true);
            $psError  = print_r($sth->errorInfo(), true);
            return false;
        }
    }
    
    /*
     * function db_fetch_row
     * Fetches a single row of data from the database
     *
     * @param (string) $sql - The SQL query to execute
     * @param (mixed) $params - either an array of placeholder/value pairs, or null, for no parameters
     * @param (string) $file - the path/filename of the file that the function call originated in
     * @param (string) $function - the name of the function that the function call originated in
     * @param (string) $line - the line number of the originating function call
     *
     * @return (mixed) $out - Either the row of data from the DB query, or false, if the query fails
     */
    function db_fetch_row($sql, $params = null, $file = 'unknown', $function = 'unknown', $line = 'unknown')
    {
        global $dbConn;
        //error_log(print_r($dbConn, true), 3, _LOG_PATH_ . 'dbConn.txt');
        try
        {
            $sth = $dbConn->prepare($sql);
            ($params === null) ? $sth->execute() : $sth->execute($params);
            $out = $sth->fetch();
            return $out;
        }
        catch (Exception $e)
        {
            //error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\n", 3, _LOG_PATH_ . 'badSQL.txt');
            $pdoError = print_r($dbConn->errorInfo(), true);
            $psError  = print_r($sth->errorInfo(), true);
            return false;
        }
    }
    
    /*
     * function db_write
     * Writes and/or updates data to the database
     *
     * @param (string) $sql - The SQL query to execute
     * @param (mixed) $params - either an array of placeholder/value pairs, or null, for no parameters
     * @param (string) $file - the path/filename of the file that the function call originated in
     * @param (string) $function - the name of the function that the function call originated in
     * @param (string) $line - the line number of the originating function call
     *
     * @return (mixed) $out - Either the number of rows affected from the DB query, or false, if the query fails
     */
    
    function db_write($sql, $params = null, $multi = false, $file = 'unknown', $function = 'unknown', $line = 'unknown')
    {
        global $dbConn;
        try
        {
            $sth = $dbConn->prepare($sql);
            switch (true)
            {
                case ($params === null):
                    $sth->execute();
                    break;
                case ($multi === true):
                    foreach ($params as $row)
                    {
                        $sth->execute($row);
                    }
                    break;
                default:
                    $sth->execute($params);
            }
            return $sth->rowCount();
        }
        catch (Exception $e)
        {
            $pdoError = print_r($dbConn->errorInfo(), true);
            $psError  = print_r($sth->errorInfo(), true);
            error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\nPDO Error:\n$pdoError\nSTH Error:\n$psError\nEsception Message:\n" . $e->getMessage(), 3, _LOG_PATH_ . 'db_write.txt');
            return false;
        }
    }

    public by brsgrlr modified Mar 5, 2018  627  3  4  0

    phptags

    opening and closing php tags
    PHP
    <?php  ?>

    public by azazqadir modified Feb 20, 2018  531  1  4  1

    LEMP on Debian

    Learn how to install LEMP on Debian: https://www.cloudways.com/blog/how-to-create-a-lemp-stack-on-debian-server/
    php
    location ~ \.php$ {
    
                   
    
                   fastcgi_pass unix:/var/run/php5-fpm.sock;
    
                   fastcgi_index index.php;
    
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
                   include fastcgi_params;
    
                   
    
           }			

    public by azazqadir modified Jan 18, 2018  338  0  5  0

    Adding Captcha in CodeIgniter

    Source: https://www.cloudways.com/blog/captcha-in-codeigniter/
    <?php
    
    defined('BASEPATH') OR exit('your exit message');
    
    class Captcha extends CI_Controller
    
    {
    
       function __construct()
    
       {
    
           parent::__construct();
    
           $this->load->helper('captcha');
    
       }
    
       public function index()
    
       {
    
           if ($this->input->post('submit')) {
    
               $captcha_insert = $this->input->post('captcha');
    
               $contain_sess_captcha = $this->session->userdata('valuecaptchaCode');
    
               if ($captcha_insert === $contain_sess_captcha) {
    
                   echo 'Success';
    
               } else {
    
                   echo 'Failure';
    
               }
    
           }
    
           $config = array(
    
               'img_url' => base_url() . 'image_for_captcha/',
    
               'img_path' => 'image_for_captcha/',
    
               'img_height' => 45,
    
               'word_length' => 5,
    
               'img_width' => '45',
    
               'font_size' => 10
    
           );
    
           $captcha = create_captcha($config);
    
           $this->session->unset_userdata('valuecaptchaCode');
    
           $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
    
           $data['captchaImg'] = $captcha['image'];
    
           $this->load->view('captcha/index', $data);
    
       }
    
       public function refresh()
    
       {
    
           $config = array(
    
               'img_url' => base_url() . 'image_for_captcha/',
    
               'img_path' => 'image_for_captcha/',
    
               'img_height' => 45,
    
               'word_length' => 5,
    
               'img_width' => '45',
    
               'font_size' => 10
    
           );
    
           $captcha = create_captcha($config);
    
           $this->session->unset_userdata('valuecaptchaCode');
    
           $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
    
           echo $captcha['image'];
    
       }
    
    }

    public by azazqadir modified Nov 21, 2017  467  0  5  1

    CDN integration in Yii2 App

    How to integrate CloudwaysCDN with Yii2 applications: https://www.cloudways.com/blog/cdn-in-yii2/
    // ...
    
    'components' => [
    
       // ...
    
    'cdn' => [
                'class' => '\yii2cdn\Cdn',
                'baseUrl' => defined ('YII2CDN_OFFLINE')
                    ? 'http://localhost/yii2/cdn'
                    : '//71265-358070-raikfcquaxqncofqfm.stackpathdns.com/cdn',
                'basePath' => dirname(__DIR__, 2) . '/cdn',
                'components' => [
                    'font-awesome' => [
                        'css' => [
                            [
                                // local version
                                'font-awesome.min.css',
                                // cdn version
                                '@cdn' => '//71265-358070-raikfcquaxqncofqfm.stackpathdns.com/cdn/font-awesome/css/font-awesome.min.css',
                            ]
                        ]
                    ]
                ],
            ],
     // ...
    
    ],
    
    // ...

    public by azazqadir modified Nov 15, 2017  255  0  3  0

    Using Eloquent Outside of Laravel for Other PHP Apps. Source: https://www.cloudways.com/blog/eloquent-illuminate-in-php-without-laravel/

    Using Eloquent Outside of Laravel for Other PHP Apps. Source: https://www.cloudways.com/blog/eloquent-illuminate-in-php-without-laravel/: bootstrap.php
    <?php
    
    
    
    require "vendor/autoload.php";
    
    use Illuminate\Database\Capsule\Manager as Capsule;
    
    
    
    $capsule = new Capsule;
    
    
    
    $capsule->addConnection([
    
       "driver" => "mysql",
    
       "host" =>"127.0.0.1",
    
       "database" => "acl",
    
       "username" => "root",
    
       "password" => ""
    
    ]);
    
    $capsule->setAsGlobal();
    
    $capsule->bootEloquent();
    
    

    public by AbhishekGhosh modified Oct 11, 2017  330  2  3  0

    ob flush example

    ob flush example: ob_flush.php
    <?php
    
    /*
    	== ob_end_flush() ==
    	Flush (send) the output buffer and turn off output buffering
    
    	== ob_end_clean() ==
    	Clean (erase) the output buffer and turn off output buffering
    
    	== ob_get_contents() ==
    	Return the contents of the output buffer
    */
    
    
    # The following two functions result in the same output.
    
    function output_some_html_with_var() {
    	ob_start();
    	?>
    	<p>This is a paragraph echoed from a variable.</p>
    	<?php
    	$para = ob_get_contents();
    	ob_end_clean();
    	echo $para;
    }
    
    function output_some_html_with_end_flush() {
    	ob_start();
    	?>
    	<p>This is a paragraph sent with ob_end_flush.</p>
    	<?php
    	ob_end_flush();
    }
    
    output_some_html_with_var();
    
    output_some_html_with_end_flush();
    
    /*
    	If you want to return the contents of ob_start()
    	you should store ob_get_contents() into a variable,
    	then call ob_end_clean(), and then return the variable.
    */
    
    function return_some_html_with_var() {
    	ob_start();
    	?>
    	<p>This is a paragraph being stored into a variable and returned.</p>
    	<?php
    	$para = ob_get_contents();
    	ob_end_clean();
    	return $para;
    }
    
    echo return_some_html_with_var();
    
    echo '<a style="text-decoration:none;" href="http://php.net/manual/en/function.ob-start.php">See the ob_start documentiation for more info</a>';
    
    

    public by azazqadir modified Oct 9, 2017  227  0  3  0

    Laravel REST API Authentication with Passport: https://www.cloudways.com/blog/rest-api-laravel-passport-authentication/

    Laravel REST API Authentication with Passport: https://www.cloudways.com/blog/rest-api-laravel-passport-authentication/: auth.php
    'guards' => [
    
       'web' => [
    
           'driver' => 'session',
    
           'provider' => 'users',
    
       ],
    
    
    
       'api' => [
    
           'driver' => 'passport',
    
           'provider' => 'users',
    
       ],
    
    ],
    
    
    • Public Snippets
    • Channels Snippets