I'm creating a Dynamic Navigation using PHP, and i want to insert a php code inside a php code
on an <li>, i want to insert this line of code inside a list item
<?php printf('<li' if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';>); printf('%s %s </a></li> ', $row['name'],$row['DESCRIPTION']); } ?>
Wouldn't be better this way (?):
$content = "<li ";
if (strpos($_SERVER['PHP_SELF'], 'index.php')){
$content .= "class='current'";
}
$content .= "> </a></li>" . $row['name'] . " " . $row['description'];
Try this way:
<?php
$index = ((strpos($_SERVER['PHP_SELF'], 'index.php')) ? true : false);
printf('<li' . (($index) ? 'class="current"' : '' ) . '%s %s </a></li> ', $row['name'],$row['DESCRIPTION']);
?>
<?php
printf('<li' . (strpos($_SERVER['PHP_SELF'], 'index.php')) ? 'class="current"' : '' . '>');
printf('%s %s </a></li> ', $row['name'], $row['DESCRIPTION']);
?>
$is_current = '';
if(strpos($_SERVER['PHP_SELF'], 'index.php')){ $is_current = 'class="current"'; }
printf('<li'.$is_current.'>%s %s </a></li>', $row['name'],$row['DESCRIPTION']);
Not totally clear what you are asking, but I guess you can get what you want writing it differently, e.g.
<?php
echo "<li ";
if (strpos($_SERVER['PHP_SELF'], 'index.php')) {
echo ' class="current"';
}
// missing: '><a ...>'
printf(' %s %s </a></li> ', $row['name'], $row['DESCRIPTION']);
?>
(there's a final } without an opening {, I've just stripped it, without asking myself if it had to be the closing } of the missing opening { of the if).
Or you can use the ?: operator.
For good pratices and best identification in your code, try this:
if (strpos($_SERVER['PHP_SELF'], 'index.php')){
$class = "";
}else{
$class = "current";
}
printf('<li class="current"> %s %s </a></li>', $row['name'], $row['DESCRIPTION']);
Related
<?php
$genre = get_the_term_list($post->ID, 'genre', '<span class="genre">', ', ', '</span>');
$tags = get_the_term_list($post->ID, 'tag-series-movies','<span class="genre">', ', ', '</span>');
if($genre,$tags)
echo '<li>Genre: '. $genre . '</li>';
echo '<li>Tags: '. $genre . '</li>';
else {
echo '';
}
?>
Hi. Guys whats wrong of my code? though the code is working 50%. the $genre is working but the else is not working. please help. i want to show the nothing when no genres is not available.
Change if($genre == $genre) to if($genre){
get_the_term_list returns the list or false so you just need to check that $genre is a truthy value
In your current code when $genre is false you are checking if(false == false) which always returns true
EDIT:
you can simplify your code like this
if($genre){
echo '<li>Genre: '. $genre . '</li>';
}
if($tags){
echo '<li>Tags: '. $tags . '</li>';
}
There's no need for the else statement as you're not echoing anything
edd restrict content plugin creates this shortcodes and these are working in pages and posts :
[edd_restrict id="any"]sample restricted html or text[/edd_restrict]
but i want to use it in my theme not in the posts or pages.
i tried this :
<?php =echo do_shortcode( '[edd_restrict id="any"]' . sample text . '[/edd_restrict]' );?>
but theme shows me fatal error.
so how can i use this shortcodes in wordress theme?
sample text here will be a php code that i want to restrict.
let me tell you more... i want to put the line below between those shortcodes in my single.php in wordpress :
<li><?php if(get_post_meta($post->ID, 'download',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download',true)."' > دانلود با لینک مستقیم</a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'download32',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download32',true)."'>لینک مستقیم نسخه 32bit / </a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'download64',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'download64',true)."'>لینک مستقیم نسخه 64bit </a> ";} else { echo ""; } ?>
<?php if(get_post_meta($post->ID, 'downloadwin',true)!= ""){echo "<a href='".get_post_meta($post->ID, 'downloadwin',true)."'>دانلود نسخه ویندوز </a> ";} else { echo ""; } ?></li>
The method do_shortcode accepts a string of a shortcode and it's attributes/content. So for example
<?php
$string = "some value or <b>html</b>";
// or (?)
$string = include TEMPLATEPATH . "/post-ads.php";
// or (!)
ob_start();
include TEMPLATEPATH . "/post-ads.php";
$string = ob_get_clean();
// for a specific use case:
if (get_post_meta($post->ID, 'download', true) != "") {
$string = "<a href='" . get_post_meta($post->ID, 'download', true) . "' >download</a> ";
} else {
$string = "";
}
if (get_post_meta($post->ID, 'download32', true) != "") {
$string2 = "<a href='" . get_post_meta($post->ID, 'download32', true) . "'>download 32bit / </a> ";
} else {
$string2 = "";
}
// after you have string:
echo do_shortcode('[edd_restrict id="any"]' . $string . '[/edd_restrict]');
I have code like this:
<ul>
<?php foreach($persons as $key) : ?>
<?php if($this->session->userdata('id_user') == $key['id_person'] ) : ?>
<li><em><?php echo $key['name'] . " " . $key['surname'] ?></em></li>
<?php else : ?>
<li><?php echo $key['name'] . " " . $key['surname'] ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
How can I place result from the if condition at the first place? Can I do here or I need to write SQL query?
EDIT:
This is output:
<ul>
<li>Marko Džunić</li>
<li><em>Saša Miljković</em></li>
<li>mirko mirkovic</li>
</ul>
I want to get this:
<ul>
<li><em>Saša Miljković</em></li>
<li>Marko Džunić</li>
<li>mirko mirkovic</li>
</ul>
<ul>
<?php
$first = '';
$content = '';
$id_user = $this->session->userdata('id_user');
?>
<?php foreach($persons as $key) : ?>
<?php
$id_person = $key['id_person'];
$name = $key['name'];
$surname = $key['surname'];
if ($id_user == $id_person)
{
$first = '<li><em>' . $name . ' ' . $surname . '</em></li>';
}
else
{
$content .= '<li>' . $name . ' ' . $surname . '</li>';
}
?>
<?php endforeach; ?>
<?php echo $first, $content; ?>
</ul>
Go through your array two times.
The first time you only echo when the person is the selected person and then delete this person from the array by using unset().
Go through your array a second time like you did before.
The selected item is now the first item in the list.
Best way is to get the result sorted from the Mysql itself. In MySQL you can place particular rows at the top.
I am trying to build a menu with an active page element for CSS using PHP and MySQL.
This example PHP/HTML hybrid works, and is what I'm trying to mimic.
<nav><ul>
<li<?php if ($thisPage == "1") echo " class=\"active\""; ?>>page 1</li>
<li<?php if ($thisPage == "2") echo " class=\"active\""; ?>>page 2</li>
<li<?php if ($thisPage == "3") echo " class=\"active\""; ?>>page 3</li>
</nav></ul>
I want to blend this statement to set the active class:
<?php if ($thisPage == $menuID) echo " class=\"active\""; ?>
Into this unordered list statement
<?php
echo "\n<nav>\n";
echo "<ul>\n";
while($row_rsMenu = mysql_fetch_assoc($rsMenu))
{
echo "<li>" . "" . $row_rsMenu['menuName'] . "</li>\n";
}
echo "</ul>\n";
echo "</nav>\n";
?>
This is where I am but I can't seem to get the syntax to work correctly.
echo "<li" . "if(" . $thisPage==$menuID . ")". echo ' class=\"active\"';" . ">" . "" . $row_rsMenu['menuName'] . "</li>\n";
If someone could help me to understand where this went wrong I'd appreciate it.
You have your if statement encapsulated in quotes. That means that PHP won't interpret it. Instead, it will print it out along with your HTML.
Something along the lines of this will work better:
echo "\n<nav>\n";
echo "<ul>\n";
while($row_rsMenu = mysql_fetch_assoc($rsMenu))
{
$active = ""; //not active by default
if($thisPage==$menuID){
$active = ' class="active" ';
}
echo "<li>" . "" . $row_rsMenu['menuName'] . "</li>\n";
}
echo "</ul>\n";
echo "</nav>\n";
By the way, where are you getting $menuID from?
Your error is wrapping the if block within quotes, among a few other issues with quotes.
Try this instead:
echo '<li', (($thisPage==$menuId) ? ' class="active"' : ''), '>', '', $row_rsMenu['menuName'], '</li>';
This uses a ternary operator to shorten things up a bit, in addition to fixing your syntax error.
I've setup a menu for a fairly simple site based on icant.co.uk. It's fairly simple with maybe 5 pages. The small site is mainly a mysql browser for a few tables using MATE. Theres a common.php file that contains the header & footer HTML so thats where I put the code below.
The code below highlights the current page on the menu. Its ugly and I'm sure there has to be a better way to do it.
Any help is appreciated, thank you!
heres my code
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
if ($currentFile == "orders.php"){
echo '<li id="active">Orders</li>';
}
else{
echo '<li>Orders</li>';
}
if ($currentFile == "customers.php"){
echo '<li id="active">Customer List</li>';
}
else{
echo '<li>Customer List</li>';
}
if ($currentFile == "order_details.php"){
echo '<li id="active">Order Details</li>';
}
else{
echo '<li>Order Details</li>';
}
?>
UPDATE For those curious, below is the working code!
<?php
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "order_details.php", "title" => "Order Details"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
?>
What I normally do is something like (for all elements...):
<li class="<?php if (condition) echo 'selected'; ?>">content part, links, etc.</li>
Not sure if that's what you meant, but this way you'll get rid of this ugly if-else:
$currentFile = Explode('/', $_SERVER["PHP_SELF"]);
$currentFile = $currentFile[count($currentFile) - 1];
// easier to manage in case you want more pages later
$pages = array(
array("file" => "orders.php", "title" => "Orders"),
array("file" => "customers.php", "title" => "Customer List")
);
$menuOutput = '<ul>';
foreach ($pages as $page) {
$activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
$menuOutput .= '<li' . $activeAppend . '>'
. '' . $page['title'] .''
. '</li>';
}
$menuOutput .= '</ul>';
echo $menuOutput;
A more concise way of doing it (if you have short tags enabled) would be:
<li class="<?= $test=="your_page_name" ? 'selected' : 'not_selected'?>">Link Name</li>
It performs the same function as the first answer, just more concisely.
Here's a snippet from a project of mine. It it old ugly code, and uses tables, but you can just as easily use the idea for divs and cleaner markup. The trick is to make the navigation use a different class if the current page matches it's url.
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_home.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_home.php'>Billing Home</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_schedules.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_schedules.php'>Billing Schedules</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_outstanding.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_outstanding.php'>Outstanding</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_list.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_list.php'>List All</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_history.php'>Billing History</a></td></tr>
<td><a class='LeftSubNavLink<?php if($_SERVER["SCRIPT_NAME"] == "/admin/billing_statement_history.php"){print("Current");}?>' href='<?php print(MAIN_URL); ?>admin/billing_statement_history.php'>Statement History</a></td></tr>