-4

我需要能够将我以前构建的站点的一部分放入我现在正在制作的另一个站点中。但是,两者都在 php 中,并且语法存在问题(即“和”的子集太多)。我该如何输入这段代码:

        if(mysql_num_rows($result) == 0)
           {
    echo 'No categories defined yet.';
         }
          else
        {
    //prepare the table
    echo '<table border="1">
          <tr>
            <th>Category</th>
            <th>Last topic</th>
          </tr>';   

    while($row = mysql_fetch_assoc($result))
    {               
        echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';

            //fetch last topic for each cat
                $topicsql = "SELECT
                                topic_id,
                                topic_subject,
                                topic_date,
                                topic_cat
                            FROM
                                topics
                            WHERE
                                topic_cat = " . $row['cat_id'] . "
                            ORDER BY
                                topic_date
                            DESC
                            LIMIT
                                1";

                $topicsresult = mysql_query($topicsql);

                if(!$topicsresult)
                {
                    echo 'Last topic could not be displayed.';
                }
                else
                {
                    if(mysql_num_rows($topicsresult) == 0)
                    {
                        echo 'no topics';
                    }
                    else
                    {
                        while($topicrow = mysql_fetch_assoc($topicsresult))
                        echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                    }
                }
            echo '</td>';
        echo '</tr>';
    }
}

进入这段代码(我希望表格本身在 div 主目录中并在欢迎下)

    echo

      "<div id=holder>
      <div id='nav'>
      <a href='chat.php'>Chat with your friends!</a> 
      <br><br><a href='changepassword.php'>Change password</a><br><br>
      <a href='logout.php'>Logout</a> 
      </div>
      <div id='main'>
      Welcome, ".$_SESSION['user_name']."!





      </div> 
      </div>";

            //fetch last topic for each cat
                $topicsql = "SELECT
                                topic_id,
                                topic_subject,
                                topic_date,
                                topic_cat
                            FROM
                                topics
                            WHERE
                                topic_cat = " .           $row['cat_id'] . "
                            ORDER BY
                                topic_date
                            DESC
                            LIMIT
                                1";

                $topicsresult = mysql_query($topicsql);

                if(!$topicsresult)
                {
                    echo 'Last topic could not be displayed.';
                }
                else
                {
                    if(mysql_num_rows($topicsresult) == 0)
                    {
                        echo 'no topics';
                    }
                    else
                    {
                        while($topicrow =           mysql_fetch_assoc($topicsresult))
                        echo '<a href="topic.php?id=' .                     $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',           strtotime($topicrow['topic_date']));
                    }
                }
            echo '</td>';
        echo '</tr>';
    }
   }
      };

我认识到这很复杂,但我现在对语法很迷茫,所以如果有人能在这方面帮助我一点,我将不胜感激

4

2 回答 2

1

假设您可以将两个文件都存在于同一位置,您可以include将文件与文件中的表格一起使用“欢迎”消息:

echo "
<div id=holder>
  <div id='nav'>
    <a href='chat.php'>Chat with your friends!</a> 
    <br><br>
    <a href='changepassword.php'>Change password</a>
    <br><br>
    <a href='logout.php'>Logout</a> 
  </div>
  <div id='main'>
    Welcome, ".$_SESSION['user_name']."!";

    include("path_to_the_table_file.php");

  echo "
  </div> 
</div>";

...

文档链接: include 语句包含并评估指定的文件。

于 2012-06-03T01:22:01.967 回答
1

你可知道?您不必弄乱复杂的回显语法。这也可以:

<div id=holder>
<div id='nav'>
<a href='chat.php'>Chat with your friends!</a> 
<br><br><a href='changepassword.php'>Change password</a><br><br>
<a href='logout.php'>Logout</a> 
</div>
<div id='main'>
Welcome, <?php echo $_SESSION['user_name'] ?>!

<?php include("yourotherfile.php"); ?>

</div> 
</div>

<?php

        //fetch last topic for each cat
            $topicsql = "SELECT
                            topic_id,
                            topic_subject,
                            topic_date,
                            topic_cat
                        FROM
                            topics
                        WHERE
                            topic_cat = " .           $row['cat_id'] . "
                        ORDER BY
                            topic_date
                        DESC
                        LIMIT
                            1";

            $topicsresult = mysql_query($topicsql);

            if(!$topicsresult)
            {
                echo 'Last topic could not be displayed.';
            }
            else
            {
                if(mysql_num_rows($topicsresult) == 0)
                {
                    echo 'no topics';
                }
                else
                {
                    while($topicrow =           mysql_fetch_assoc($topicsresult))
                    echo '<a href="topic.php?id=' .                     $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',           strtotime($topicrow['topic_date']));
                }
            }
        echo '</td>';
    echo '</tr>';
}
}
  };

?>
于 2012-06-03T01:26:52.807 回答