Jump to content

PHP & CSS Question?


Trium918

Recommended Posts

It can be done, but you are defeating one of the major points behind CSS files - that a file only has to be downloaded once and can be used by other pages in your site. A better practice is to write multiple classes in your CSS file for what you want to do, then change the classname dynamically with the php in your php script.

Link to comment
Share on other sites

I'm 99% sure you can NOT. What are you trying to do?

 

#left_content {
  float: left;
  width: $PHP_variable;
}
#right_content {
  float: right;
  width: $PHP_variable;
}

 

There are two columns called left_content and right_content.

I am trying to get the width for both left and right content to change when

a user switches pages. The widths are stored in MySQL database under the page

name.

 

Example:

Home.php left_content width = 370px; and right_content width = 370px;

 

The user clicks on the browse.php link and the width for both columns changes to.

 

Browse.php left_content width = 150px; and right_content width = 550px;

Link to comment
Share on other sites

If its a reasonable amount of possible widths (which it sounds like it should be), just create a number of classes in a normal CSS file. For the two examples you gave, you could call them this:

 

#left_content_370 {
  float: left;
  width: 370px;
}
#right_content_370 {
  float: right;
  width: 370px;
}

#left_content_150 {
  float: left;
  width: 150px;
}
#right_content_550 {
  float: right;
  width: 550px;
}

 

Then, in your php file, you call it this way (for example)

 

<div id="left_content_<?php echo $php_left_width_variable; ?>"></div>
<div id="right_content_<?php echo $php_right_width_variable; ?>"></div>

 

$left_width_variable and $right_width_variable are called from your database, and appended onto the end of the CSS ID name. The CSS IDs have already been set in your CSS file, so the applicable one will be attached to the <div>

Link to comment
Share on other sites

This should Work:

if(isset($_GET['page'])
{
  if($_GET['page'] == 'next')
  {
  echo'<div style="float:left width:'.$row['width_two'].'"></div>
  <div style="float:right width:'.$row['width_two'].'"></div>';
  }
else
{
  echo'<div style="float:left width:'.$row['width_one'].'"></div>
  <div style="float:right width:'.$row['width_one'].'"></div>';
}

Link to comment
Share on other sites

That will work, but its bad practice. CSS shouldn't be written inline, it should be linked to externally. Separation of markup (xhtml) design (CSS) and function (javascript). They should all be in separate files.

 

Well "best practice" seems to be in conflict with PHP and CSS so I would go with with inline CSS in this case (see elpaisa's post).

 

Especially if it's just a specific use like this.

 

You mean no way other than the solution I already gave?

what if it's a dynamic width? there could be any # of possible widths (potentially).

 

 

Link to comment
Share on other sites

How many pages can he have?

 

But even if it is purely dynamic, he would be best outputting a class dynamically in the head of his (x)html document, then applying that class to the tag at hand. Inline CSS is the worst practice as far as CSS is concerned.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.