Jump to content

Setting CSS class using PHP


adambedford

Recommended Posts

I have two classes: highlighted and standard. I'm using some PHP to determine which class to used based upon a value (Y or N) in a MySQL table. The field is called Highlighted and, obviously, if it is Y then the 'highlighted' class needs to be used.

 

This is the PHP code I have at the moment and its not working :(

 


<?php $highlight = $row_Featured['Highlight'] ?>
        
        <li class="<?php 
	if ($highlight == "Y") {
		echo ("highlighted");
	} else {
		echo ("standard");
	} ?>">

 

The  $row_Featured['Highlight'] refers to the 'Featured' recordset/query that I have at the top of the page.

I'm not sure where the error is, but on loading the page, neither style class is used.

 

Anyone know where I'm going wrong?

Link to comment
Share on other sites

Thanks for your reply, I tried it and unfortunately it didn't do anything, the field in the table is actually 'Highlighted' with a capital H.

 

I'm really at a loss as to why this isn't working.

 

I forgot to mention that this occurs in a do while loop if that makes any difference. Some of the records will be highlighted and some won't.

 

 

Link to comment
Share on other sites

did you checked html output?

may be thebe a thing that u dont have included correct style.css file or u just dont have define these style classes(mistspell or other dump mistake)

 

plus u can try dump value

<?php $highlight = $row_Featured['Highlight']; var_dump($row_Featured);?>

Link to comment
Share on other sites

do while loop

 

Do while loops are almost never used because they require extra logic to make sure that there is data and to pre-fetch the first piece of data before the start of the loop.

 

If you want help with your code, you will need to post all of it from where the query is being formed through the end of your presentation code that is not working the way you expect it to.

Link to comment
Share on other sites

I've got this code at the top of the page, produced by Dreamweaver so I apologise for it being really messy!

 

$colname_websites = "-1";
if (isset($_GET['Category_ID'])) {
  $colname_websites = $_GET['Category_ID'];
}
mysql_select_db($database_links, $links);
$query_websites = sprintf("SELECT Name, URL, `Description`, Category_ID FROM links WHERE Category_ID = %s AND Visible = 'Y'", GetSQLValueString($colname_websites, "int"));
$query_limit_websites = sprintf("%s LIMIT %d, %d", $query_websites, $startRow_websites, $maxRows_websites);
$websites = mysql_query($query_limit_websites, $links) or die(mysql_error());
$row_websites = mysql_fetch_assoc($websites);

$highlight = $row_websites['Highlight'];

 

And this code in the <html> section, which is the repeat region of the <li> element and some echos to display data from the database. It's here that I've put in the If statement for selecting the right CSS style to use.

 

 

    <ul>
      <?php do { ?>
        <li class="<?php if ($highlight == "Y") {
		echo ("highlighted");
	} else {
		echo ("standard");
	} ?>"  >
        <a href="forward.php?URL=<?php echo $row_websites['URL']; ?>"><?php echo $row_websites['Name']; ?></a></li>
        <?php } while ($row_websites = mysql_fetch_assoc($websites)); ?>
    </ul>

 

Thanks for the help.

Link to comment
Share on other sites

what if change code a bit

 

//begining of the code

$colname_websites = "-1";
if (isset($_GET['Category_ID'])) 
{  
  $colname_websites = $_GET['Category_ID'];
  mysql_select_db($database_links, $links);
  $query_websites = sprintf("SELECT Name, URL, `Description`, Category_ID FROM links WHERE Category_ID = %s AND Visible = 'Y'", GetSQLValueString($colname_websites, "int"));
  $query_limit_websites = sprintf("%s LIMIT %d, %d", $query_websites, $startRow_websites, $maxRows_websites);
  
  $websites = mysql_query($query_limit_websites, $links) or die(mysql_error());
  
  echo "<ul>";
  while ($row_websites = mysql_fetch_assoc($websites))
  {
    $class = ($row_websites['Highlight'] == "Y")? "highlighted" : "standard";
    echo "<il class='$class'>".$row_websites['URL']."</il>";
  }
  echo "</ul>";
}

 

also if it doesn't work u can again try to change Camelcased indices rename to lowercased $row_websites['highlight'] and $row_websites['url']

Link to comment
Share on other sites

Hi thanks for your reply. Unfortunately it's still not working. Should I try changing Highlighted to highlighted even though the field in the table has a capital H?

 

It's just the setting the Class thats a problem and I really don't get why. I tried doing an echo of $row_websites['Featured'] and it didn't show anything, so I'm guessing that's the problematic part....but I don't understand why!

Link to comment
Share on other sites

The reason why I suggesting change indices to lowercases is from my own experience wher that was the case and fiew days ago find out funny stuf and with $_POST index names I created form with

fileds <input name='c.name'> <input name='c.title'> then after submiting form I got empty values

later I find out that my field names bacome c_name c_title so... :) PHP corrects indices names I bit

 

Well tryng to examine deeper your code...

Link to comment
Share on other sites

That returns:

NULL

pcloudhosting_link

SELECT Name, URL, `Description`, Category_ID FROM links WHERE Category_ID = 2 AND Visible = 'Y' LIMIT 0, 20

 

But I know that my database connection is OK and my query works because I've got other data on the page that works fine.

 

It just seems to be that <?php $class = ($row_websites['Highlight'] == "Y")? "highlighted" : "standard" ?> does not echo highlighted under any circumstance. And even though it echoes 'standard' all the time, the class does not get switched to .standard

Link to comment
Share on other sites

sader, unfortunately thats not it - the field in the table is called Highlight, not HighlightED. I wish it was that! :(

 

And xjake88x, print_r($row_websites) didn't return anything. (I changed it from Featured to websites as I made a mistake in my original post - I'm working on the 'websites' recordset, not the 'Featured' one.

Link to comment
Share on other sites

Yes it returns the same records that are showing on my page.

 

Neither CSS class is showing though, so I'm thinking it's a problem with $row_websites['Highlight'] although I don't see why it wouldn't work - I've used the same function quite a few times on my page (with different fields) and they work fine.

 

Even $row_websites['URL'] works, so I dont why 'Highlight' doesnt !

Link to comment
Share on other sites

U know what, I found :D :D :D :D

 

u not selecting that field with your query :D :D :D

look

SELECT Name, URL, `Description`, Category_ID FROM <= do you see that u not inetrested in field 'Highlight'

simple u can use * to select all fields

 

SELECT * FROM bla bla bla

 

such waste of time LOL :)

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.