Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // config
- $accountId = 102193;
- $database = 'todo';
- // 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'; //this is a dummy password and you should signup for your own at clusterpoint.com
- // More about INSERT: https://www.clusterpoint.com/docs/?page=Insert
- if (isset($_POST['new_priority'])) {
- // insert new to-do item
- $doc = array(
- 'priority' => $_POST['new_priority'],
- '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 todo["' . addslashes($_POST['update_id']) . '"] SET text = "' . addslashes($_POST['text']) . '", priority = "' . addslashes($_POST['priority']) . '"';
- $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 todo ORDER BY \'timestamp\' DESC';
- // search
- if (isset($_GET['search']) && $_GET['search'] !== '') {
- // We are storing item text in field named "text"
- $query = 'SELECT * FROM todo 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);
- $todos = 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> ';
- $priorityInput = date('Y-m-d H:i:s', (string)$document->timestamp) . '
- <input type="number" name="priority[]" value="' . (string)$document->priority . '" id="priority_' . $inc . '"> ';
- $textInput = '<input type="text" name="text[]" value="' . (string)$document->text . '" id="text_' . $inc . '"> ';
- $todos[] = '<li> ' . $priorityInput . $textInput . $btnUpdate . $btnDelete . '</li>';
- }
- // More about Aggregation: https://www.clusterpoint.com/docs/?page=Aggregation_with_GROUP_BY
- $ch = curl_init();
- $query = 'SELECT priority, COUNT(priority) AS count FROM todo 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>Simple To-Do list</title>
- <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="number"] {
- width: 70px;
- }
- </style>
- </head>
- <body>
- <h1>To-Do 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('', $todos) ?>
- </ul>
- <ul>
- <? echo implode('', $stats) ?>
- </ul>
- <ul>
- <li>
- <p>Add new item:</p>
- <input type="number" name="priority" value="" id="new_priority" placeholder="Priority"/>
- <input type="text" name="todo" value="" id="new_text" placeholder="Text"/>
- <button onclick="createItem()">Add</button>
- </li>
- </ul>
- </body>
- </html>
- <script type="text/javascript">
- function createItem() {
- $.post('', {
- new_priority: $('#new_priority').val(),
- new_text: $('#new_text').val()
- },
- function () {
- alert('Item created!');
- location.reload();
- });
- }
- function updateItem(docId, fieldsId) {
- $.post('', {
- update_id: docId,
- text: $('#text_' + fieldsId).val(),
- priority: $('#priority_' + 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