Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/Devices/Search.php
<?php

require_once('./CS_Includes.inc.php');

try {
	CS::Check($_POST['hostname_bool'], CSA_EMPTY|CSA_THROW, 'DeviceSearch with no hostname_bool');
	CS::Check($_POST['ipaddress_bool'], CSA_EMPTY|CSA_THROW, 'DeviceSearch with no ipaddress_bool');
	CS::Check($_POST['sys_descr_bool'], CSA_EMPTY|CSA_THROW, 'DeviceSearch with no sys_descr_bool');

	// Let's see what options the user has passed for the WHERE clauses.
	//

	$whereClauses = array();
	$whereItems = array();

	if (!empty($_POST['hostname']))
	{
		$whereClauses[] = '`hostname` '.$CSS_QUERY[$_POST['hostname_bool']].' ?';
		$whereItems[] = str_ireplace('*', '%', $_POST['hostname']);
	}
	if (!empty($_POST['ipaddress']))
	{
		$whereClauses[] = '`ipaddress` '.$CSS_QUERY[$_POST['ipaddress_bool']].' ?';
		$whereItems[] = str_ireplace('*', '%', $_POST['ipaddress']);
	}
	if (!empty($_POST['sys_descr']))
	{
		$whereClauses[] = '`sys_descr` '.$CSS_QUERY[$_POST['sys_descr_bool']].' ?';
		$whereItems[] = str_ireplace('*', '%', $_POST['sys_descr']);
	}

	// Now we have built our WHERE clauses, build the query.
	//

	$queryStr = 'SELECT `device_id`,`hostname`,`ipaddress`,`sys_descr` FROM `devices`';
	$i = 0;

	foreach($whereClauses as $clause)
	{
		if ($i++ == 0)
			$queryStr .= ' WHERE ';
		else
			$queryStr .= ' AND ';

		$queryStr .= $clause;
	}

	//
	// Now submit the query, passing the where items for escaping.
	//
	$res = $CS->getDb()->GetRecords($queryStr, $whereItems);

	if (!is_array($res))
		throw new Exception('Search: CSDatabase::GetRecords() returned an invalid recordset');
}
catch (Exception $e) {
	CS::Abort($e);
}

$i = 0; // Row counter

CS::PrintHtmlHeader();

?>

<body id="Main">

	<a name="top"></a><h1>Search Results</h1>
	<p>Your search returned <?php echo count($res); ?> devices.</p>
	<p><a href="javascript:history.back(1);">Back</a></p>

	<table width="100%">
	<tr class="Header"><td>Hostname</td><td>IP Address</td><td>System Description</td></tr>

	<?php foreach($res as $row): ?>
		<tr class="<?php echo (++$i % 2) ? 'Odd' : 'Even'; ?>">
			<td><a href="View.php?device_id=<?php echo $row['device_id']; ?>"><?php echo $row['hostname']; ?></a></td>
			<td><?php echo $row['ipaddress']; ?></td>
			<td><?php echo nl2br($row['sys_descr']); ?></td>
		</tr>
	<?php endforeach; ?>

	<?php if (count($res) == 0): ?>
		<tr><td colspan="3">None</td></tr>
	<?php endif; ?>

	</table>

	<p><a href="#top">Top of Page</a></p>	

</body>
</html>
Return current item: Network Configuration Store