Style MySQL table with CSS

How to import/include a CSS file using PHP code and not HTML code?

However, my page prints the CSS code. Note: I want to use PHP to include the CSS file, and not use I also do you want to rename my CSS file to a PHP file as some website mentioned. Any clues? Many thanks.

Do you want to have the CSS interpreted as PHP, or do you just want the page to use that CSS file in how it displays the HTML?

Your question is a bit confusing because it’s unclear why you want to do this. CSS is meant to affect how the page looks. It can only do that if the user’s browser reads the CSS and applies it to the HTML. The browser won’t be given your PHP code, so it either has to see a reference to the CSS file and be able to fetch it, or see the CSS embedded in the HTML. Which one of those do you mean to have appear in the HTML: the reference to the CSS file, or the raw CSS code? If neither, what’s the purpose of bringing in the CSS to your PHP script?

16 Answers 16

You have to surround the CSS with a tag:

PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.

This is the best solution, PHP includes don’t necessarily have to end .php . Also the tags don’t need to be echoed so could be used for better readability.

@Sarah Check the source code in the browser. Is the CSS code showing up between the

@Sarah It definitely SHOULD work — so the problem is almost surely this: when you do an include(), you need a filesystem based path, while in the document everything is web root based. Therefore, your php won’t find the CSS file. An easy fix for this is using __ DIR __ (uppercase DIR with double underscores on both sides, no spaces; a magic constant of php) that will give you the filesystem path of the file you’re in; then you only have to go relative from that.

You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?

This (or an @import) is the best way to include CSS in the page. If you use PHP to dump the contents of your CSS file into your HTML, then every time your user requests a page, they will have to download the entire contents of your CSS file along with it, as part of the HTML. However, if you use this kind of tag, the browser can request the CSS seperately, and, the next time it sees a reference to the same CSS file, it will say «oh, I already have that,» and not bother requesting it. That will make things faster for the user and will mean less work to your web server.

Читайте также:  Номер телефона ссылкой html

Источник

How to include css file in php?

I am using php file and want to include CSS file into PHP, I have included but its not working properly. The file that I am executing is in this directory:

http://host.com/at_t/application/modules/employees/view/employee_detail_view.php?id=4 
http://host.com/at_t/public/style/style.css 

Does it work if you use /at_t/public/style/style.css or the complete address http://host.com/at_t/public/style/style.css with the code you’re using.

4 Answers 4

Here You Only Write Following Code.

No this is not working.If i write click and open source code then click over the css file path then it moves to this path and file not found appears host.com/at_t/application/modules/employees/view/public/css/…

Put the following css inside of your existing html head tag. Like this. Then put css tags in php this:

Maybe you didnt put the right path to your css file?

1) First thing you have typos in your code.

According to your question css file is in style folder(http://host.com/at_t/public/style/style.css) but you are adding wrong path in link href tag (< link href ).

Your employee_detail_view.php file present in view folder(http://host.com/at_t/application/modules/employees/view/employee_detail_view.php?id=4) So if you give link href as (< link href ) then also it can't find the css file because it search only in view folder and its sub folder for css file. So use complete address in your link href tag as follows.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Include: CSS with .php file extension?

I’d like to wrap a CSS file in PHP. So I write the header for the CSS file and give it a .php file extension, thus. css.php. Will this work if the page is already being used as an include? Or will this new header clash with the frame the page is being included into?

Might I ask what you want to use this css.php file to serve? Some sort of styles that are database generated or something?

5 Answers 5

and then in top of your .css.php file:

Make sure that header is printed at very top of the file. If you have (like I did) an empty line at top it may not work somewhere!

If you have a file called css.php just make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into a bootstrap.php if you haven’t already. A quick example of loading some style info from a databse:

Читайте также:  Основы PHP и MySQL

From your other php file, just output the tag to include the css.php, you do not want to use the php include() function for this task!

Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn’t do much good. You could force that to update by adding a get parameter to the href of the link like so:

Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don’t change very often, the first example should work for you quite well.

Источник

Use CSS Style in PHP

Use CSS Style in PHP

  1. Use CSS in a PHP-Only File
  2. Use CSS in a PHP+HTML File
  3. Use Inline CSS in PHP echo Statements

This article will teach you three methods that’ll help you use CSS styles in PHP.

The first method is via a PHP-only file, and the second is to embed PHP in an HTML+CSS file. Then the third method will use inline CSS in PHP echo statements.

Use CSS in a PHP-Only File

A standard HTML file can embed CSS styles in the element or link to an external CSS file. By default, this CSS file will have the css extension, but it can also have the php extension.

This means you can write CSS code, save it as a PHP file and link it to your HTML. In this PHP file, you can do more than what you’ll do in a CSS file; you can write PHP code.

First, you can define a PHP code block with CSS property and values stored as variables. Then outside the PHP block, you can write normal CSS that’ll use the variables as values of CSS properties.

We’ve done that in the following; save it as styles.php .

php  // The "header" is the most important part of  // this code. Without it, it will not work.  header("Content-type: text/css");   $font_family = 'Trebuchet MS, Verdana, Helvetica';  $font_size = '1.2em';  $background_color = '#000000';  $font_color = '#ffffff';   // Close the PHP code block.  ?> body   background-color: ;  color: ;  font-size: ;  font-family: ; > 

Save the following HTML, and ensure you’ve linked styles.php in the tag.

 html lang="en"> head>  meta charset="utf-8">  title>Webpage using CSS styles generated with PHPtitle>   link rel="stylesheet" type="text/css" href="styles.php">  head> body>  h1>Hello, We styled this page using CSS in a PHP file!h1>  h1>How cool is that?h1>  body>  html> 

Web page styled with CSS in PHP

Use CSS in a PHP+HTML File

HTML can use CSS via the tag or the tag, which can contain PHP in a dedicated PHP block. If the PHP code generates or manipulates HTML code, the linked CSS code can style the HTML.

For example, you can style the table using CSS if PHP pulls database records to make an HTML table. To show how to do this, create a database called my_website in MySQL.

Next, create a site_users table in my_website using the following queries.

CREATE TABLE site_users (  user_id INT NOT NULL AUTO_INCREMENT,  username VARCHAR(50) NOT NULL,  email VARCHAR(100) NOT NULL,  PRIMARY KEY (user_id)) ENGINE = InnoDB; 

Insert data into the site_users table.

INSERT INTO site_users (username, email) VALUES ('user_1', 'user_1@gmail.com');  INSERT INTO site_users (username, email) VALUES ('user_2', 'user_2@gmail.com');  INSERT INTO site_users (username, email) VALUES ('user_3', 'user_3@gmail.com'); 

Now, in the following, we have HTML, PHP, and CSS. The CSS is in the element; the PHP is in a PHP block within the HTML.

The PHP creates an HTML table using records from the site_users table. When the PHP generates the table, the CSS will style it.

    /* This CSS will style the table generated by PHP. */ table < border-collapse: collapse; width: 30em; font-size: 1.2em; >table, th, td < border: 2px solid #1a1a1a; >td,th < padding: 0.5em; >/* Create a striped table. */ tr:nth-child(even) < background-color: #f2f2f2; >/* General body styles. */ body  
query("SELECT * FROM site_users")->fetch_all(MYSQLI_ASSOC); // Get keys from the first row. $table_header = array_keys(reset($site_users)); // Print the table. echo ""; // Print the table headers. echo ""; foreach ($table_header as $value) < echo ""; > echo ""; // Print the table rows foreach ($site_users as $row) < echo ""; foreach ($row as $value) < if (is_null($value)) < echo ""; > else < echo ""; > > echo ""; > echo "
" . $value . "
NULL" . $value . "
"; ?>

PHP table styled with CSS

Use Inline CSS in PHP echo Statements

PHP works well with HTML and has the echo statement that can send HTML with inline CSS to the web browser. This is useful when debugging or sending large chunks of HTML to the web browser.

The following shows you how to use inline CSS with PHP echo . We define the text and store three colors in the $colors_array .

Then we use foreach to loop through the $colors_array , and we set the array element as the value of the inline CSS. When you run the code, the text appears three times with different colors.

php  $sample_text = "We generated this text color using inline CSS in PHP.";   $colors_array = ['red', 'green', 'blue'];   foreach ($colors_array as $value)   // Use inline CSS with PHP echo.  echo "

" . $sample_text . "

"
;
> ?>

A text styled using red, green, and blue

Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.

Related Article — PHP CSS

Источник

Оцените статью