Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Created December 18, 2012 19:20
Show Gist options
  • Save rjcorwin/4331047 to your computer and use it in GitHub Desktop.
Save rjcorwin/4331047 to your computer and use it in GitHub Desktop.
An example of using the CouchDB client for PHP (https://github.com/dready92/PHP-on-Couch)
<?php
session_start();
error_reporting(1);
// A handy library for talking to CouchDB (https://github.com/dready92/PHP-on-Couch).
// You could use another library to make pure HTTP calls but sometimes using this couch library is just easier because
// it is very concise.
require_once 'couch.php';
require_once 'couchClient.php';
require_once 'couchDocument.php';
// There is one more additional line in talk2db.php
// $couch = new couchClient('http://127.0.0.1:5984', 'schoolbell');
include "talk2db.php";
// Get the data for this page
try {
// This calls a view named "resources" which is contained in a design document called "bell-views"
/*
* The resources view is one simple map function in CouchDB
*
* if (doc.type == "resource") {
* emit(doc._id, doc.title)
* }
*/
$view = $couch->getView('bell-views', 'resources');
} catch (Exception $e) {
echo "something weird happened: ".$e->getMessage()."<BR>\n";
}
?>
<html>
<head>
<title>Open Learning Exchange - Ghana</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="stylesheet/img/devil-icon.png">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body style="background-color:#FFF">
<div id="wrapper" style="background-color:#FFF; width:600px;">
<div id="rightContent" style="float:none; margin-left:auto; margin-right:auto; width:550px; margin-left:auto; margin-right:auto;">
<table width="105%">
<tr>
<td height="24"><b>Resource Title</b></td>
<td width="65">Option</td>
</tr>
<?php
$cnt = 0;
foreach($view->rows as $row)
{
if($cnt%2==0)
{
echo '<tr>
<td>'.$row->value.'</td>
<td><input type="button" class="button" value="View"></td>
</tr>';
}
else
{
echo '<tr bgcolor="#F0F0F0">
<td>'.$row->value.'</td>
<td><input type="button" class="button" value="View"></td>
</tr>';
}
$cnt++;
}
?>
</table>
</div>
<div class="clear"></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment