Scooby08 Posted August 21, 2008 Share Posted August 21, 2008 Trying to add a class to the links in this printf.. I need to get this: <?=($current=='%s')?'class="current"':'';?> into this <?php printf('<a href="%s?letter=%s">%s</a>', $PHP_SELF, chr($i), chr($i)); ?> This is the closest I can get and it is still a bit off <?php printf('<a href="%s?letter=%s" ('.$current.'==%s)?'class="current"':''>%s</a>', $PHP_SELF, chr($i), chr($i), chr($i)); ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 maybe something like <?php $a_string = '<a href="%s?letter=%s" ('; $a_string .= ($current == '%s')?'class="current"':''; $a_string .= '>%s</a>'; printf($a_string, $PHP_SELF, chr($i), chr($i), chr($i)); ?> Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 21, 2008 Author Share Posted August 21, 2008 The page seems to load alright with that code, but it's still not reading the class="current" for some reason?? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2008 Share Posted August 21, 2008 have you checked to see if this is ever true? ($current == '%s') Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 your best bet, if it will work at all, will be to put the ternary operator into the variables section: <?php printf('<a href="%s?letter=%s" %s>%s</a>', $PHP_SELF, chr($i), (($current == '%s') ? 'class="current"' : ''), chr($i), chr($i)); ?> otherwise, simply use a conditional to assign a variable and concatenate that directly. again, as bluesky suggested, check if the condition will ever be true. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 21, 2008 Author Share Posted August 21, 2008 I did check that.. here is what the code looks like (I just copied the first few): <a href="?letter=A" (A == "A")>A</a> <a href="?letter=B" (A == "B")>B</a> <a href="?letter=C" (A == "C")>C</a> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 then you'll probably just have to concatenate directly. <?php $class = ($current == '%s') ? 'class="current"' : ''; printf('<a href="%s?letter=%s"'.$class.'>%s</a>', $PHP_SELF, chr($i), chr($i)); ?> Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 21, 2008 Author Share Posted August 21, 2008 Nothing seems to be showing up when trying those last couple of ways by akitchin. Here is all I get: <a href="?letter=A">A</a> <a href="?letter=B">B</a> <a href="?letter=C">C</a> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 ah, that's because you didn't literally mean %s. try this: $class = ($current == chr($i)) ? 'class="current"' : ''; Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 21, 2008 Author Share Posted August 21, 2008 Somebody give this guy a raise!! haha Worked that time! thanks.. Quote Link to comment 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.