显示表中的信息,然后显示另一个表中的相关信息

时间:2014-05-28 21:52:14

标签: php mysql

您好我有2张桌子。

一个是由联系信息(帐户,姓名,电子邮件,地址,城市,州,邮政编码,标签)组成的表(nameinfo)

表二是事件表(事件)(电子邮件,event_type,event_name,日期,关键字)

这是我想要做的......

我想根据匹配的电子邮件地址显示此人前往的所有活动的列表。我想像这样返回结果......

nameinfo person 1
事件人1去了

nameinfo person 2
 事件人2去了

nameinfo person 3
 事件人3去了

     $query =  mysql_query("SELECT * FROM nameinfo WHERE account='$a'");

     while($row = mysql_fetch_array($query))


    { 
     $i++;

        echo "<div>";
        echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td  style='text-align:left;'
        >";
       echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
        echo "</td>";
        echo "<td  >";
        echo $row['target'];
        echo "</td><td >";
        echo $row['lname'];
        echo "</td>";
        echo "<td >";
        echo $row['fname'];
        echo "</td><td >";
        echo $row['title'];
        echo "</td>";
        echo "<td >";
        echo $row['company'];
        echo "</td><td >";
        echo $row['address'];
        echo "</td>";
        echo "<td >";
        echo $row['address2'];
        echo "</td><td>";
        echo $row['city'];
        echo "</td>";
        echo "<td>";
        echo $row['state'];
        echo "</td><td>";
        echo $row['zip'];
        echo "</td>";
          echo "<td>";
        echo $row['email'];
        echo "</td><td>";
        echo $row['officenum'];
        echo "</td>";
          echo "<td>";
        echo $row['cellnum'];
        echo "</td>";
          echo "<td>";
        echo $row['date'];
        echo "</td><td>";
        echo $row['rep'];
        echo "</td></tr></table></div>";
        echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
        **echo THIS IS WHERE I WOULD LIKE TO PUT THE EVENT INFORMATION BUT HAVE NO IDEA HOW TO DO IT.;**
        echo "</div>";
    }    

<script>
$("button.toggles").click(function() {
       var value = $(this).attr('id');
       $("#show-" + value).toggle();
}); 
</script>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在循环内运行另一个查询。在这里,我正在调用一个函数来完成这项工作:

$i=0;
$link = ""\\your database connection
$query =  mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query)){ 
    $i++;

    echo "<div>";
    echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td  style='text-align:left;'>";
    echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
    echo "</td>";
    echo "<td  >";
    echo $row['target'];
    echo "</td><td >";
    echo $row['lname'];
    echo "</td>";
    echo "<td >";
    echo $row['fname'];
    echo "</td><td >";
    echo $row['title'];
    echo "</td>";
    echo "<td >";
    echo $row['company'];
    echo "</td><td >";
    echo $row['address'];
    echo "</td>";
    echo "<td >";
    echo $row['address2'];
    echo "</td><td>";
    echo $row['city'];
    echo "</td>";
    echo "<td>";
    echo $row['state'];
    echo "</td><td>";
    echo $row['zip'];
    echo "</td>";
      echo "<td>";
    echo $row['email'];
    echo "</td><td>";
    echo $row['officenum'];
    echo "</td>";
      echo "<td>";
    echo $row['cellnum'];
    echo "</td>";
      echo "<td>";
    echo $row['date'];
    echo "</td><td>";
    echo $row['rep'];
    echo "</td></tr></table></div>";
    echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
        getEvent($row['email']);//Pass the email to the function here
    echo "</div>";
}    

function getEvent($email){  
    global $link;       
    $evant = "";
    $query =  mysql_query("SELECT * FROM events WHERE email='$email'", $link);
    while($row = mysql_fetch_array($query)){    
        $event = $event . $row["event_name"] . "<br />";
        //Format this however you wish to format
    }   
    return $event;
}

<script>
$("button.toggles").click(function() {
       var value = $(this).attr('id');
       $("#show-" + value).toggle();
}); 
</script>