Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // config
- $accountId = 102193;
- $database = 'URLS';
- // Note: Replace 'api-eu' with 'api-us', if you are connecting to US cloud!
- $url = 'https://api-us.clusterpoint.com/v4/' . $accountId . '/' . $database;
- $userPassword = 'marun2@gmail.com:capitalism';
- // More about INSERT: https://www.clusterpoint.com/docs/?page=Insert
- if (isset($_POST['new_URL'])) {
- // insert new URL item
- $doc = array(
- 'URL' => $_POST['new_URL'],
- 'text' => $_POST['new_text'],
- 'timestamp' => time(),
- );
- $ch = curl_init();
- // https://api-eu.clusterpoint.com/v4/ACCOUNT_ID/DATABASE_NAME[ID] - Insert single document with explicit ID
- curl_setopt($ch, CURLOPT_URL, $url); // In this case document ID is automatically generated by system.
- curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($doc));
- $response = curl_exec($ch);
- $errorMsg = curl_error($ch);
- if ($errorMsg) {
- var_dump($errorMsg);
- }
- curl_close($ch);
- die(); // AJAX request, just kill it.
- }
- // More about UPDATE: https://www.clusterpoint.com/docs/?page=Update
- if (isset($_POST['update_id'])) {
- $query = 'UPDATE URLS["' . addslashes($_POST['update_id']) . '"] SET text = "' . addslashes($_POST['text']) . '", URL = "' . addslashes($_POST['URL']) . '"';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url . '/_query');
- curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // or use PATCH request https://www.clusterpoint.com/docs/?page=Update
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
- $response = curl_exec($ch);
- $errorMsg = curl_error($ch);
- if ($errorMsg) {
- var_dump($errorMsg);
- }
- curl_close($ch);
- die(); // AJAX request, just kill it.
- }
- if (isset($_POST['delete_id'])) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url . '['.$_POST['delete_id'].']');
- curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- $errorMsg = curl_error($ch);
- if ($errorMsg) {
- var_dump($errorMsg);
- }
- curl_close($ch);
- die(); // AJAX request, just kill it.
- }
- // More about QUERY: https://www.clusterpoint.com/docs/?page=Query
- $ch = curl_init();
- $query = 'SELECT * FROM URLS ORDER BY \'timestamp\' DESC';
- // search
- if (isset($_GET['search']) && $_GET['search'] !== '') {
- // We are storing item text in field named "text"
- $query = 'SELECT * FROM URLS WHERE text.CONTAINS("*'.addslashes($_GET['search']).'*") ORDER BY \'timestamp\' DESC';
- // More info: https://www.clusterpoint.com/docs/?page=Matching_Documents_with_WHERE
- }
- curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
- curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
- $response = json_decode(curl_exec($ch));
- $errorMsg = curl_error($ch);
- if ($errorMsg) {
- var_dump($errorMsg);
- }
- curl_close($ch);
- $URLS = array();
- $inc = 0;
- foreach ($response->results as $document) {
- $inc++;
- $btnUpdate = '<button onclick="updateItem(\'' . $document->_id . '\',' . $inc . ')">update</button> ';
- $btnDelete = '<button onclick="deleteItem(\'' . $document->_id . '\',' . $inc . ')">delete</button> ';
- $URLInput = date('Y-m-d H:i:s', (string)$document->timestamp) . '<input type="text" name="URL[]" value="' . (string)$document->URL . '" id="URL_' . $inc . '"> ';
- $textInput = '<input type="text" name="text[]" value="' . (string)$document->text . '" id="text_' . $inc . '"> ';
- $URLS[] = '<li> ' . $URLInput . $textInput . $btnUpdate . $btnDelete . '</li>';
- }
- // More about Aggregation: https://www.clusterpoint.com/docs/?page=Aggregation_with_GROUP_BY
- /*
- $ch = curl_init();
- $query = 'SELECT URL, COUNT(URL) AS count FROM URLS GROUP BY priority ORDER BY priority DESC';
- curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
- curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
- $response = json_decode(curl_exec($ch));
- $errorMsg = curl_error($ch);
- if ($errorMsg) {
- var_dump($errorMsg);
- }
- curl_close($ch);
- $stats = array();
- foreach ($response->results as $document) {
- $stats[] = '<li>' . $document->count . ' tasks with priority: ' . $document->priority . ' </li>';
- }
- */
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
- <title>My public bookmarks, my comments searchable</title>
- <link rel="import" href="statcounter-include.html">
- <!--<script type="text/javascript" src="zopim-include.js" defer></script>-->
- <!--Start of Zopim Live Chat Script-->
- <script type="text/javascript">
- window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
- d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
- _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
- $.src="//v2.zopim.com/?3OEmwjPqSV4goKBnrct9k32AQkWeres8";z.t=+new Date;$.
- type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
- </script>
- <!--End of Zopim Live Chat Script-->
- <style type="text/css">
- body {
- width: 600px;
- margin: 0 auto;
- }
- ul {
- list-style: none;
- padding: 0;
- }
- ul li {
- border: 1px solid gray;
- padding: 5px
- }
- input[type="text"] {
- width: 300px;
- }
- </style>
- </head>
- <body>
- <h1>URL list:</h1>
- <form action="" method="get">
- Search: <input type="text" value="<? echo @$_GET['search'] ?>" name="search"/>
- <button type="submit">Search</button>
- </form>
- <ul>
- <?php echo implode('', $URLS) ?>
- </ul>
- <ul>
- <? //echo implode('', $stats) ?>
- </ul>
- <ul>
- <li>
- <p>Add new item:</p>
- <input type="text" name="new_URL" value="" id="new_URL" placeholder="URL"/>
- <input type="text" name="new_text" value="" id="new_text" placeholder="Text"/>
- <button onclick="createItem()">Add</button>
- </li>
- </ul>
- </body>
- </html>
- <script type="text/javascript">
- function createItem() {
- $.post('', {
- URL: $('#new_URL').val(),
- text: $('#new_text').val()
- },
- function () {
- alert('Item created!');
- location.reload();
- });
- }
- function updateItem(docId, fieldsId) {
- $.post('', {
- update_id: docId,
- text: $('#text_' + fieldsId).val(),
- URL: $('#URL_' + fieldsId).val()
- },
- function () {
- alert('Item updated!');
- location.reload();
- });
- }
- function deleteItem(id) {
- $.post('', { delete_id: id },
- function () {
- alert('Item deleted!');
- location.reload();
- });
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement