如何在另一个PHP代码中放入一个复杂的PHP代码?

时间:2012-06-03 01:13:39

标签: php mysql syntax

我需要能够将之前构建的网站的一部分放入我现在制作的另一个网站。但是,两者都在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>';
    }
   }
      };

我认识到这很复杂,但我现在在语法上迷失了,所以如果有人可以帮我一点点,那就非常感激了

2 个答案:

答案 0 :(得分: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>";

...

文档链接: The include statement includes and evaluates the specified file.

答案 1 :(得分:1)

你知道吗?您不必混淆复杂的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, <?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>';
}
}
  };

?>