<?php

function printcat ($thecat, $thelimit, $thepage, $search="") {
    // establish db connection
    $sql_link = mysql_connect("mysqlhost", "u70262984", "xxxxxx");
    mysql_select_db("d60244618");
?>



<?php



$numperpage = 5;

if ($thelimit == "") {
    $thelimit = "0," . $numperpage;
}


if ($thepage != "search.html") {
    $sql_cat = mysql_query("select id, typeofitem, description, yourname, email, phone, photo, active from content where typeofitem='" . $thecat . "' and active='1' order by id asc limit " . $thelimit);
    $sql_nummessages = mysql_query("select count(*) from content where typeofitem='" . $thecat . "' and active='1'");
    echo "1";
} else {
    if ($thecat == "" || $thecat=="ALL") {
        $sql_cat = mysql_query("select id, typeofitem, description, yourname, email, phone, photo, active from content where match(description) against('" . addslashes($search) . "') and active='1' order by id asc limit " . $thelimit);
        $sql_nummessages = mysql_query("select count(*) from content where match(description) against('" . addslashes($search) . "') and active='1'");
        echo "2";
    } else {
        if ($search == "") {
            $sql_cat = mysql_query("select id, typeofitem, description, yourname, email, phone, photo, active from content where typeofitem='" . $thecat . "' and active='1' order by id asc limit " . $thelimit);
            $sql_nummessages = mysql_query("select count(*) from content where typeofitem='" . $thecat . "' and active='1'");
            echo "3";
        } else {
            $sql_cat = mysql_query("select id, typeofitem, description, yourname, email, phone, photo, active from content where typeofitem='" . $thecat . "' and match(description) against('" . addslashes($search) . "') and active='1' order by id asc limit " . $thelimit);
            $sql_nummessages = mysql_query("select count(*) from content where typeofitem='" . $thecat . "' and match(description) against('" . addslashes($search) . "') and active='1'");
            echo "4";
        }
    }
}






$nummes = mysql_fetch_row($sql_nummessages);
$numresults = $nummes[0];
$thelimitarr = explode(",", $thelimit);
$curpage = ceil(($thelimitarr[0] + $numperpage) / $numperpage);
$numpages = ceil($numresults / $numperpage);

if ($thecat == "") { $thecat="ALL";}
?>


<?php if ($search=="") {echo $getcat_keys[2];} else {echo "Search Terms: (" . $thecat . ") ''" . stripslashes($search) . "''<br><br>";}?>


<table border=0 width=100%>
<?php if ($numpages > 1) { ?>
<tr>
    <Td width=50><?php if ($curpage != "1") { ?>
                    <a href="javascript: location.href='<?php echo $thepage;?>?typeofitem=<?php echo $thecat;?>&search=<?php echo $search;?>&thelimit=<?php echo $thelimitarr[0] - $numperpage;?>,<?php echo $numperpage;?>'">&lt;&lt;Previous</a>
                    <?php } ?>&nbsp;</td>
    <td align=center><?php echo "<br><b>Viewing page " . $curpage . " of " . $numpages . "</b>";?></td>
    <td width=50><?php if ($curpage != $numpages) { ?>
                    <a href="javascript: location.href='<?php echo $thepage;?>?typeofitem=<?php echo $thecat;?>&search=<?php echo $search;?>&thelimit=<?php echo $thelimitarr[0] + $numperpage;?>,<?php echo $numperpage;?>'">Next&gt;&gt;</a>
                    <?php } ?>&nbsp;</td>
</tr>
<tr>
<td colspan=3 align=center>

<?php
if ($numresults > $numperpage) {

    if ($curpage < 5) {

        for ($xxx = 0; $xxx < $numpages; $xxx++) {
            if ($xxx == 1) {
                $xxxnum = $numperpage;
            } else {
                $xxxnum = ($xxx * $numperpage);
            }

            if ($curpage - 1 != $xxx) {
                echo '<a href="' . $thepage . '?typeofitem=' . $thecat . '&search=' . $search . '&thelimit=' . $xxxnum . ',' . $numperpage . '">';
            }
                echo $xxx + 1;
            if ($curpage - 1 != $xxx) {
                echo '</a>';
            }

            if (($xxx + 1) != $numpages) {
                echo " | ";
            }
            if ($curpage + 3 > $numpages) {
                $whentostop = $numpages;
                $aretheremore = "no";
            } else {
                $whentostop = 9;
                $aretheremore = "yes";
            }
            if ($xxx == $whentostop) {
                $xxx = $numpages;
            }
        }
        if ($aretheremore == "yes") {echo " ...";}

    } else {
        if ($curpage != "5") {
            echo "... | ";
        }
        for ($xxx = ($curpage - 5); $xxx < $numpages; $xxx++) {
            if ($xxx == 1) {
                $xxxnum = $numperpage;
            } else {
                $xxxnum = ($xxx * $numperpage);
            }

            if ($curpage - 1 != $xxx) {
                echo '<a href="' . $thepage . '?typeofitem=' . $thecat . '&search=' . $search . '&thelimit=' . $xxxnum . ',' . $numperpage . '">';
            }
                echo $xxx + 1;
            if ($curpage - 1 != $xxx) {
                echo '</a>';
            }

            if (($xxx + 1) != $numpages) {
                echo " | ";
            }
            if ($curpage + 3 > $numpages) {
                $whentostop = $numpages;
                $aretheremore = "no";
            } else {
                $whentostop = $curpage + 3;
                $aretheremore = "yes";
            }
            if ($xxx == $whentostop) {
                $xxx = $numpages;
            }
        }
        if ($aretheremore == "yes") {echo " ...";}
    }



}
?>

</td>
</tr>
<?php } ?>

<tr>
<td colspan=3>

<table border=0 width=100%>
<?php







if ($numresults == "0") {
    echo "<br><br><font size=3><b>Sorry, your search returned no results.</b></font><Br><br>";
}

    while($cat_keys = mysql_fetch_row($sql_cat)) {
?>
<tr>
    <td width=100><?php if ($cat_keys[6] != "" && file_exists("/export/home/d60244618/public_html/images/content/" . $cat_keys[6])) { ?><a href="http://www.alamotrades.com/php-lib/img.php?filename=images/content/<?php echo $cat_keys[6];?>&width=900&height=900" target="_blank"><img src="http://www.alamotrades.com/php-lib/img.php?filename=images/content/<?php echo $cat_keys[6];?>&width=100&height=100" border=0><br><font size="1">Click to enlarge</font></a><?php } else { ?><img src="http://www.alamotrades.com/images/notavail.gif"><?php } ?></td>
    <td>
    <?php if ($search != "") { ?>
    <font size="3"><b><?php echo $cat_keys[1];?></b></font><br>
    <?php } ?>

    <?php echo $cat_keys[2];?>
        <br><br>
        <b>Name:</b> <?php echo $cat_keys[3];?><br>
        <b>Phone:</b> <?php echo $cat_keys[5];?><br>
        <b>Email:</b> <a href="mailto:<?php echo $cat_keys[4];?>"><?php echo $cat_keys[4];?></a><br>

    </td>
</tr>
<tr>
    <td colspan=2><hr></td>
</tr>
    <?php
    }
?>

</table>

</td>
</tr>


<?php if ($numpages > 1) { ?>
<tr>
    <Td width=50><?php if ($curpage != "1") { ?>
                    <a href="javascript: location.href='<?php echo $thepage;?>?typeofitem=<?php echo $thecat;?>&search=<?php echo $search;?>&thelimit=<?php echo $thelimitarr[0] - $numperpage;?>,<?php echo $numperpage;?>'">&lt;&lt;Previous</a>
                    <?php } ?>&nbsp;</td>
    <td align=center><?php if ($numpages > 1) {echo "<br><b>Viewing page " . $curpage . " of " . $numpages . "</b>";}?></td>
    <td width=50><?php if ($curpage != $numpages) { ?>
                   <a href="javascript: location.href='<?php echo $thepage;?>?typeofitem=<?php echo $thecat;?>&search=<?php echo $search;?>&thelimit=<?php echo $thelimitarr[0] + $numperpage;?>,<?php echo $numperpage;?>'">Next&gt;&gt;</a>
                    <?php } ?>&nbsp;</td>
</tr>
<tr>
<td colspan=3 align=center>

<?php
if ($numresults > $numperpage) {

    if ($curpage < 5) {

        for ($xxx = 0; $xxx < $numpages; $xxx++) {
            if ($xxx == 1) {
                $xxxnum = $numperpage;
            } else {
                $xxxnum = ($xxx * $numperpage);
            }

            if ($curpage - 1 != $xxx) {
                echo '<a href="' . $thepage . '?typeofitem=' . $thecat . '&search=' . $search . '&thelimit=' . $xxxnum . ',' . $numperpage . '">';
            }
                echo $xxx + 1;
            if ($curpage - 1 != $xxx) {
                echo '</a>';
            }

            if (($xxx + 1) != $numpages) {
                echo " | ";
            }
            if ($curpage + 3 > $numpages) {
                $whentostop = $numpages;
                $aretheremore = "no";
            } else {
                $whentostop = 9;
                $aretheremore = "yes";
            }
            if ($xxx == $whentostop) {
                $xxx = $numpages;
            }
        }
        if ($aretheremore == "yes") {echo " ...";}

    } else {
        if ($curpage != "5") {
            echo "... | ";
        }
        for ($xxx = ($curpage - 5); $xxx < $numpages; $xxx++) {
            if ($xxx == 1) {
                $xxxnum = $numperpage;
            } else {
                $xxxnum = ($xxx * $numperpage);
            }

            if ($curpage - 1 != $xxx) {
                echo '<a href="' . $thepage . '?typeofitem=' . $thecat . '&search=' . $search . '&thelimit=' . $xxxnum . ',' . $numperpage . '">';
            }
                echo $xxx + 1;
            if ($curpage - 1 != $xxx) {
                echo '</a>';
            }

            if (($xxx + 1) != $numpages) {
                echo " | ";
            }
            if ($curpage + 3 > $numpages) {
                $whentostop = $numpages;
                $aretheremore = "no";
            } else {
                $whentostop = $curpage + 3;
                $aretheremore = "yes";
            }
            if ($xxx == $whentostop) {
                $xxx = $numpages;
            }
        }
        if ($aretheremore == "yes") {echo " ...";}
    }



}
?>

</td>
</tr>
<?php } ?>
</table>

<?php
}

?>