#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Search 2 tables and display 2 tables


    How can I change this so that my returned results will display as 2 tables instead of one?
    Its returning results from 2 different tables but its not displaying as 2 different tables As I want it to. Thanks for any help or tips you can provide.

    PHP Code:
    <?php
    mysql_connect ("localhost", "","")  or die (mysql_error());
    mysql_select_db ("");
    if(isset($_POST['submit']) && !empty($_POST['submit'])){
        $result = ""; //USED LATER
        /*$term = $_POST['term']; THIS WORKS BUT FOR SECURITY ISSUES USE:*/
        $term = mysql_real_escape_string($_POST['term']);//AVOID MYSQL INJECTION
        $sql = mysql_query("SELECT * FROM `Cater Gids` where Cater like '%$term' UNION SELECT * FROM `Cater Food` where Cater like '%$term'")  ;
         if (mysql_num_rows($sql) <= 0) {
            // no results
            //echo 'No results found.'; BETTER ECHO LATER
            $error = "No result found";
        } else if ($term ="") {
            $error = "No name entered!";
        } else {
            $result .= "<table border='1'>";
            $result .="<tr><td>Cater</td><td>Part</td><td>Gids</td></tr>";
            while ($row = mysql_fetch_array($sql)){
                $result .= '<tr>';
                $result .= '<td>'.$row['Cater'].'</td>';
                $result .= '<td>'.$row['Part'].'</td>';
                $result .= '<td>'.$row['Gid'].'</td>';
                $result .= '</tr>';
            }
            $result .= "</table>"; 
            $result .= "<table border='1'>";
            $result .="<tr><td>Cater</td><td>Dish</td><td>Gids</td></tr>";
            while ($row = mysql_fetch_array($sql)){
                $result .= '<tr>';
                $result .= '<td>'.$row['Cater'].'</td>';
                $result .= '<td>'.$row['Dish'].'</td>';
                $result .= '<td>'.$row['Gid'].'</td>';
                $result .= '</tr>';
            }
            $result .= "</table>";
        }
         mysql_close();
    }
    ?>
    <html>
        <head>
            <title>FIND YOUR GID</title>
        </head>
        <body color="" bgcolor="#634673" text="white"><font size="7"></font>
    <center><font size="7"> </font><br><br><br><br>
    <h1> <center>MORE DATA COMING SOON! </center></h1>
    <h2> <center>ENTER NAME OF CATER! </center></h2>
        <form action="caterfood.php" method="post">
         Search: <input type="text" name="term" /><br />
        <input type="submit" name="submit" value="Submit" />
        </form>
        <div name="Results">
        <?php
        if(isset($error)){echo $error;}
        if(isset($result)){echo $result;}
        ?>
        </div>
        </body>
    </html>
    <script type="text/javascript">
    var arrRequiredFields = [ "term" ];
    window.onload = function() {
       document.forms[0].onsubmit = function() {
          for (var i = 0; i < arrRequiredFields.length; i++) {
             var field = document.forms[0].elements[arrRequiredFields[i]];
             if (field && field.value.length == 0) {
                alert("Missing Name of Food");
                field.focus();
                return false;
             }
          }
          return true;
       };
    };
    </script>
    Last edited by Winters; October 7th, 2013 at 12:23 PM. Reason: Added highlighting.
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    108
    Rep Power
    4
    In general you need to duplicate the query itself and the code displaying the results. Then modify the first part to display only the first table and modify the second part to display only the second table.

    The query itself:

    $sql = mysql_query("SELECT * FROM `Cater Gids` where Cater like '%$term' UNION SELECT * FROM `Cater Food` where Cater like '%$term'") ;

    should be modified to remove the UNION [ie]:

    $sql1 = mysql_query("SELECT * FROM `Cater Gids` where Cater like '%$term'") ;

    and

    $sql2 = mysql_query("SELECT * FROM `Cater Food` where Cater like '%$term'") ;

    As a side issue, the mysql interface is deprecated. I suggest that you use mysqli instead.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    thanks that did it!
    How would I go about using mysqli instead of mysql?
  6. #4
  7. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    108
    Rep Power
    4
    Originally Posted by dogk9
    thanks that did it!
    How would I go about using mysqli instead of mysql?
    See http://php.net/manual/en/book.mysqli.php
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    Another thing when I search for data it wants exact before it gives me result even thought I have it as like '%$term'
    Why would it be doing that?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0
    Never mind I found my error.. was missing a %

IMN logo majestic logo threadwatch logo seochat tools logo