how to add style effect for elements inside of echo using classes ?

76
March 29, 2017, at 10:40 PM

i have a problem with adding style effect for div elements inside of echo ,here's my code :

<?php
$db= new PDO('mysql:host=localhost;dbname=myDataBase;charset=utf8','user','password');
$response=$db->query('select * from users order by ID desc limit 10');
while($line=$reponse->fetch())
{
    echo 
        '<div class="elements" ><h1>'.$line['pseudo'].'</h1></div>
        <div class="elements">'.$line['message'].'</div>
        </div>';
} ?>

and the stylesheet :

.elements{
display: inline-block;
padding: 10px;
    }

when i execute it,it doesn't work

thanks for your collaboration

Answer 1

This is pretty easy. Just inline your php as needed into the html code to display on the page. For Example:

<?php
Inlcude your php and queries here in this block 

?>
<!DOCTYPE html>
    <html>
    <head>
        <title>Test Page</title>
        <link rel="stylesheet" href="css/styles.css" />
    </head>
    <body>
        <?php echo '<div class="elements">the rest of your php code goes here</div> '; ?>
    </body>
</html>

This is just bare bones basic code to show you what I am talking about. So your custom css would live in the styles.css file. Once you echo out what it is you want in php, then it will inherit those styles. You can inline php as much as you want throughout the file. Just name it file.php or whatever you want.

This will allow you to do what it is you are asking. Of course you could reverse this and echo out the html from start to finish if you really wanted too, but it would be easier to just add the relevant connection info for php and query at the top of the page before any html and then echo out the specific information you wish inside the html below the main php code.

Also I noticed you have one too many closing div tags at the bottom of your echo statement. As others have already commented, you have some other errors as well.

READ ALSO
I am gettingSQLSTATE[HY093]: Invalid parameter number: parameter was not defined

I am gettingSQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Hi I am trying to create a table, insert information into database and display it in sql with multiple checkboxes and emailI am new in php and It giving me this error but I don't know how to fix it

67
Laravel: Perform where using multiple tables

Laravel: Perform where using multiple tables

I'm using the Eloquent ORM that Laravel provides and I want to perform a where over two tablesI have 3 models

52
File upload error when checking if empty

File upload error when checking if empty

I am trying to check if the user is uploading a file and if he does check if the file is OKIf he doesn't upload a file ignore that field and move on to the rest of the fields

86
What is API ? how can i write api [on hold]

What is API ? how can i write api [on hold]

can anybody share best tutorial link to learn API with json? i need to communicate with android app and php application

74