0

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
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>
1
Contributor
2
Replies
31
Views
3 Years
Discussion Span
Last Post by kelley82879
0

I tried to change to this:

SELECT 0 AS resultset, c . * 
FROM  `Cater Gids` c
WHERE Cater LIKE  '%a%'
UNION SELECT 1 , f. * 
FROM  `Cater Food` f
WHERE Cater LIKE  '%a%'
ORDER BY resultset

and also tried this:

$result .= "<table border='1'>";
$result .="<tr><td>Cater</td><td>Part</td><td>Gids</td></tr>";
while ($row = mysql_fetch_assoc($sql) && $row['resultset'] === 0) {
    $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){
    $result .= '<tr>';
    $result .= '<td>'.$row['Cater'].'</td>';
    $result .= '<td>'.$row['Part'].'</td>';
    $result .= '<td>'.$row['Gid'].'</td>';
    $result .= '</tr>';
    $row = mysql_fetch_assoc($sql);
}
$result .= "</table>";

but then it didnt return any results

This question has already been answered. 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.