ZoEM
ZoEM

Reputation: 852

Passing different classnames to posts in 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'. 

Upvotes: 0

Views: 67

Answers (1)

smozgur
smozgur

Reputation: 1812

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

Upvotes: 1

Related Questions