Jump to content

CSS with PHP


Matt B

Recommended Posts

Hi,

 

I have read about PHP and how to use it and I have read about CSS and how to use it. But I have not read about how to use CSS with PHP!?

 

For example, if I use an array in PHP and then add coding to the array for PHP purposes where does the CSS come in? Does CSS normally appear within PHP coding? I am guessing my CSS file contains all my CSS coding and then the CSS styling and positioning is called using div's (which call the CSS from the web page)... but does this coding get inserted in between the PHP coding...or do designers keep PHP and CSS separate.

 

Furthermore, so far I have only added PHP to the top of a PHP file. Is it possible to add PHP code followed by CSS code followed by PHP code followed by CSS code, for example? I just thought when I access my Database using MYSQL QUERY at the top of the page maybe all the PHP needs to be at the top of the page. But then how does the CSS relate back to the top of the page?

 

Hopefully someone has experience and knows the best-practice?

 

Matt.

Link to comment
Share on other sites

Matt, It is HTML that writes the pages your see on the web. It is the HTML that uses the CSS files to style the code that it produces.

 

PHP can be used for a lot of things but mostly it communicates with your site database and writes variables into your HTML code(over simplification). How all of this works: HTML writes a line of code = <p class="red">Hi, My name is Fred. What's yours?</p>. This line uses the CSS to color the line in red type to make it stand out. The CSS code is: .red{color:red;}

 

The HTML either has the styles in it's HEADER or calls a file that has the code. Calling is normally done.

 

How PHP works with this: When you enter your name it is sent to a PHP file (this file also contains HTML code, the php is normally the first part of the file followed by the HTML. The PHP part is given the name and stores it (DB communication), places it into a variable and in the HTML code we see something like this:

 

<p>Hi <?php echo $user_name; ?>, glad to meet you.</p>.

 

This is simple but I hope it helps. If you need clearer examples just ask.

 

 

 

 

Link to comment
Share on other sites

Hello again.

 

The PHP code is not complete but you should get the idea of it. It is a left navigation bar whereby the category you click gets listed first (at the top) in the navigation with a submenu below. Followed by the remaining categories. A couple of the categories have a number of sub categories (more depth) in the navigation bar.

 

How would CSS style the main menu links different to the submenu links? Would I just use the code below with <?php and ?> breaking up the code with bits of CSS styling/coding? If the php is broken up will it know where is 'last finished' and where it is 'carrying-on-from'?

 

I do not want the same styling for the whole navigation bar!

 

Code is below.

 

Matt.

 

 

$res = mysql_query("SELECT * FROM categories");

while($row = mysql_fetch_array($res)) {

  $cats[$row['categoryid']] = $row['categoryname'];

}

if(isset($_GET['category'])) {

$selcat = $_GET['category'];

}

if(isset($_GET['product'])) {

$prod_info = mysql_fetch_array(mysql_query("SELECT * FROM products WHERE productid = ".$_GET['product']));

//Prod_Info now gets stored for use in the main display.

$selcat = $prod_info['categoryid'];

}

if(isset($_GET['subcategory'])){

$option_info = mysql_fetch_array(mysql_query("SELECT * FROM products WHERE subcategoryid = ".$_GET['subcategory']));

$selcat1 = $option_info['categoryid'];

}

if(isset($_GET['subcategory1'])){

$option_info1 = mysql_fetch_array(mysql_query("SELECT * FROM products WHERE subcategory1id = ".$_GET['subcategory1']));

$selcat2 = $option_info1['subcategoryid'];

}

if(isset($selcat)) {

  echo "<a href='categorylist.php?category=".$selcat."'>".$cats[$selcat]."</a>";

  unset($cats[$selcat]); //Gets rid of it for later.

  $res = mysql_query("SELECT productid,name FROM products WHERE categoryid = ".$selcat);

  while($row = mysql_fetch_array($res)) {

  echo "<a href='product.php?product=".$row['productid']."'>".$row['name']."</a>";

  }

}

if(isset($selcat1)) {

  $smh = mysql_query("SELECT subcategoryid,name FROM products WHERE subcategory1id = ".$selcat1);

  echo "<a href='categorylist.php?category=".$selcat1."'>".$cats[$selcat1]."</a>";

  unset($cats[$selcat2]); //Gets rid of it for later.

  $res = mysql_query("SELECT subcategoryid,name FROM products WHERE subcategory1id = ".$selcat1);

  while($row = mysql_fetch_array($res)) {

  echo "<a href='subcategory1id.php?subcategory1=".$row['subcategoryid']."'>".$row['name']."</a>";

    }

}

if(isset($selcat2)) {

  $smh = mysql_query("SELECT name FROM products WHERE subcategoryid = ".$_GET['subcategory1']);

  while($row = mysql_fetch_array($smh)){

  echo "<a href='categorylist.php?category=".$row['name']."'>".$row['name']."</a>";

  unset($cats[$selcat2]); //Gets rid of it for later.

  }

 

  $res = mysql_query("SELECT subcategory1id,name FROM products WHERE subcategoryid = ".$selcat2);

  while($row = mysql_fetch_array($res)) {

  echo "<a href='subcategory1id.php?subcategory1=".$row['subcategory1id']."'>".$row['name']."</a>";

  }

}

 

 

foreach($cats AS $key => $cat)  {

  echo "<a href='categoryview.php?category=".$key."'>".$cat."</a>";

}

Link to comment
Share on other sites

please if you have code, place them in code tags!

 

Also if you read about css, you probably would have read on page 1 or 2 of that book. that css is all about selecting html elements, by using selector's #id's and .classes.

 

So the moment you print or echo something you produce html. That is where you could set a class or id or leave it be. For instance by giving every second row a class of .even

 

Than in an external style sheet (don't use inline style) you can target that element by using it's class, id or general selector

 

Hope this helps! may I ask what books you read?

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.