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)); ?> Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/ 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)); ?> Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622509 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?? Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622515 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') Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622517 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. Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622520 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> Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622525 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)); ?> Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622527 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> Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622536 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"' : ''; Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622538 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.. Link to comment https://forums.phpfreaks.com/topic/120769-solved-cant-get-this-in-proper-format/#findComment-622541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.