Using CSS When Outputting HTML Through PHP

Questions and tutorials related to Cascading Style Sheets *all versions*

Moderators: macek, egami, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 39
Joined: Fri Sep 23, 2011 5:02 am
Location: U.S.A.

Using CSS When Outputting HTML Through PHP

Post by ehauser » Wed Oct 12, 2011 7:26 am

I have an array in PHP that displays an HTML table with populated data using AJAX. I would like the table that the code displays to be able to pull from my CSS file so it displays correctly on the other page that it loads on. The PHP code that builds the table and displays it on the other page in below...

Code: Select all

// CSS
<?php include 'http://url/web/style.css'; ?>

$republicordernumber = $_GET['order_nb'];

$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname) or die(mysql_error());

$query = "SELECT quantity1, partnumber1, legs1, color1, expstend1, colorcode1, hardware1, specialupright1, esc1, options1, df1, ";
$query .= "top1, back1, bottom1, shelf1, compartment1, upright1, partition1 FROM orderstest WHERE republicordernumber = '$republicordernumber'";

$qry_result = mysql_query($query) or die(mysql_error());


$display_string = "<table name='partsordered' id='partsordered' width='1000px' class='style3 c5' title='Parts Table'>";
$display_string .= "<tr>";
$display_string .= "<td class='style2'><strong>Qty.</strong></td>";
$display_string .= "<td class='style3 c21'><strong>Part #</strong></td>";
$display_string .= "<td class='style3'><strong>Legs</strong></td>";
$display_string .= "<td class='style3'><strong>Color</strong></td>";
$display_string .= "<td class='style3 c22'><strong>Exp. S.T. END</strong></td>";
$display_string .= "<td class='style3'><strong>C/C</strong></td>";
$display_string .= "<td class='style3'><strong>HDW</strong></td>";
$display_string .= "<td class='style3'><strong>SPL UP</strong></td>";
$display_string .= "<td class='style3'><strong>Esc Y/N</strong></td>";
$display_string .= "<td class='style3'><strong>Opt</strong></td>";
$display_string .= "<td class='style3'><strong>D/F</strong></td>";
$display_string .= "<td class='style3'><strong>Top</strong></td>";
$display_string .= "<td class='style3'><strong>Bak</strong></td>";
$display_string .= "<td class='style3'><strong>Btm</strong></td>";
$display_string .= "<td class='style3'><strong>Shlf</strong></td>";
$display_string .= "<td class='style3'><strong>CD</strong></td>";
$display_string .= "<td class='style3'><strong>Upt</strong></td>";
$display_string .= "<td class='style3'><strong>Ptn</strong></td>";
$display_string .= "</tr>";

// Populate table for each record
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<tr>";
	$display_string .= "<td class='style3'><input name='quantity1' id='quantity1' type='text' class='c23' size='4' maxlength='4' value='$row[quantity1]' /></td>";
	$display_string .= "<td class='style3'><input name='partnumber1' id='partnumber1' type='text' class='c24' size='7' maxlength='7' value='$row[partnumber1]' /></td>";
	// example of a dropdown instead of text box
	$display_string .= "<td class='style3'><select name='legs1' id='legs1' class='c25' title='Legs Needed?'>";
	$display_string .= "<option value='Y'";
	if ($row[legs1] == "y") $display_string .= " SELECTED";
	$display_string .= ">Yes</option>";
	$display_string .= "<option value='N'"; 
	if ($row[legs1] == "N") $display_string .= " SELECTED";
	$display_string .= ">No</option>"; 

	$display_string .= "</select></td>";
	//$display_string .= "<td class='style3'><input name='legs1' id='legs1' type='text' value='$row[legs1]' /></td>";
	$display_string .= "<td class='style3'><input name='color1' id='color1' class='c25' type='text' value='$row[color1]' /></td>";
	$display_string .= "<td class='style3'><input name='expstend1' id='expstend1' class='c25' type='text' value='$row[expstend1]' /></td>";
	$display_string .= "<td class='style3'><input name='colorcode1' id='colorcode1' class='c26' type='text' value='$row[colorcode1]' /></td>";
	$display_string .= "<td class='style3'><input name='hardware1' id='hardware1' class='c27' type='text' value='$row[hardware1]' /></td>'";
	$display_string .= "<td class='style3'><input name='specialupright1' id='specialupright1' class='c73' type='text' value='$row[specialupright1]' /></td>";
	$display_string .= "<td class='style3'><input name='esc1' id='esc1' class='c25' type='text' value='$row[esc1]' /></td>";
	$display_string .= "<td class='style3'><input name='options1' id='options1' class='c25' type='text' value='$row[options1]' /></td>";
	$display_string .= "<td class='style3'><input name='df1' id='df1' class='c25' type='text' value='$row[df1]' /></td>";
	$display_string .= "<td class='style3'><input name='top1' id='top1' class='c25' type='text' value='$row[top1]' /></td>";
	$display_string .= "<td class='style3'><input name='back1' id='back1' class='c25' type='text' value='$row[back1]' /></td>";
	$display_string .= "<td class='style3'><input name='bottom1' id='bottom1' class='c25' type='text' value='$row[bottom1]' /></td>";
	$display_string .= "<td class='style3'><input name='shelf1' id='shelf1' class='c25' type='text' value='$row[shelf1]' /></td>'";
	$display_string .= "<td class='style3'><input name='compartment1' id='compartment1' class='c25' type='text' value='$row[compartment1]' /></td>";
	$display_string .= "<td class='style3'><input name='upright1' id='upright1' class='c25' type='text' value='$row[upright1]' /></td>";
	$display_string .= "<td class='style3'><input name='partition1' id='partition1' class='c25' type='text' value='$row[partition1]' /></td>";
	$display_string .= "</tr>";
$display_string .= "</table>";

echo $display_string;


New php-forum User
New php-forum User
Posts: 11
Joined: Sat Dec 29, 2012 6:02 pm

Re: Using CSS When Outputting HTML Through PHP

Post by Snaek » Tue Jan 01, 2013 9:13 pm

It works for me!

See the thing is, I don't know if you're showing your whole PHP file, but it doesn't matter because I am assuming that all you have is pure PHP code in the file. what you need to do is put the php tags into html tags. like so...


Code: Select all

<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style.css" />
echo "<div id='some_div'></div>";

Code: Select all

#some_div {
	width: 200px;
	height: 50px;
	border: 1px solid #000;
And when you run this, the correct output is seen (a box 200px wide and 50px high with a solid black border around it).

Hope this helps! let me know if the problem persists.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest