I need to add php code in a Joomla backend file to display different link depending to the selected language.
I need to display link1 if language selected is english, while link2 if language selected is german
I'm using this php code, but I not understand the error:
<?php
$menu = JSite::getMenu();
if ($menu->getActive() == $menu->getDefault( 'en-GB' ))
{
<li>
<a href="link1" class="btn btn-primary">
<?php echo JText::_('OSM_ALTERNATIVE'); ?>
</a>
</li>
}
else
{
<li>
<a href="link2" class="btn btn-primary">
<?php echo JText::_('OSM_ALTERNATIVE'); ?>
</a>
</li>
}
?>
Can you help me?
Firstly, you have not used a closing PHP tag before your HTML.
Secondly, you are detecting the frontpage of your Joomla site, not the language. Try using the following:
<?php
$lang = JFactory::getLanguage();
$tag = $lang->getTag();
if ($tag == 'en-GB')
{ ?>
<li>
<a href="link1" class="btn btn-primary">
<?php echo JText::_('OSM_ALTERNATIVE'); ?>
</a>
</li>
<?php } else { ?>
<li>
<a href="link2" class="btn btn-primary">
<?php echo JText::_('OSM_ALTERNATIVE'); ?>
</a>
</li>
<?php }
?>
Related
I am currently working on a tour website, where the number of destinations is available for the user. A Login user can like any destination which will be added to his favorites destination section. I have done this task successfully. But after liked the destination the like button appears multiple times or equal to the number of likes have the user done. Maybe I am wrong in my logic here is my code. The first loop is fetching all the destination and the second loop fetching the favorites destination.
Note:-Here I am showing only logic code not complete HTML or other PHP code
<?php foreach($listing_data as $list){ ?>
<?php foreach($favorites as $fav){ if($fav['link_id']==$list['id']){?>
<li class="pull-right"> <a class="Theme" id="liked"><i class="fas fa-heart"></i> </a> </li>
<?php } else {?>
<li class="pull-right"> <i class="far fa-heart"></i> </li>
<?php } } }?>
Just add a helper variable
and take out the html in 2th foreach.
$like = false; in each Site
Then in each site you loop $favorites, then walk in $favorites for check if TheSite is liked;
after second foreach you should compare if $like is true for use class "fas" or false for use class "far"
<?php foreach($listing_data as $list){
$like = false;
foreach($favorites as $fav){
if($fav['link_id']==$list['id']){
$like = true;
}
}
if($like){ ?>
<li class="pull-right">
<a class="Theme" id="liked"><i class="fas fa-heart"></i></a>
</li>
<?php } else { ?>
<li class="pull-right">
<i class="far fa-heart"></i>
</li>
<?php
}
}
?>
This is just a refactoring of the code to make it look a bit cleaner.
The links are not included as they are ? and also use id's that will not be unique.
So the code could become something like...
<?php foreach ($listing_data as $list): ?>
<?php
foreach ($favorites as $fav) {
$like = ($fav['link_id'] == $list['id']);
}
?>
<li class="pull-right">
<?php if ($like): ?>
<span>More Sensible Link 1</span>
<?php else: ?>
<span>More Sensible Link 2</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
my view
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $row['count_name']?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
my controller
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
var_dump($a);die();
}
the result is show like this
string(12) "%20%20BRUNEI"
i want to show only the country name 'BRUNEI'
You can use - (dash) instant of space because space value change in url like %20%. Try the following code i have replace space to - in view and revert it on controller
View :
<div class="dropdown1">
<button class="dropbtn1">Asia</button>
<div class="dropdown1-content">
<li>
<ul style="height:520px; overflow: auto">
<?php foreach ($result as $row) {?>
<li>
<?php $county_name = str_replace(' ','-',$row['count_name']); ?>
<a href="<?php echo base_url(); ?>index.php/MyWeb_Controller/countriesView/<?php echo $county_name; ?>">
<?php echo $row['count_name'];?>
</a>
</li>
<?php } ?>
</ul>
</li>
</div>
</div>
Controller :
public function countriesView($a){
$this->load->helper('url');
$this->load->view('Country_View');
$a = str_replace('-',' ',$a);
var_dump($a);die():
}
I have some working PHP code and I have recently added a button that allows the user to download the image form the root directory, in the database we put the file name e.g. example.png / example.jpeg and when the user clicks download it opens the image in a new tab
what we need is: if the [proof] is= NULL , the download button disables, otherwise it will be enabled and they can click the button
<?php
// output data of each row
while($row=mysqli_fetch_assoc($designresult)) {
?>
<div class="card mb-4 box-shadow"><div class="card-header">
<h4 class="my-0 font-weight-normal">Job Reference: <?php echo $row["jobRef"]; ?></h4>
</div>
<div class="card-body">
<p><b>Company Name:</b><br> <?php echo $row["companyName"]; ?> </p>
<p><b>Requested:</b><br> <?php echo $row["dateReq"]; ?> </p>
<p><b>Request By:</b><br> <?php echo $row["yourName"]; ?> </p>
<p><b>Graphic Type:</b><br> <?php echo $row["graphicType"]; ?> </p>
<p><b>Double Sided:</b><br> <?php echo $row["doubleS"]; ?> </p>
<p><b>Design Info:</b><br> <?php echo $row["info"]; ?> </p>
<p><b>Purpose:</b><br> <?php echo $row["purpose"]; ?> </p>
<p><b>Proof:</b><br> <?php echo $row["approved"]; ?> </p>
<p><b>Proof Date:</b><br> <?php echo $row["appDate"]; ?> </p>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">Download</a>
</div>
</div>
<?php
}
?>
To my knowledge the link tag does not have a "disabled" attribute. But you can "disable" the link by removing the href attribute.
Something like this: It checks if $row['proof'] has some value (by negating the empty), then it prints out the href if result is true.
<a class="btn btn-success" target="_blank" <?php if(!empty($row['proof'])): ?> href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php endif; ?> >Download</a>
Or maybe better: Check if variable is empty and give the user a hint that it's not available. I think this is the better solution, because then your users will know what's going on.
<?php if(empty($row['proof'])): ?>
<span>No proof available</span>
<?php else: ?>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
<?php endif; ?>
<a
class="btn btn-success"
target="_blank"
<?php if(empty($row['proof'])) echo "disabled"; ?>
href="<?php if(!empty($row['proof'])) echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
try above code. and add disabled class using this condition
<?php
$state = (empty($row['proof'])) ? "disabled='disabled'" : "";
$class = (empty($row['proof'])) ? "disabled" : "";
?>
<a class="btn btn-success <?php echo $class; ?>" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php echo $state; ?>>Download</a>
To disabled the button, you need to use disabled HTML attribute. The code above checks $row['proof'] == NULL. If this statement is true it prints disabled = "disabled" in the button element and it isn't true, it prints nothing.
Assuming that you are using bootstrap, .disabled will grayed out the button.
i'm trying to store html tag that includes php script.
This is for the navigation menu, i'm trying to display only the menu for a particular user role.
I'm storing it this way
public function edit_module(){
$id = $this->input->post('Module_ID', TRUE);
$update = $this->db->update('ref_modules', array('Module_Name'=>$this->input->post('Module_Name', TRUE),
'Module_Menu'=> htmlspecialchars($this->input->post('Module_Menu')),
'UpdatedBy'=>$this->session->userdata('user_id')), "Module_ID = '$id'");
if($update){
return TRUE;
}
}
For display
<!--Menu-->
<!--================================-->
<div id="mainnav-menu-wrap">
<div class="nano">
<div class="nano-content">
<ul id="mainnav-menu" class="list-group">
<!--Category name-->
<li class="list-header">Navigation</li>
<?php if($this->data['menu']): ?>
<?php foreach($this->data['menu'] as $row): ?>
<?php echo htmlspecialchars_decode($row->Module_Menu); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div> <!-- nano -->
</div><!-- mainnav-menu-wrap -->
<!--================================-->
<!--End menu-->
When i view the source, its fine but when i click the link, special characters are showing.
http://localhost/folderName/%3C?php%20echo%20base_url(%27citizens%27);%20?%3E
Here's a sample value that i am inserting
<li class="<?php echo ($title == 'Citizens' ? 'active-link' : '');?>">
<a href="<?php echo base_url('citizens'); ?>">
<i class="fa fa-user"></i>
<span class="menu-title">
<strong>Citizens</strong>
</span>
</a>
</li>
Any idea guys? Thanks in advance!
Inserting Html into database For Different user role is not the right way. You can use if Conditions To show your Menu for different User roles.
I have a simple navigation which uses php to know which "current page" you are on allowing my css to indicate this.
my php code:
<?php echo "\n"; if ($currentPage == 'about.php') { ?>
What I'm trying to do is have this button active as the current page within its child pages?
is there any way of having multiple pages within the above code?
I tried this but it doesn't work
<?php echo "\n"; if ($currentPage == 'about.php about2.php about3.php') { ?>
You can use in_array:
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
Which will basically go through an array and compare the value ($currentPage) with each of those in the array.
You could use the in_array() function : http://php.net/manual/en/function.in-array.php
if (in_array($currentPage, array('about.php', 'about2.php'))) {
// Do Something
}
(NOT AN ANSWER JUST AN UPDATE)
This is what I have at the moment: (only the about.php activates the switch)
<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="about.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
About
</a>
</li>
<?php } ?>
If I change the pages to home.php, contact.php (which were in the navigation originally) they all work turning them all on at the same time?
So im not sure what I need to do in order to include my new pages?
I this is one of my other buttons:
<?php echo "\n"; if ($currentPage == 'contact.php') { ?>
<li class="button on">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</li>
<?php } else { ?>
<li class="button off">
<a class="nav" href="contact.php">
<img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
Contact
</a>
</li>
<?php } ?>
Is this why it is picking up the original pages but not my new ones?