2012-06-03 47 views
-4

我需要能夠把我以前建立的網站的一部分放入另一個網站即時製作。然而,無論是在PHP和有問題的語法(即過多的子集「和「),我怎樣才能把這個代碼:如何將一個複雜的php代碼放入另一個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>'; 
    } 
} 

這個代碼(我想表本身在裏面股利主的歡迎下)

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>'; 
    } 
    } 
     }; 

我認識到這是相當複雜的,但我很迷失在這一點上,語法,所以如果有人可以幫助我在此ITD有點不勝感激

+2

警告你的代碼可能容易受到sql注入攻擊。 –

+0

我知道了,即時執行sha1和/或鹽後,我知道這 – ryno

+3

「sha1和/或鹽」與SQL注入沒有任何關係。 – ceejayoz

回答

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

你知道嗎?您不必混淆複雜的回聲語法。這也可以工作:

<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>'; 
} 
} 
    }; 

?> 
相關問題