PHP help needed this time :D
19-03-2004, 09:12
|
#1
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
PHP help needed this time :D
hellup, I need to include a CSS file based on a field entry in a database
Let's say the field name is test, it has 3 entries, nob, blah and oi
What I need to to do is enter the url (for example) http://www.website.com/test.php?test=oi and it'll then read the database, see that the field test is "oi" and load a relevant CSS file for "oi" ...
does that make any sense?
Cheers
edit: alternatively, what'd be easier for me to incorporate is if someone goes to http://www.website.com/test.php?test=oi it'll load oi.css and bypass the database automatically.
|
|
|
19-03-2004, 09:35
|
#2
|
|
Inactive
Join Date: Oct 2003
Location: East Midlands
Age: 48
Services: Rural BB - Radio Link via Virgin Fibre
Posts: 2,947
|
Re: PHP help needed this time :D
i've done this with cookies in the past.. it's fairly straight forward.. and avoids the need to have complicated things like user accounts
|
|
|
19-03-2004, 09:37
|
#3
|
|
Inactive
Join Date: Oct 2003
Location: East Midlands
Age: 48
Services: Rural BB - Radio Link via Virgin Fibre
Posts: 2,947
|
Re: PHP help needed this time :D
|
|
|
19-03-2004, 10:13
|
#4
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: PHP help needed this time :D
Cheers, but that wouldn't work for what I'm doing  basically let's say there's 100 customers, each uses a different stylesheet, we incorporate the page into their site so what I need to do is read the URL for determine the stylesheet, probably didn't explain it very well, still hungover. Let's say the URL is www.wherever.com/index.php?css=customer1
I want it to then incorporate "customer1.css" (which'll be uploaded manually).
|
|
|
19-03-2004, 11:16
|
#5
|
|
Inactive
Join Date: Jun 2003
Location: Lincoln, UK
Posts: 44
|
Re: PHP help needed this time :D
This will work if you don't want it to save the selection over different sessions.
PHP Code:
if (!$css) $css="default";
switch($css){
// Set default css file if none is selected in the URL.
case "default":
$css = default.css;
break;
//Modify below for each css choice.
case "css1":
if (@join ('', file ("customer1.css"))=="")
$css = default.css;
else
$css = customer1.css
break;
// Don't modify below except to set your default css file.
default:
include("default.css");
break; }
Then for the HTML part, if it's not within PHP tags, simple use
PHP Code:
<link href="<? echo $css;?>" rel="stylesheet" type="text/css">
Else, if it is...
PHP Code:
echo "<link href='$css' rel=\"stylesheet\" type=\"text/css\">"
|
|
|
19-03-2004, 11:22
|
#6
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: PHP help needed this time :D
Ah now see, I fixed it just before you posted that
I just stuck in
Code:
<?php
print "<link rel=\"stylesheet\" type=\"text/css\" href=$css.css>";
?>
and it does the job perfectly, I then just use a url like http://www.blah.com/css=whatever
|
|
|
19-03-2004, 11:24
|
#7
|
|
Inactive
Join Date: Jun 2003
Location: Lincoln, UK
Posts: 44
|
Re: PHP help needed this time :D
Yup, that works equally well.
|
|
|
19-03-2004, 11:30
|
#8
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: PHP help needed this time :D
Quote:
|
Originally Posted by Peter
Yup, that works equally well. 
|
Ah haha .. see, it's a search page for a database .. the search page creates a URL string like results.php?something=arse&somethingelse=arsetoo
need to carry the CSS details over, I populated a hidden field called "styles" with the relevant value and it carries that over in the URL but the results page doesn't know wtf "styles" is and gives me all sorts of errors, pretty sure I have to create a variable on the results page to tell the script that "styles" is something or other .. but I've no idea what
|
|
|
19-03-2004, 13:20
|
#9
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
|
Re: PHP help needed this time :D
Quote:
|
Originally Posted by Bifta
Ah haha .. see, it's a search page for a database .. the search page creates a URL string like results.php?something=arse&somethingelse=arsetoo
need to carry the CSS details over, I populated a hidden field called "styles" with the relevant value and it carries that over in the URL but the results page doesn't know wtf "styles" is and gives me all sorts of errors, pretty sure I have to create a variable on the results page to tell the script that "styles" is something or other .. but I've no idea what
|
If you basically need to get a variable from the URL just do it like this:
PHP Code:
$styles = $_GET['styles'];
echo "<link rel=\"stylesheet\" href=\"$styles\" type=\"text/css\" />";
|
|
|
19-03-2004, 13:30
|
#10
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: PHP help needed this time :D
Quote:
|
Originally Posted by Richard M
If you basically need to get a variable from the URL just do it like this:
PHP Code:
$styles = $_GET['styles'];
echo "<link rel=\"stylesheet\" href=\"$styles\" type=\"text/css\" />";
|
Still gives me this error
Code:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in xxxxxxxxxxxxxxxxxxxxxxxx
because styles=whatever is in the URL it's somehow trying to find that field in the DB by the look of it, when it can't find it it throws the above
|
|
|
19-03-2004, 13:48
|
#11
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
|
Re: PHP help needed this time :D
Quote:
|
Originally Posted by Bifta
Still gives me this error
Code:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in xxxxxxxxxxxxxxxxxxxxxxxx
because styles=whatever is in the URL it's somehow trying to find that field in the DB by the look of it, when it can't find it it throws the above 
|
I'm just guessing here but you'd need something like this:
PHP Code:
// Open SQL connection.
if (!$dbconnection) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select DB
@mysql_select_db($dbname) or die("Could not select database");
// The query for getting the $style
$result = @mysql_query("SELECT * FROM " . $table_name . " WHERE styles='" . $styles . "'");
if (!$result) {
echo("<p>Error performing query: " .
mysql_error() . "</p>");
exit();
}
// Loop through the array and echo out the $style retrieved from the DB
while ( $row = @mysql_fetch_array($result) ) {
$styles = $row['styles'];
echo "<link rel=\"stylesheet\" href=\"$styles\" type=\"text/css\" />";
}
// Close SQL connection.
mysql_close($dbconnection);
You could also use the more OOP way by using mysql_fetch_object() instead like so:
PHP Code:
// Open SQL connection.
if (!$dbconnection) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select DB
@mysql_select_db($dbname) or die("Could not select database");
// The query for getting the $style
$result = @mysql_query("SELECT * FROM " . $table_name . " WHERE styles='" . $styles . "'");
if (!$result) {
echo("<p>Error performing query: " .
mysql_error() . "</p>");
exit();
}
// Get the object from the DB and echo out the $style.
$style_object = @mysql_fetch_object($result);
$styles = $style_object->styles;
echo "<link rel=\"stylesheet\" href=\"$styles\" type=\"text/css\" />";
// Close SQL connection.
mysql_close($dbconnection);
|
|
|
19-03-2004, 14:12
|
#12
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: PHP help needed this time :D
eep .. ok, I'm going to attempt to fit that around my code, cheers for the help
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 08:18.
|