Skip to content

Commit b08673d

Browse files
author
Steven Surowiec
committed
Added search functionality
1 parent ef9b987 commit b08673d

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

mongodbadmin.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
184184

185185
// Actions
186186
try {
187-
// SEARCH
188-
if (isset($_REQUEST['search'])) {
187+
// SEARCH BY ID
188+
if (isset($_REQUEST['search_by_id'])) {
189189
$customId = false;
190-
$document = findMongoDbDocument($_REQUEST['search'], $_REQUEST['db'], $_REQUEST['collection']);
190+
$document = findMongoDbDocument($_REQUEST['search_by_id'], $_REQUEST['db'], $_REQUEST['collection']);
191191

192-
if (!$document) {
193-
$document = findMongoDbDocument($_REQUEST['search'], $_REQUEST['db'], $_REQUEST['collection'], true);
192+
if (!$document) {
193+
$document = findMongoDbDocument($_REQUEST['search_by_id'], $_REQUEST['db'], $_REQUEST['collection'], true);
194194
$customId = true;
195195
}
196196

@@ -383,7 +383,7 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
383383
border: 1px solid #ccc;
384384
padding: 8px;
385385
margin-bottom: 15px;
386-
width: 350px;
386+
width: 400px;
387387
float: right;
388388
}
389389
table {
@@ -516,24 +516,36 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
516516
</table>
517517

518518
<?php // CREATE AND LIST DB COLLECTION DOCUMENTS ?>
519-
<?php elseif ( ! isset($_REQUEST['id'])): ?>
519+
<?php elseif ( ! isset($_REQUEST['id']) || isset($_REQUEST['search'])): ?>
520520

521521
<h2>
522522
<a href="<?php echo $_SERVER['PHP_SELF'] ?>">Databases</a> >>
523523
<a href="<?php echo $_SERVER['PHP_SELF'] ?>?db=<?php echo $_REQUEST['db'] ?>"><?php echo $_REQUEST['db'] ?></a> >>
524524
<?php echo $_REQUEST['collection'] ?>
525525
</h2>
526+
526527
<?php
527528
$max = 20;
528529
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
529530
$limit = $max;
530531
$skip = ($page - 1) * $max;
531-
$cursor = $mongo
532+
533+
if (isset($_REQUEST['search'])) {
534+
$cursor = $mongo
535+
->selectDB($_REQUEST['db'])
536+
->selectCollection($_REQUEST['collection'])
537+
->find(json_decode($_REQUEST['search'], true))
538+
->limit($limit)
539+
->skip($skip);
540+
} else {
541+
$cursor = $mongo
532542
->selectDB($_REQUEST['db'])
533543
->selectCollection($_REQUEST['collection'])
534544
->find()
535545
->limit($limit)
536546
->skip($skip);
547+
}
548+
537549
$total = $cursor->count();
538550
$pages = ceil($total / $max);
539551
if ($pages && $page > $pages) {
@@ -551,7 +563,13 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
551563
<div id="search">
552564
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?db=<?php echo $_REQUEST['db'] ?>&collection=<?php echo $_REQUEST['collection'] ?>" method="POST">
553565
<label for="search_input">Search by ID</label>
554-
<input type="text" id="search_input" name="search" size="20" />
566+
<input type="text" id="search_input" name="search_by_id" size="30" />
567+
<input type="submit" name="submit_search" value="Search" />
568+
</form>
569+
570+
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?db=<?php echo $_REQUEST['db'] ?>&collection=<?php echo $_REQUEST['collection'] ?>" method="POST">
571+
<label for="search_input">Search</label>
572+
<input type="text" id="search_input" name="search" size="36"<?php echo isset($_REQUEST['search']) ? ' value="' . htmlspecialchars($_REQUEST['search']) . '"': '' ?> />
555573
<input type="submit" name="submit_search" value="Search" />
556574
</form>
557575
</div>
@@ -632,3 +650,4 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
632650
</div>
633651
</body>
634652
</html>
653+

0 commit comments

Comments
 (0)