adambedford Posted February 11, 2010 Share Posted February 11, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/ Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 if $row_Featured is fetched query result then try $row_Featured['Highlight'] => $row_Featured['highlight'] Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010852 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010864 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 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);?> Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010873 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 That dump returned this: bool(false) I don't know what that means! Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010877 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2010 Share Posted February 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010900 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1010909 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 Does anyone make anything of this? I don't get why the boolean returned false and why it isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011029 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 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'] Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011037 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011056 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011060 Share on other sites More sharing options...
adambedford Posted February 11, 2010 Author Share Posted February 11, 2010 I've tried it as 'highlighted' and it's still not working. This is really strange, everything seems to be in order and yet neither style is applied. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011081 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 u know what echo these fiew vars also echo final sql how it looks var_dump($link); echo $database_links; echo $query_limit_websites; //put it before $websites = mysql_query(....); Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011088 Share on other sites More sharing options...
adambedford Posted February 12, 2010 Author Share Posted February 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011091 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 look do u get some output between <il>..<il> I mean $row_websites['URL'] has any value? and u know what if $row_websites['URL'] is not NULL u could try rename css class highlighted into something like .hg same do with table column name Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011095 Share on other sites More sharing options...
adambedford Posted February 12, 2010 Author Share Posted February 12, 2010 Yeah I do get an output between <li></li> which is why I know the database connection and query are working - why is why this is so confusing why this doesn't work! I've also changed the name of the CSS class and still nothing. I really don't get whats going on here.... Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011098 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 I read again your some posts and I spoted this. U say "The field is called Highlighted" but in the code u use $row_Featured['Highlight'] u see no 'ed' Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011101 Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 Sader probably just solved your problem. It always turns out to be the simple typos. Anyways you can try print_r($row_Featured) to make sure all the data is there. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011102 Share on other sites More sharing options...
adambedford Posted February 12, 2010 Author Share Posted February 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011106 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 I am so fu-ing curious what's wrong with this code Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011107 Share on other sites More sharing options...
adambedford Posted February 12, 2010 Author Share Posted February 12, 2010 Yeah me too! I've been trying to figure this out all day! $row_websites['Highlight'] appears to be null, which is strange. Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011109 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 try execute this SELECT Name, URL, `Description`, Category_ID FROM links WHERE Category_ID = 2 AND Visible = 'Y' LIMIT 0, 20 with phpmyadmin any result? Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011110 Share on other sites More sharing options...
xjake88x Posted February 12, 2010 Share Posted February 12, 2010 Can you post your latest code, the whole thing (on that file)? Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011112 Share on other sites More sharing options...
adambedford Posted February 12, 2010 Author Share Posted February 12, 2010 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011113 Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 U know what, I found :D :D u not selecting that field with your query :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 Quote Link to comment https://forums.phpfreaks.com/topic/191790-setting-css-class-using-php/#findComment-1011114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.