SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search Fuction Problem

    I really hope you can help me with this I posted this up on the other PHP Forum, but haven't got any replies I jus hope someone in the advanced forum could help me to solve this problem.

    this is the whole search code I have created so far

    PHP Code:
    <form name="search" action=<?php Print $_SERVER["PHP_SELF"]; ?> method="post">
                <br>
                 <hr width=100%>
                <?php

                $Order 
    = (isset($_POST['Order']))?$_POST['Order']:"tax_id";
                
    $InOrder = (isset($_POST['InOrder']))?$_POST['InOrder']:"";
                
    $taxref = (isset($_POST['taxref']))?$_POST['taxref']:"";

                
    ?> 
                <div align="center" class="style14"><strong>You have to choose One means of search either:
                <br>
                </strong></div>
                <table align=center>
                      <th align=center><span class="style7">Search For  Tax Reference Number: </span>
                        <td><input type ="text" name="taxref" value ="<?print $taxref;?>"size="40">
                </table>
                <br>
                
                
                <table align=center>
                    <td> <span class="style13">Make Search Found Ascending Or Descending:</span>                  
                    <select name="InOrder">
                           <option value="ASC"<?if ($InOrder == "ASC"){print ' SELECTED ';}?>>Ascending</option>
                           <option value="DESC"<?if ($InOrder == "DESC"){print ' SELECTED ';}?>>Descending</option>
                         </select>
                   </table>
                <br>
                <table align=center>
                <th align=center>
                  <td><input type="submit" name="submit" value="Search" />
                </table>
                <br>
                
                <hr width=100%>            
                
                <?

            
    //May not be needed depending on the connection.

                
    $conn mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
                    
    mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

            
    //build up query using each conditional - else statement refers to search field.

                
    $conditional "";

                if (
    $taxref ==""){
                
    $conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
                }
                
                
    $conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

                    
    $query "SELECT tax_id, c_registration_no, start_date, tax_reference_number FROM tax ORDER BY $Order $InOrder";
                    
    $search mysql_query($query,$conn);

            
    $NumRows=mysql_num_rows($search);
            
    ?>
                <center>
                  <b></strong><?print "There are $NumRows found items relate to your search ";?></b>
                  <span class="style3"><span class="style3 style5"></span></span>            
                </center>
                <br>
                <table border="0" cellpadding="0" cellspacing="0" align="center">
              <tr height="20">
                <td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Tax ID </strong></span></td>
                <td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Company Registration Number</strong></span></td>
                <td bgcolor="#93B0B4" width="145"><span class="style14 style4"><strong>Tax Start Date (YYYY-MM-DD)</strong></span></td>
                <td bgcolor="#93B0B4" width="145"><span class="style4 style14"><strong>Tax Reference Number</strong></span></td>
                <td bgcolor="#93B0B4" width="125"><span class="style14 style4"><strong>View Client</strong></span></td>
              </tr>
              <tr>
                <td>
                <?php
        
    if($NumRows>0){

        
    //get the first row and begin the table.

        
    $row mysql_fetch_assoc ($search);

                
    //reset the result set back to the first row and display the data set.
                
    mysql_data_seek ($search0);
                while (
    $row mysql_fetch_row ($search)) {
                
                echo 
    "<tr valign=top>";
                echo 
    "<td bgcolor=#93B0B4 colspan=6></td>";
                echo 
    "</tr>";
                
                print 
    "<TR>\n";
                print 
    "<TD>$row[0]</TD>";
                print 
    "<TD>$row[1]</TD>";
                print 
    "<TD>$row[2]</TD>";
                print 
    "<TD>$row[3]</TD>";
                if(
    $row[3]=="taxrefno"){
                
                
    ?>
                </td>
              </tr>
            </table>
                <?php
                
    print $row[0];
                
    ?>
                <?php
                
    print "</TR>";
                
                    }
                else{
                
                
    ?>
              </form>
            </blockquote>
        <td><a href ="viewtax2.php?id=
                <?php
                
    print $row[0];
                
    ?>
                ">View</a></td>
        <?php
                
    print "</tr>";
                }
                }
                print 
    "</table>";
                }
                
    ?>

    but I think the main problem lies between this part of the code
    PHP Code:
    <?php

                $Order 
    = (isset($_POST['Order']))?$_POST['Order']:"tax_id";
                
    $InOrder = (isset($_POST['InOrder']))?$_POST['InOrder']:"";
                
    $taxref = (isset($_POST['taxref']))?$_POST['taxref']:"";

                
    ?> 
                <div align="center" class="style14"><strong>You have to choose One means of search either:
                <br>
                </strong></div>
                <table align=center>
                      <th align=center><span class="style7">Search For  Tax Reference Number: </span>
                        <td><input type ="text" name="taxref" value ="<?print $taxref;?>"size="40">
                </table>
                <br>
                
                
                <table align=center>
                    <td> <span class="style13">Make Search Found Ascending Or Descending:</span>                  
                    <select name="InOrder">
                           <option value="ASC"<?if ($InOrder == "ASC"){print ' SELECTED ';}?>>Ascending</option>
                           <option value="DESC"<?if ($InOrder == "DESC"){print ' SELECTED ';}?>>Descending</option>
                         </select>
                   </table>
                <br>
                <table align=center>
                <th align=center>
                  <td><input type="submit" name="submit" value="Search" />
                </table>
                <br>
                
                <hr width=100%>            
                
                <?

            
    //May not be needed depending on the connection.

                
    $conn mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
                    
    mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

            
    //build up query using each conditional - else statement refers to search field.

                
    $conditional "";

                if (
    $taxref ==""){
                
    $conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
                }
                
                
    $conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

                    
    $query "SELECT tax_id, c_registration_no, start_date, tax_reference_number FROM tax ORDER BY $Order $InOrder";
                    
    $search mysql_query($query,$conn);

            
    $NumRows=mysql_num_rows($search);
            
    ?>
    basically it is not responding to the "search" button at its just printing out all the infor mation in the "tax" table regardless what is typed in the search field hope someone can help me with this coz it s really frustrating me

    basically I have a search field and search button and if a user types into the search field and the search button is clicked if it matches the name it will show the realted information on the table but if no matches it will just say to items found but at the moment it shows all the information from the "tax" table with out the user entering anything and if the uer enters the reference number into the field and it matches the reference number in the database, instead of it just printing the reference number corresponding to the one that is type in it will not update the table it will still show all the data in the tax table instead. This goes the same for when the reference number doesn't match, instead of it saying no items found it continues to show all the data from the tax table. Basically the reference number typed into is not responding the the submit button I don't know why I rellly hope you or anybody can help I have tried everything and I just can seem to find away to solve this problem

    Thank you for your time

    Hopefully you can help me to sort this problem out thank you again

    Takecare

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    India
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay. I see a variable called $conditional which has the "where clause". But where is it appended to your main query?

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps replace
    PHP Code:
    $conditional "";

                if (
    $taxref ==""){
                
    $conditional=$conditional."tax_reference_number = '".$taxref."' OR ";
                }
                
                
    $conditional="WHERE ".substr($conditional,0,strlen($conditional)-4); 
    with something like
    PHP Code:
    $where_clauses = array();
    if (
    $taxref) {
      
    $where_clauses[] = "tax_reference_number = '".$taxref."'";
    }
    $where = (count($where_clauses)) ? 'WHERE '.implode(' OR ',$where_clauses') : ''; 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •