0

I have the following code to search my database, I would like to display the # of results found and display it in the line of code below that reads "Search Results:" - so it would look something like this... Search Results: 10 matches found. And 10 would be the number of results it has found. Here is what I have so far. Everything is working great, I would just like to add the feature I have described. Any help is much appreciated.

            <body>
<div id="content">
<div class="contentbox">
    <h1>Search For A PO</h1>
            <form method="post" action="">
<table class="search">
            <tr><td> Search: <select name="type">
            <option value="po_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='po_num'){print "selected";}?>>PO Number</option>
            <option value="date" <?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='date'){print "selected";}?>>Date</option>
            <option value="vin_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='vin_num'){print "selected";}?>>VIN Number</option>
                     </select></td>
<td>for: <input type="text" name="term" /><br /></td></tr>
           <tr><td> <input type="submit" name="submit" value="Search" /></td>
<td class="date">* date format: yyyymmdd</td></tr>
        </table>    
    </form>
</div>
<hr width='100%'></hr>
    <?php
        $term = $_POST['term'];
    if ($term == '')
    {
    echo '<h2>Please enter a value</h2>';
    exit;
    }
        $search_result = '';
        if($_SERVER['REQUEST_METHOD'] === "POST")  {
        mysql_connect ("localhost", "user","pass")  or die (mysql_error());
        mysql_select_db ("podat");

        $type = mysql_real_escape_string($_POST['type']);
        $term = trim(mysql_real_escape_string($_POST['term']));
        $sql = mysql_query("select * from parts_ord where $type like '%$term%' AND date<>'0'");
}
if (! mysql_num_rows ($sql)){
echo '<h2>No Results Found</h2>';
exit;
}   

echo '<h2>Search Results:</h2>';
        while ($row = mysql_fetch_assoc($sql)){
        //var_dump($row);

echo "<br/><table class='results'>
<tr><td class='short'><B>PO Number:</B> </td><td>".$row['po_num']."</td></tr>
<tr><td><B>Vendor:</B> </td><td>".$row['vendor']."</td></tr>
<tr><td><B>Date:</B> </td><td>".$row['date']."</td></tr>
<tr><td><B>VIN Number:</B> </td><td>".$row['vin_num']."</td></tr>
<tr><td><B>Description:</B> </td><td>".$row['descr']."</td></tr>
<tr><td><B>Invoice Number:</B> </td><td>".$row['inv_num']."</td></tr>
<tr><td><B>Purchase Agent:</B> </td><td>".$row['agt']."</td></tr>
<tr><td colspan='2'><a href=\"edit.php/?id=".$row['po_num']."\">Edit</a></td></tr></table>";

                }

        ?>

Edited by Nick Evan: Fixed formatting

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by dschuett
0
<body>
<div id="content">
<div class="contentbox">
<h1>Search For A PO</h1>
<form method="post" action="">
<table class="search">
<tr><td> Search: <select name="type">
<option value="po_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='po_num'){print "selected";}?>>PO Number</option>
<option value="date" <?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='date'){print "selected";}?>>Date</option>
<option value="vin_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='vin_num'){print "selected";}?>>VIN Number</option>
</select></td>
<td>for: <input type="text" name="term" /><br /></td></tr>
<tr><td> <input type="submit" name="submit" value="Search" /></td>
<td class="date">* date format: yyyymmdd</td></tr>
</table>
</form>
</div>
<hr width='100%'></hr>
<?php
$term = $_POST['term'];
if ($term == '')
{
echo '<h2>Please enter a value</h2>';
exit;
}
$search_result = '';
if($_SERVER['REQUEST_METHOD'] === "POST") {
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("podat");

$type = mysql_real_escape_string($_POST['type']);
$term = trim(mysql_real_escape_string($_POST['term']));
$sql = mysql_query("select * from parts_ord where $type like '%$term%' AND date<>'0'");
}
$total = mysql_num_rows ($sql);
if (!$total ){
echo '<h2>No Results Found</h2>';
exit;
}

echo '<h2>Search Results: ' .$total .' matches found.</h2>';
while ($row = mysql_fetch_assoc($sql)){
//var_dump($row);

echo "<br/><table class='results'>
<tr><td class='short'><B>PO Number:</B> </td><td>".$row['po_num']."</td></tr>
<tr><td><B>Vendor:</B> </td><td>".$row['vendor']."</td></tr>
<tr><td><B>Date:</B> </td><td>".$row['date']."</td></tr>
<tr><td><B>VIN Number:</B> </td><td>".$row['vin_num']."</td></tr>
<tr><td><B>Description:</B> </td><td>".$row['descr']."</td></tr>
<tr><td><B>Invoice Number:</B> </td><td>".$row['inv_num']."</td></tr>
<tr><td><B>Purchase Agent:</B> </td><td>".$row['agt']."</td></tr>
<tr><td colspan='2'><a href=\"edit.php/?id=".$row['po_num']."\">Edit</a></td></tr></table>";

}

?>
0

That's it??? Easy as that???!!! Haha, sorry...i'm a complete newb to php. One more question would be...is there an easy way to only display a certain number of results per page, and then have a next button or page 1, 2, .... buttons to return the rest of the results? Thanks so much for your help! I'm all for learning...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.