du77887 2016-06-04 20:29
浏览 18
已采纳

将不同的类名传递给PHP中的帖子

I've got a blog system on my site. The text gets inserted into a database and then selected again. I have the option to post between two types of blog posts. There are two submit buttons for each. They both execute the same code that inserts the text, because I want both blog posts to go to the same table.

 <?php
              include_once("config.php");
              $query = "SELECT * FROM pages";   
              $result = mysqli_query($mysqli, $query);        

              if (isset($_SESSION['username']))
              {   
              while($row = mysqli_fetch_assoc($result)) 
              {
              $pagetitle = $row['pagetitle'];
              $message = $row['message'];        

              echo 
                  '<article class="topcontent">                       
                     <div class="mct">
                          <h2>
                              '.$pagetitle .'</h2>
                     </div><br>
                     <p class="post-text"><br>    
                              '.$message.'</p>                          
                    </article>                                        
                  </form>
                  </div>   
                     ';}              

                 //IF ADMIN LOGGED IN, POST BLOGPOST WHEN BUTTON CLICKED
                  if (isset($_SESSION['username']))
                  {
                      echo '</nav>
                      <br><br>  <div class="blog">
                      <form id="sent" action="sent.php" method="post">Post new blog<br>
                      <input type="text" placeholder="Title" method="POST" name="pagetitle" /><br><br>
                      <textarea id="message" name="message"></textarea><br>
                      <input type="submit" name="postsent" value="Confirm" />
                      </form></div>'; 
                  }   

              //IF ADMIN LOGGED IN, POST COLLAGE WHEN BUTTON CLICKED
              if (isset($_SESSION['username']))
                  {
                      echo '</nav>
                      <br><br>  <div class="collage">
                      <form id="sent"  action="sent.php" method="post">Post new collage<br>
                      <input type="text" placeholder="Title" method="POST" name="pagetitle" /><br><br>
                      <textarea id="collage" name="message"></textarea><br>
                      <input type="submit" name="collagesent"  value="Confirm" />
                      </form></div>'; 
                  }   
      ?>

The issue is that I don't know how to pass an HTML id or class to the posts so that I can style them differently in CSS. I've tried to use a variable for the article class that is defined differently depending on which button is clicked, not as shown in the code above, it didn't work.

I simply want the class for the text that gets inserted into the database to be different. How do I do that?

EDIT:

The code that posts the data into the table.

$sql = "INSERT INTO pages (pagetitle, message) VALUES ('$_POST[pagetitle]','$_POST[message]')";
//the insertion into the table of the database 

if ($MySQLi_CON->query($sql) === TRUE) {
    echo "";
} else {
    echo "Error: ". $sql . "" . $MySQLi_CON->error;
}

Second edit:

echo '<article class="topcontent">
    <div class="mct">
    <h2> ' . $pagetitle .'</h2>
    </div><br>
    <p class="post-text"><br>'.$message.'</p>
    <div class=" . ($row[\'type\'] == 2 ? "topcontent" : "othertopcontent") . "> 
    </article>' ;
    //I want 'type 2' to be given a different article classname that's not 'topcontent'. Elsewise, it should always be posted in 'topcontent'. 
  • 写回答

1条回答 默认 最新

  • douzhangcuo2174 2016-06-05 13:29
    关注

    According to edit you made and assuming that you are fetching type field value from the database:

    if ($row['type'] == 2) {
        $className = "topcontent";
    } else {
        $className = "othertopcontent";
    }
    
    echo '<article class="topcontent">
        <div class="mct">
        <h2> ' . $pagetitle .'</h2>
        </div><br>
        <p class="post-text"><br>'.$message.'</p>
        <div class="' . $className . '"> 
        </article>' ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮