1

I am using codeigniter and any data that is within the for loop doesn't have the CSS applied. The file is pointed correctly as all the other css works fine, however I can't find a solution to this anywhere

HTML :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE = html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('/assets/css/home.css')?>">
     <title>Spill.It</title>

</head>
<body>
    <header>
    <div id="gifLogo">
        <img src="<?php echo base_url('/assets/images/homelogo.gif')?>" width="100%" alt="Spill.It Logo">
     </div>
     </header>
<!-- Foreach loop to print all messages from the user. -->
<?php foreach ($messages as $row): ?>
    <div id=messageDisplay>
    <?php if ($row->user_username == null){
        echo '<p>Nothing has been spilt here yet sorry!</p>';
        }?>
    <h1><?php echo $row->user_username;?> : </h1><p><?php echo $row->text;?></p>
    <p id="time"> Posted At :<?php echo $row->posted_at;?></p>

    </div>  
<?php endforeach ?>
</div>
</body>
</html>

CSS (I haven't included all the css just the parts within the loop as my css file is very messy) :

#messageDisplay {
    text-align: center;
    padding-left: 10em;
    padding-right: 10em;
    border: 1px solid #4099ff;
    border-radius: 6px;
    box-shadow: 0 0 8px #4099ff;
    margin: 20px;
padding: 5px;   
}
#time {
    font-size: 2px;
}
1
  • You should not use IDs for elements that repeat because IDs should be unique to one element in a document, use classes instead. Also I’m not sure where PHP is involved but if you generate your CSS with PHP, make sure you serve the CSS with the correct content-type header. Commented Oct 22, 2016 at 20:10

3 Answers 3

1

In your code you are repeating the CSS IDs. CSS ID can be used once in the whole page as IDs must be unique.

You should use classes instead.

CSS

.messageDisplay {
    text-align: center;
    padding-left: 10em;
    padding-right: 10em;
    border: 1px solid #4099ff;
    border-radius: 6px;
    box-shadow: 0 0 8px #4099ff;
    margin: 20px;
    padding: 5px;   
 }
.time {
    font-size: 2px;
 }

PHP

 <div class="messageDisplay"> </div>
Sign up to request clarification or add additional context in comments.

Comments

0

Missing quotes after id , change it to

<div id="messageDisplay">

Please use classes if you need to design more than one element using same CSS. ID's in a DOM must be unique

Comments

0

Doctype should change like this:

<!DOCTYPE html>

Loading CodeIgniter assets should be like:

href="<?php echo base_url() ;?>assets/css/home.css"

So folder structure would be:

application
assets
    - css
        - home.css
system
index.php
.htaccess

And in body:

  1. wrap id or class with ' or "

  2. check out your tag open and close (because there is extra </div> in your code)

    <body>
        <header>
            <div id="gifLogo">
                <img src="<?php echo base_url(); ?>assets/images/homelogo.gif" width="100%" alt="Spill.It Logo">
            </div>
         </header>
        <!-- Foreach loop to print all messages from the user. -->
        <?php foreach ($messages as $row): ?>
            <div id= "messageDisplay">
                <?php 
                if ($row->user_username == null)
                {
                    echo '<p>Nothing has been spilt here yet sorry!</p>';
                }
                ?>
                <h1><?php echo $row->user_username;?> : </h1><p><?php echo $row->text;?></p>
                <p id="time"> Posted At :<?php echo $row->posted_at;?></p>
            </div>  
        <?php endforeach ?>
        <!-- </div> -->
    </body>
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.