Created
June 4, 2012 22:50
-
-
Save johnny5th/2871313 to your computer and use it in GitHub Desktop.
Mongo Test - Address Book
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL); | |
// connect | |
$m = new Mongo(); | |
// select a database | |
$db = $m->mongotest; | |
// select a collection (analogous to a relational database's table) | |
$collection = $db->people; | |
if($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['action'] == "submit"){ | |
// add a record | |
$phones = array(); | |
for($i=1;$i<=3;$i++) | |
if($_POST['phone'.$i] != "") | |
$phones['Phone '.$i] = filter_var($_POST['phone'.$i], FILTER_SANITIZE_STRING); | |
$req = array( | |
"First Name" => filter_var($_POST['fname'], FILTER_SANITIZE_STRING), | |
"Last Name" => filter_var($_POST['lname'], FILTER_SANITIZE_STRING), | |
"Phone Numbers" => $phones ); | |
$searchcurrent = array( | |
"First Name" => filter_var($_POST['fname'], FILTER_SANITIZE_STRING), | |
"Last Name" => filter_var($_POST['lname'], FILTER_SANITIZE_STRING) | |
); | |
$collection->update($searchcurrent, $req, true); | |
} | |
?> | |
<style> | |
label{ | |
display:block; | |
} | |
label input{ | |
display: block; | |
} | |
fieldset{ | |
margin:20px 0; | |
} | |
</style> | |
<form method="post"> | |
<label>First Name <input type="text" name="fname"></label> | |
<label>Last Name <input type="text" name="lname"></label> | |
<fieldset> | |
<legend>Phones</legend> | |
<label>Phone 1 <input type="text" name="phone1"></label> | |
<label>Phone 2 <input type="text" name="phone2"></label> | |
<label>Phone 3 <input type="text" name="phone3"></label> | |
</fieldset> | |
<input type="hidden" name="action" value="submit"> | |
<input type="submit"> | |
</form> | |
<pre> | |
<? | |
// find everything in the collection | |
$cursor = $collection->find(); | |
// iterate through the results | |
foreach ($cursor as $obj) { | |
print_r($obj); | |
} | |
?> | |
</pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment