<?php require_once 'protect.php'; ?>
<?php if ( !preg_match('/^ *$/', $_REQUEST['search']) ) { ?>
<?php
$search = $mysqli->real_escape_string(stripslashes($_REQUEST['search'])); 

// split on + 
$parts = explode(" ",$search);
foreach ($parts as $section)
{
    if (!empty($querystring)) $querystring .= " AND ";
	
// split words 
    $keywords = explode(" ",trim($section));
    foreach ($keywords as $issue)
    {
    if (!empty($searchstring)) $searchstring.= " AND ";
    $searchstring .= "name LIKE '%{$issue}%' OR text LIKE '%{$issue}%' OR details LIKE '%{$issue}%' OR code LIKE '%{$issue}%' OR url LIKE '%{$issue}%'";
    }
    $querystring .= " ($searchstring) ";
    $searchstring = '';

    if (!empty($querystringc)) $querystringc .= " AND ";
	
// split words 
    $keywords = explode(" ",trim($section));
    foreach ($keywords as $issue)
    {
    if (!empty($searchstringc)) $searchstringc.= " AND ";
    $searchstringc .= "name LIKE '%{$issue}%' OR text LIKE '%{$issue}%'";
    }
    $querystringc .= " ($searchstringc) ";
    $searchstringc = '';
}
?>
<?php
$result2 = $mysqli->query("SELECT insert_id, page, name FROM sections WHERE ({$querystringc}) AND (publish='yes' OR promote!='0') AND lang_id=$lang ORDER BY priority");
$nrsections = mysqli_num_rows($result2);
while($row = $result2->fetch_array()) {
$idsc=$row['insert_id'];
$pagesc=$row['page'];
$namesc=$row['name'];
?>
<a href="<?php echo $pagesc ?>."><?php echo $namesc ?></a>
<hr>
<?php }?>
<?php
$result3 = $mysqli->query("SELECT insert_id, page, name FROM categories WHERE ({$querystringc}) AND (publish='yes' OR promote!='0') AND lang_id=$lang ORDER BY priority");
$nrcategories = mysqli_num_rows($result3);
while($row = $result3->fetch_array()) {
$idc=$row['insert_id'];
$pagec=$row['page'];
$namec=$row['name'];
?>
<a href="<?php echo $pagec ?>"><?php echo $namec ?></a>
<hr>
<?php }?>
<?php
$result1 = $mysqli->query("SELECT * FROM items WHERE (({$querystring}) 
OR insert_id IN (SELECT item_id FROM extras WHERE type=1 AND name LIKE '%{$issue}%'))
AND category_id NOT IN (SELECT insert_id FROM categories WHERE publish='no' AND promote=0)
AND publish='yes' AND lang_id=$lang ORDER BY category_id, priority");
$nritems = mysqli_num_rows($result1);
while($row = $result1->fetch_array()) {
include 'itemq.php';
if ($page=='index') {
$page="";
}
?>

<?php if ($typec==0 OR $typec==1) { ?>
<div class="cleared padbottom20">
<?php include 'info.php';?>
<?php if ($section_id==0) { ?><a href="<?php echo $page ?>">&raquo; <?php echo $category ?></a><?php } ?>
<hr class="bordercolor3">
</div>
<?php }else{?>
<?php include 'itemb.php';?>
<?php } ?>

<?php }?>
<hr>
<?php 
$nrresults = $nritems + $nrcategories + $nrsections;
?>
<?php echo $nrresults ?> <?php echo $textresults ?>
<?php }?>
<!--search sections -->
<?php if ($_REQUEST['secid']) { 
$catid = $mysqli->real_escape_string(stripslashes($_REQUEST['catid']));
$secid = $mysqli->real_escape_string(stripslashes($_REQUEST['secid']));
?>
<?php
if ($catid==0) {
$result2 = $mysqli->query("SELECT * FROM items WHERE category_id=ANY (SELECT insert_id FROM categories WHERE type=2 AND lang_id=$lang AND publish='yes') 
AND (section_id='$secid' OR section_id=ANY (SELECT insert_id FROM sections WHERE parent='$secid')) AND lang_id=$lang AND publish='yes' ORDER BY category_id, section_id, priority");
} else {
$result2 = $mysqli->query("SELECT * FROM items WHERE (category_id='$catid' OR category_id=ANY (SELECT insert_id FROM categories WHERE parent='$catid' AND type=2 AND lang_id=$lang AND publish='yes')) AND (section_id='$secid' OR section_id=ANY (SELECT insert_id FROM sections WHERE parent='$secid')) AND lang_id=$lang AND publish='yes' ORDER BY category_id, section_id, priority");
}

$nritems = mysqli_num_rows($result2);
while($row = $result2->fetch_array()) {	
include 'itemq.php';
?>
<?php include 'itemb.php';?>
<?php } ?>
<hr>
<?php echo $nritems ?> <?php echo $textresults ?>
<?php }?>