This is a complete example for searching advertisements.
It uses a form page which allows the user to filter by keywords, category, trade type, publication period, price range, with images only and member custom fields. The form is submitted to the result page, which displays the returned advertisements. On this page, there is a link to the details page to show more details on an specific advertisement.
<?php
// Get the web service proxy
require_once 'cyclos.php';
$cyclos = new Cyclos();
// Find the advertisement categories
$adsService = $cyclos->service('ads');
$categories = null;
try {
$result = $adsService->listCategories(array());
$categories = ensure_array($result->return);
} catch (SoapFault $e) {
die("Error retrieving advertisement categories: $e");
}
// Find the member custom fields which are used for searching advertisements
$fieldsService = $cyclos->service('fields');
$memberFields = null;
try {
$result = $fieldsService->memberFieldsForAdSearch(array());
$memberFields = ensure_array($result->return);
} catch (SoapFault $e) {
die("Error retrieving member fields for advertisement search: $e");
}
// Helper function to build a list of options
function options_for_elements($elements, $value_field, $label_field) {
$options = "";
foreach ($elements as $el) {
$options = $options . "<option value='" . $el->$value_field . "'>" . $el->$label_field . "</option>";
}
return $options;
}
?>
<h1>Search advertisements</h1>
<form method="post" action="search_ads_result.php">
<table>
<tr>
<td>Keywords</td>
<td><input type="text" name="keywords"></td>
</tr>
<tr>
<td>Category</td>
<td>
<select name="category">
<option value="">Any</option>
<? foreach ($categories as $category) { ?>
<option value="<?= $category->id ?>"> <?= $category->name ?> </option>
<? } ?>
</select>
</td>
</tr>
<tr>
<td>Type</td>
<td>
<label><input type="radio" checked="checked" name="trade_type" value="OFFER"> Offers</label>
<label><input type="radio" name="trade_type" value="SEARCH"> Requests</label>
</td>
</tr>
<tr>
<td>Published within</td>
<td>
<input name="since_number" maxLength="2" style="width:30px;">
<select name="since_field">
<option value="DAYS">Days</option>
<option value="WEEKS">Weeks</option>
<option value="MONTHS">Months</option>
</select>
</td>
</tr>
<tr>
<td>Price range</td>
<td>
<input type="text" name="initial_price" size="12">
to
<input type="text" name="final_price" size="12">
</td>
</tr>
<tr>
<td>Only with images</td>
<td><input type="checkbox" name="with_images_only" class="checkbox" value="true"></td>
</tr>
<? // Find out which member custom fields may be used to search ads
foreach($memberFields as $field) { ?>
<tr>
<td><?= $field->displayName ?></td>
<td>
<? //This hidden will store the field ids, to match positionally the respective values ?>
<input type="hidden" name="memberFieldIds[]" value="<?= $field->id ?>" />
<? //Check the field type
if ($field->enumerated) {
// Enumerated fields will be displayed as a select. Get the possible values
try {
$result = $fieldsService->possibleValuesForMemberField(array('name' => $field->internalName));
$possibleValues = ensure_array($result->return);
} catch (SoapFault $e) {
die("Error retrieving possible values for field $field->internalName: $e");
} ?>
<select name="memberFieldValues[]">
<option value="">Any</option>
<? foreach ($possibleValues as $value) { ?>
<option value="<?= $value->id ?>"> <?= $value->value ?> </option>
<? } ?>
</select>
<? } else { ?>
<input type="text" name="memberFieldValues[]" />
<? } ?>
</td>
</tr>
<? } ?>
<tr>
<td></td>
<td><input type="submit" value="Search"></td>
</tr>
</table>
</form>
<?php
// Get the web service proxy
require_once 'cyclos.php';
$cyclos = new Cyclos();
$adsService = $cyclos->service('ads');
// Build the search parameters
$params = new stdclass();
// Use the simple parameters
$params->pageSize = 15;
$params->keywords = trim($_POST['keywords']);
$params->tradeType = trim($_POST['trade_type']);
$params->categoryId = trim($_POST['category']);
$params->initialPrice = trim($_POST['initial_price']);
$params->finalPrice = trim($_POST['final_price']);
$params->withImagesOnly = trim($_POST['with_images_only']);
// Build the since parameter
$since_number = trim($_POST['since_number']);
$since_field = trim($_POST['since_field']);
if (strlen($since_number) > 0 && strlen($since_field) > 0) {
$params->since = new stdclass();
$params->since->number = $since_number;
$params->since->field = $since_field;
}
// Build the member custom field parameters
$fieldIds = ensure_array($_POST['memberFieldIds']);
$fieldValues = ensure_array($_POST['memberFieldValues']);
$fieldIdsSize = count($fieldIds);
$fieldValuesSize = count($fieldIds);
if ($fieldIdsSize != $fieldValuesSize) {
die("Invalid member field submission");
}
$params->memberFields = array();
for ($i = 0; $i < $fieldIdsSize; $i++) {
$field_value = new stdclass();
$field_value->field = $fieldIds[$i];
$field_value->value = $fieldValues[$i];
$params->memberFields[$i] = $field_value;
}
// Execute the search
$page = $adsService->fullTextSearch(array('params' => $params))->return;
$ads = ensure_array($page->ads);
?>
<h1>Search results</h1>
Showing <?= count($ads) ?> of <?= $page->totalCount ?> advertisements.<br>
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<th> </th>
<th>Title</th>
<th>User</th>
<th>Price</th>
</tr>
<?php foreach($ads as $ad) { ?>
<tr>
<td><img src='<?php echo $ad->images->thumbnailUrl ?>'></td>
<td><a href="view_ad_details.php?id=<? echo $ad->id ?>"><?= $ad->title ?></a></td>
<td><?= $ad->owner->name ?></td>
<td><?= $ad->formattedPrice ?> </td>
</tr>
<?php } ?>
</table>
<?php
// Get the web service proxy
require_once 'cyclos.php';
$cyclos = new Cyclos();
$adsService = $cyclos->service('ads');
// Load the advertisement details
$ad = $adsService->load(array('id' => $_GET['id']))->return;
?>
<h1><?= $ad->title ?></h1>
Published by <?= $ad->owner->name ?><br>
<?php if (!empty($ad->price)) { ?>
Price: <?= $ad->formattedPrice ?><br>
<?php } ?>
<?= $ad->description ?><br>
<br>
<?php foreach(ensure_array($ad->images) as $image) { ?>
<a href="<?= $image->fullUrl ?>"><img src="<?= $image->thumbnailUrl ?>" border="0"></a>
<?php } ?>
<br><br>
<input type="button" value="Back" onclick="JavaScript:(history.go(-1))">