Monkuar Posted February 11, 2012 Share Posted February 11, 2012 I need to wrab <b> tags around the current Letter (If they clicked on it) $letters = range('A', 'Z'); $menu=(empty($menu))?'':$menu; $menu.='<center><div class=pages>Starts With: '; foreach ($letters as $letter) { $menu .= '<a href="?a=(^_-)&muffin='. $letter .'">'. $letter .'</a>'." • "; } $menu.= '</div></center>'; if ( isset( $_GET['muffin'] ) AND in_array($_GET['muffin'], $letters ) ){ // CONNECT TO DATABASE AND QUERY $q_extra .= " AND m.name LIKE '".mysql_escape_string($_GET['muffin'])."%'"; // DO QUERY AND CHURN OUT RESULTS } I tried if ($_GET['muffin'] == $letter){ $boldtag1 = '<b>'; $boldtag2= '</b>'; } I tried that inside my foreach and it's not working, it shows the bold stuff all over, not around the current selection ? Anyone can chime on this one for me? ty Current selection I mean around the '.$letter.' so it looks nice and bolded if clicked on Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/ Share on other sites More sharing options...
MMDE Posted February 11, 2012 Share Posted February 11, 2012 Try to remove this from the anchor (link) "a=(^_-)". I'm not sure if it likes that "^", could be wrong though. Read your source code too... and try to print out something where it does something you wonder if really goes to or not, like inside the code under if ($_GET['muffin'] == $letter){ $boldtag1 = '<b>'; $boldtag2= '</b>'; } This inside the for each loop would do nothing, because I don't see you use $boldtag1 or $boldtag2 in the code anywhere else. You miss a lot of code so it's kind of hard to help you. In any case, I would strongly recommend you to put this into your code: echo $_GET['muffin']; echo $_GET['a']; Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/#findComment-1316888 Share on other sites More sharing options...
Monkuar Posted February 11, 2012 Author Share Posted February 11, 2012 $letters = range('A', 'Z'); $menu=(empty($menu))?'':$menu; $menu.='<center><div class=pages>Starts With: '; $_GET['muffin']=(empty($_GET['muffin']))?'':$_GET['muffin']; foreach ($letters as $letter) { if ( isset( $_GET['muffin'] ) AND in_array($_GET['muffin'], $letters ) ){ $test = "hey"; } $menu .= '<a href="?a=(^_-)&muffin='. $letter .'">'. $letter .' '.$test.'</a>'." • "; } $menu.= '</div></center>'; if ( isset( $_GET['muffin'] ) AND in_array($_GET['muffin'], $letters ) ){ // CONNECT TO DATABASE AND QUERY $q_extra .= " AND m.name LIKE '".mysql_escape_string($_GET['muffin'])."%'"; // DO QUERY AND CHURN OUT RESULTS } I know this sounds childish but $test is being called for EACH forloop? when I only want it to be next to the letter that is matches.. so then I can use the bold tags around the '.$letter.' so then it will show the user if they clicked to sort by "A" or whatever, it would show that letter in a Bold color. Im getting 26 words of "HEY" when im only needing 1.. Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/#findComment-1316891 Share on other sites More sharing options...
Drummin Posted February 11, 2012 Share Posted February 11, 2012 Not sure if this helps you out but a few simple IF conditions does the job. <?php $letters = range('A', 'Z'); $menu=(empty($menu))?'':$menu; $menu.='<center><div class=pages>Starts With: '; foreach ($letters as $letter) { $menu .= '<a href="?muffin='. $letter .'">'; if (isset($_GET['muffin']) && $_GET['muffin'] == $letter){ $menu .="<b>$letter</b>"; }else{ $menu .="$letter"; } $menu .= '</a>'; if ($letter!="Z"){ $menu .=" • "; } } $menu.= '</div></center>'; //echo "$menu"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/#findComment-1316894 Share on other sites More sharing options...
Monkuar Posted February 11, 2012 Author Share Posted February 11, 2012 Not sure if this helps you out but a few simple IF conditions does the job. <?php $letters = range('A', 'Z'); $menu=(empty($menu))?'':$menu; $menu.='<center><div class=pages>Starts With: '; foreach ($letters as $letter) { $menu .= '<a href="?muffin='. $letter .'">'; if (isset($_GET['muffin']) && $_GET['muffin'] == $letter){ $menu .="<b>$letter</b>"; }else{ $menu .="$letter"; } $menu .= '</a>'; if ($letter!="Z"){ $menu .=" • "; } } $menu.= '</div></center>'; //echo "$menu"; ?> Hey, that works good. Quick question though, so I can understand what you did, How did u make the $letter only show once instead of each time, since it's on the same variable $menu? (and in the foreach??) (Im just trying to understand the code so I can learn it, Idon't want to steal code) Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/#findComment-1316899 Share on other sites More sharing options...
Drummin Posted February 11, 2012 Share Posted February 11, 2012 Really you just need to see the brackets and understand that the foreach end bracket is the last one before the </div>. Within each loop, the $letter can be compared to something, in this case we're checking against a GET value. So as this loop is running through all the letters, each is checked and if there is a match we place the <b></b> tags around $letter. The }else{ is saying, "if not a match, just use $letter without the bold tags. Because a GET value is not always present we add the isset() check to the IF statement so we don't get an undefined error message when GET is not there. I noticed the extra bullet you had at the end so I just added the IF statement that says IF $letter is not "Z" add the bullet. So in other words, IF and ELSE are a matching set where if the conditions of IF are true, do what's within the brackets of IF. ELSE is saying if IF is not true, do what's within these brackets. So within this IF ELSE group only one $letter will be TRUE and shown. Quote Link to comment https://forums.phpfreaks.com/topic/256860-need-to-wrap-tags-around-the-current-selection/#findComment-1316910 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.