Skip to content

Instantly share code, notes, and snippets.

@vishnusomanus
Last active September 20, 2018 09:47
Show Gist options
  • Save vishnusomanus/b303d0dd588b7abcbad4c119c87e3d4f to your computer and use it in GitHub Desktop.
Save vishnusomanus/b303d0dd588b7abcbad4c119c87e3d4f to your computer and use it in GitHub Desktop.
mongo.php
..................................................
<?php
/**
* Mongo connection
*/
class Mongo_db extends MongoClient
{
private $connection;
private $username;
private $password;
private $hostname;
private $port;
private $database;
function __construct()
{
$this->username ="admin";
$this->password ="admin";
$this->hostname ="localhost";
$this->port ="27017";
$database ="keralasme";
$this->connection = "mongodb://{$this->hostname}:{$this->port}/{$this->database}";
$this->db = (new MongoClient($this->connection))->$database; // Connect to Database
}
public function select($collection)
{
$documents = $this->db->$collection->find();
return $documents;
}
}
?>
index.php
..................................................
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'mongo.php';
$db = new Mongo_db();
$data = $db->select('category');
foreach ($data as $key => $value) {
echo "<pre>";
print_r($value);
}
?>
function create_slug($collection,$slug)
{
$ci =& get_instance();
$ci->load->model('Select');
$slug = trim($slug);
$slug = preg_replace('!\s+!', ' ', $slug);
$slug = preg_replace('/[^A-Za-z0-9\-]/', ' ', $slug);
$slug = strtolower(str_replace(' ', '_', $slug));
$data = $ci->Select->db->$collection->findOne(['slug'=>$slug]);
if($data == NULL)return $slug;
$i = 1;
$new_slug = $slug;
while($data != NULL)
{
$new_slug = $slug.'_'.$i;
$data = $ci->Select->db->$collection->findOne(['slug'=>$new_slug]);
if($data == NULL)return $new_slug;
$i++;
}
return $new_slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment