Jump to content

Need to wrap <b> Tags around the current Selection


Monkuar

Recommended Posts

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

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

$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.

 

:shrug:

 

Im getting 26 words of "HEY" when im only needing 1..

 

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

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.