PHP: Search Function

Discussion in 'PHP' started by ko.silence, Mar 4, 2009.

  1. #1
    Hi every one,

    Here is my code. What I would like to do is to add a "Search Function" in my website.
    how to create a search function in php wherein all the records are displayed first and there is search criteria above. When I search the record were filtered. My problem is the records were not filtered it add the search result below my table. sorry im php newbie. tnx;;
    So far i've done this:


    
    <?php
    
    function printTitle()
    {
    	return "Archived Weather Records";
    }
    
    function run($dbname, $link)
    {
    
      /* Run the query on the database through the connection */
      $query = "select * from weatherdata_archive";
      $result_array = mysql_db_query($dbname, $query, $link);
      
      print "<h2>Old weather data table</h2>";
      ?>  
      <form action="list_weather_data_old.php" method="post">
    	<p>Search By ID: <input type="text" name="searchid" size="15" maxlength="20" value="" />	
    	<input type="submit" name="submit" value="Search" /></p> <br/>	<br/>
      </form>    
      <?php
      
      
      
      print "<table border=0>
             <tr>
             <th align=left>Data ID</th>
             <th align=left>Data/time</th>
             <th align=left>Node ID</th>
             <th align=left>Outdoor Temp</th>
             <th align=left>Indoor Temp</th>
             <th align=left>Humidity</th>
             <th align=left>Wind Speed</th>
    	 <th align=left>Airpressure</th>
    	 <th align=left>Wind direction</th>
    	 <th align=left>Rainfall</th>
    	 <th align=left>Wind Chill</th>
             </tr>\n";
    
      /* While there are still rows in the result set,
           assign the current row to the array $row */
      while ($row = mysql_fetch_assoc($result_array))
      {
        print "\t<tr>\n";
        /* Print out each element in row, that is,
             print the values of the attributes */
        foreach ($row as $col_value)
        {
          print "\t\t<td>$col_value</td>\n";
        } // end for
        print "\t</tr>\n";
      } // end while
    
      print "</table>\n";
      mysql_free_result($result_array);
    }
    
    ?>
    
    Code (markup):


    What I want to do is If I put the DataID and press "Search" button, I would like it to only display 1 row that ID is exactly same as what I typed just now.

    To be honestly, I really don't know how to put the "Search Function" in my website.
    Can any help me??? I am just dummy in PHP.



    Thank You very much
    silence
     
    ko.silence, Mar 4, 2009 IP
  2. mehdiali

    mehdiali Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    according waht i understand from you

    if(!isset($_GET['id']))
    {
    show_search_form();
    }
    else
    {
    show_answer();
    }

    function show_search_form()
    {
    echo "<form method=post action=self>
    enter id <input type=text name=id><br>
    <input type=submit value=search >
    </form>
    ";
    }
    function show_answer()
    {
    $ch=mysql_connection('localhost','root','');
    mysql_select_db('db','ch');
    $query='select form TableName where id ='.$_GET['id'];
    $result=mysql_query();
    $row = mysql_fetch_array($result);
    // $name = $row['name'];
    //echo $name;
    }

    it is simple one.
     
    mehdiali, Mar 4, 2009 IP