2012-03-02 40 views
0

生病再試一次:/

IM建立一個博客頁面,

我想讓它顯示通過,海報或產品類別或一個月或一年,或一個職位組合。 但只有當$ _GET在url中。如果不是,他們只是去blogs.php我希望它只顯示最近的12個職位。不,我有自己的工作和自己的海報工作。

這裏是我的代碼,到目前爲止,編輯出來後一點

    <?php 


$category = mysql_real_escape_string($_GET['Category']); 
$Poster = mysql_real_escape_string($_GET['Poster']); 
$Month = mysql_real_escape_string($_GET['Month']); 
$Year = mysql_real_escape_string($_GET['Year']); 


$sql = "SELECT * FROM blog, blog_poster, blog_category WHERE blog.category = '$category' 
AND blog_poster.pid = blog.posterid 
AND blog_category.ID = blog.category 

OR posterid = '$Poster' 
AND blog_poster.pid = blog.posterid 
AND blog_category.ID = blog.category 
ORDER BY timestamp DESC LIMIT 5"; 

$result = mysql_query($sql) or print ("Can't select entries from table.<br />" . $sql . "<br />" . mysql_error()); 
while($row = mysql_fetch_array($result)) { 
    $date = date("D M d Y", $row['timestamp']); 

$title = stripslashes($row['title']); 
$entry = stripslashes($row['entry']); 
$id = $row['id']; 
$entry =substr($entry, 0, 250); 


?> 

      <div class="ribbon"> 
        <div class="wrapAround"></div> 
        <div class="tab"> 
         <span class="blogDate"><?php echo $date; ?></span> 
         <span class="blogPostInfo">Posted by <a href="#"><?php echo $row['name']; ?></a> <!--| <a href="#">0 Comments</a --></span> 
        </div> 
       </div> 
       <div class="blogPostSummary"> 
        <h1><span><b><?php echo $title; ?></b></span></h1> 
        <div class="blogPostImage"> 
         <a href='cs_blog/content.php?id=<?=$id?>'class="img"> 
         <img src="../images/content/blogs/cs_blog/date/headers/blog-post-01.JPG" width="556" height="133" alt="blog post image" /></a> 
        </div> 
        <p> 
         <?php echo $entry."..&nbsp;<br><a href='content.php?id=$id'>Read more...</a>"; ?> 
        </p> 
        <p></p> 
       </div> 



       <!-- End of Content --> 
       <?php }; ?> 

+4

'isset'有什麼問題? – Rikesh 2012-03-02 11:45:33

+0

isset看起來像是這裏的正確方法 – 2012-03-02 11:46:19

+1

很難理解你想要達到的目標。你試過什麼了?向我們展示您的代碼 - 我們可能會指出錯誤。 – 2012-03-02 11:46:35

回答

0
$conditions = array(); 
if(isset($_GET['Category'])) $conditions[] = "category='". mysql_real_escape_string($_GET['Category']) . "'"; 
if(isset($_GET['Poster'])) $conditions[] = "poster='". mysql_real_escape_string($_GET['Poster']) . "'"; 

if(!empty($conditions)) 
    $conditions = 'where ' . implode(' AND ', $conditions); 
echo 'select * from table ' . $conditions; 
+0

將嘗試這一個,謝謝:) – 2012-03-02 12:38:27

+0

工作感謝隊友,只需要一個{關於if(!空和其他結尾謝謝隊友 – 2012-03-02 13:41:23

0

試試這個

$category='DefaultCategory'; 
$poster='DefaultPoster'; 
$month='defaultMonth'; 
$month='defaultYear'; 

if(isset($_GET['Category'])) 
{ 
    $category = mysql_real_escape_string($_GET['Category']); 
} 
if(isset($_GET['Poster'])) 
{ 
    $Poster = mysql_real_escape_string($_GET['Poster']); 
} 
if(isset($_GET['Month'])) 
{ 
    $Month = mysql_real_escape_string($_GET['Month']); 
} 
if(isset($_GET['Year'])) 
{ 
    $Year = mysql_real_escape_string($_GET['Year']); 
} 

$query="insert into sampleTable(category,poster,month,year) 
values ($Category,$Poster,$Month,$Year)"; 

或者如果您已經在表中的所有字段設置默認值設計 那麼你只能插入提供的字段

相關問題