I have some code where i need to insert a class called active within the link tag. But for some really weird reason it wont work even though the values match and it really should only make the beef menu item blue and not the others. Screenshot attached.
if($menuitems->title==$menutitle) {
$activemenu='active';
}
echo '<a href="#menu_'.$cid.'" class="list-group-item list-group-item-success '.$activemenu.'" data-toggle="collapse" data-parent="#MainMenu" >'.$menuitems->title.' / '.$menutitle.' / '.$menuitems->title.'</a>';
$menu->title does only equal beef but yet its inseting teh active classs intoall the other top level menus.
Thanks for your help :)
Jonny
Resetting $activemenu variable will fix it before if statement or inside foreach.
$activemenu = '';
<?php
$activemenu = '';
if($menuitems->title==$menutitle)
{
$activemenu='active';
}
?>
<a href="#menu_<?php echo $cid ;?>" class="list-group-item list-group-item-success <?php echo $activemenu; ?>" data-toggle="collapse" data-parent="#MainMenu" ><?php echo $menuitems->title . ' / ' . $menutitle .' / '. $menuitems->title; ?></a>
I'd suggest you to go with this. Honestly, I myself got stuck in these type of situations before. It's always better to have <a> tags outside php.
Related
this is my second question here, first one was resolved quite quickly and I appreciate any help I get. Here's the thing:
I have a folder views in which I have a folder named _global in which I have my beforeContent.php and afterContent.php those are my header and footer of actual Web pages. The main content in my pages is set in other view folders and I have no problem there.
I have this script in my beforeContent.php (this one shows my navbar):
<?php if (Session::exists('user_id')): ?>
<?php include 'app/views/_global/menu-session.php'; ?>
<?php else: ?>
<?php include 'app/views/_global/menu-no-session.php'; ?>
<?php endif; ?>
basically, if a user is logged in, it will show menu-session.php, if there's no user logged in, it will show menu-no-session.php.
my menu-session has this in it:
<ul>
<li><a <?php if ($FoundRoute['Controller'] == 'Main') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?>">Home</a>
</li>
<li><a <?php if ($FoundRoute['Controller'] == 'Overview') echo
'class="active";'?> href="<?php echo Configuration::BASE_URL; ?
>overview">Overview</a></li>
</ul>
it has more tabs, but you get the point.
I had to use this function that chooses the controller that will decide if the page is active or not. basically: If i'm on a page Overview, the script asks if the active Controller is named 'Overview' and if it is, it will select the tab as class="active".
However, this is wrong: my teacher said I can't have scripts in my view files (it's not wrong, it's just bad practice) and I need another method of doing this:
So, I created a method class Misc.php with a function Misc::url:
public static function url($link, $text){
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
this way my menu-session can just be:
<ul>
<li>Misc::url('', 'Home')</li>
<li>Misc::url('overview', 'Overview')</li>
</ul>
Now: I need a correct function in Misc.php where I have the >>> $FoundRoute['Controller'] <<< if statement implanted, something like:
Misc::urlWithActive($link, $text){
if ($foundRoute['Controller] = *thiscontroller*) {
echo '<a href="' . Configuration::BASE_URL . $link . '">' . $text .
'</a>';
}
I actually don't know how to write that.
I Really hope you guys understand what I need to do here and help me.
I've come across a situation where there's a gnarly mix of HTML and PHP (well at least it seems that way to me because I'm not an expert in PHP). Currently, there's a hard-coded URL that I'd like to generalize using a PHP function. However, this is where I'm running into issues as this mix is getting rather complex.
After spending over 2 hours on this, I think I'm at a point where looking through topics on this doesn't seem to discuss this particular use case, and lots of trial-and-error isn't yielding the desired results.
Inside my template, I have the following code for my sidebar:
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<?php
// First attempt HTML in PHP
$home_url = get_home_url();
echo '<div class="field-title"><a class="field-value rating" target="_blank" href=' . $home_url . '/rating-criteria/' . '>Rating:</a> <span class="rating">' . the_field('rating') . '</span></div>'; ?>
// Second attempt PHP in HTML
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php the_field('rating'); ?></span></div>
// The hard-coded URL that I'm trying to generalize
<div class="field-title"><a class="field-value difficulty" target="_blank" href="https://s1.temporary-access.com/~allacros/sandbox2/difficulty-criteria/">Difficulty:</a> <span class="difficulty"><?php the_field('difficulty'); ?></span></div>
...
The results of this code can be seen in the sidebar in:
https://s1.temporary-access.com/~allacros/sandbox2/california-switzer-falls.html
In that sidebar (beneath "About Switzer Falls" below the Hero Image), you can see 2 Ratings.
The first one has the correct link, but the formatting is off as the "2" is not where it's supposed to be and it's unformatted.
The second one has the correct formatting, but it has the incorrect link.
Anyone know what I'm doing wrong?
Thanks
For the first line, looks like there is "echo" statement in your function the_field("rating"), which cause the rating "2" output first before your "echo" execute.
For the second line, there is error at "$php", which should be "?php".
Change your 2nd statement to the following:
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php echo the_field('rating'); ?></span></div>
In addition to what others have mentioned about the $ where a ? should be, you could maybe even simplify things for yourself by doing something like this for the link. All you've got to do is echo the $home_url variable you're creating at the start.
link
It's quite simple, really. There's a number of things you are doing on one single line that you don't actually need to do...
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<div class="field-title">
<a class="field-value rating" target="_blank" href="<?php echo home_url(); ?>/rating-criteria/">Rating:</a>
<span class="rating"><?php the_field('rating'); ?></span>
</div>
Now, things to note... I split the HTML over multiple lines instead of trying to keep it on just one. I have also removed the bulk of it from PHP processing altogether, as the static HTML doesn't need to go through PHP. This cleans it up immensely.
Lastly, if you look back at your code, you were echoing out the return value of the_field. This is where your random '2' is coming from, most likely. the_field should be outputting the value itself, so you should not concatenate it's return value on then output it.
O.K. Can't quite get any reference to something similar. I have two arrays, one sets my menu navigation items:
$nav_items = array('item1_link'=>'item1_displayname',
'item2_link'=>'item2_displayname',
'item3_link'=>'item3_displayname',
. . .
Then, I have my second array, which sets a number of possible colors:
$colors = array('red'=>'#f00',
'green'=>'#090',
'yellow'=>'fc0',
. . .
The idea is to merge these two so that the result will be:
<a class="red" href="item1_link">item1_displayname</a>
<a class="green" href="item2_link">item2_displayname</a>
<a class="yellow" href="item3_link">item3_displayname</a>
The thing is: imagine I have 10 menu items and I decide on 7 different colors. The idea is that (this is where I'm stuck) a main loop will iterate through the 10 menu items, assigning to each one of the color items from a second loop, which should cycle one time and then a second time (and a third, etc., if necessary) until all items from group 1 have been exhausted.
Perhaps an example of what I want to end up with will be more helpful:
<a class="red" href="item1_link">item1_displayname</a>
<a class="green" href="item2_link">item2_displayname</a>
<a class="yellow" href="item3_link">item3_displayname</a>
<a class="blue" href="item4_link">item4_displayname</a>
<a class="orange" href="item5_link">item5_displayname</a>
<a class="purple" href="item6_link">item6_displayname</a>
<a class="gray" href="item7_link">item7_displayname</a>
<a class="red" href="item8_link">item8_displayname</a> <!--Notice how colors restart here-->
<a class="green" href="item9_link">item9_displayname</a>
<a class="yellow" href="item10_link">item10_displayname</a>
So, PHP-code-wise, what I've got up to now is the following:
<?php
reset($nav_items);
reset($colors);
while ((list($nav_link, $nav_name) = each($nav_items))) {
list($color_name) = each($colors);
?>
<li><a class="<?php echo $color_name ?>" href="<?php echo $nav_link ?>"><?php echo $nav_name ?></a></li>
<?php
}
?>
Which is not bad, but only goes once through color array and then repeats the last color for the remaining menu navigation items.
So, how do I get the color array to restart once its iteration is finished (and the nav_item iteration is not)??
Any help on the matter will be MUCH appreciated!
P.S. I also tried this as an Iterator but couldn't quite get it to work. Perhaps that is the best response after all, but still I couldn't get the colors array to loop back in order to complete the nav_items array cycle.
if (current($colors) === false) reset($colors);
You might want to consider using the modulo operator:
$number_of_colors = count($colors);
$loop_count = 0;
foreach($nav_items as $nav_link => $nav_name) {
$use_color = $colors[$loop_count % $number_of_colors];
echo "<li><a class='{$use_color}' href='{$nav_link}'>{$nav_name}</a></li>\n";
$loop_count++;
}
Edit: Oups, sorry, didn't realize you have the names of your colors you want to use as CSS class as keys in your array... If you must have it organized this way, you could do this:
$color_classes = array_keys($colors);
and then use $color_classes instead of $colors in the snippet I provided above.
I am learning PHP by myself and I wonder if you guys can help me to solve my problem. I want to concatenate 2 variables that are align right in the form of an hyperlink with a word. I can do that no problem, but the word is not getting aligned right as well. I have tried somethings, but it doesnt work. Please see my code:
<?php
$log = ( '<a style="float:right; "href="login.php">login </a>' ) ;
$reg = ( '<a style="float:right; "href="login.php">register </a>' );
echo $log ." or " . $reg;
?>
I need login or register to be displayed on the top right of the page, but I dont want to hyperlink the word "or".
Also, if you guys have any links for good tutorials on PHP or tutorials on how to create a good website design, maybe templates, please paste here. I am starting with websites now and I am learning by myself. Any help is appreciatted. :-)
You probably are thinking wrong in terms of HTML/CSS markup. HTML code that will be generated by your script looks as follows:
<a style="float:right; "href="login.php">login </a> or <a style="float:right; "href="login.php">register </a>
It will first float "login" to the right, then append "register" to the left of "login".
I would suggest doing it this way:
<?php
$log = 'login ';
$reg = 'register ';
echo sprintf("<div style='float:right;'>%s or %s</div>", $log, $reg);
?>
I would wrap them in a container div thats floated right,
float the links left and make the or a span that is floated left as well.
<?php
$log = ( '<a style="float:left; "href="login.php">login </a>' ) ;
$reg = ( '<a style="float:left; "href="login.php">register </a>' );
echo '<div style="float:right;">'.$log .'<span style="float:left;"> or </span>'.$reg.'</div>';
also, its better to assign classes. you could write all that markup just by assigning a class to the container div
Try encapsulating both of your anchor tags in a div tag that is floated to the right.
<?php
$log = 'login' ;
$reg = 'register';
echo "<div style='float:right; width:200px;'>$log or $reg</div>";
?>
also, notice how inside of double quotes, you can put a variable as i've done in my echo statement.
If you need a CMS try some existing distributions instead of inventing the wheel again.
For templates use something like Mustache or Smarty.
First of all, I know this is simple but I'm not a PHP developer by trade so I apologize if this is dumb for everyone. That being said, I've spent the last 3 hrs searching and can't find a way to do this without re-inventing my wheel so to speak. I use a simple command to mark the navigation on websites when the page is "current".
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'page')) echo 'class="current"';?> href="page.php" class="tp">Page</a></li>
Works great in most cases. However in this case, I need to concatenate the class string, not overwrite it with current. I've tried
...echo'class=" "."current".'
and several variations and can't get it to simply add the class not overwrite it. Thank you
Is the <li> not in a loop?
Ideally your menu would come from a table array through which you would loop. If that is the case; try this:
foreach(..){
$class = strpos($_SERVER['PHP_SELF'], 'page') ? " current" : "";
echo "<li>Page</li>";
}
Overall, for readability, I would recommend to not put if statements inline in the HTML.
But based on your code:
<?php
$class = strpos($_SERVER['PHP_SELF'], 'page') ? " current" : "";
?>
<li>Page</li>
What about something like this :
<li>
<a href="page.php"
class="<?php if (strpos($_SERVER['PHP_SELF'], 'page')) {echo 'current ';} ?>tp">
Page
</a>
</li>
This way, you'll always have at least class="tp", and, if the condition is met, a 'current ' (note the space at the end of this string : it's important and required so the two classes are separated by a space and not considered as one) will be inserted just before the 'tp', inside the class="..."