Base_url not working? - php

Just a simple quetsion?
How to make CI
<?= base_url();?>
working with 'String'
The full codes as follows:
<?php if($_SESSION['admin'] == 1||$_SESSION['admin']== 0){
echo "<a class='btn-common' href='<?= base_url();?>usr/logout'>LOGOUT</a>";
}
else{
echo "<a class='btn-common2'></a>";
}
Base_url is not woking
<?base_url();?>

You can't use a php method inside this string, you need to concatenate the string with the result instead:
echo "<a class='btn-common' href='" . base_url() . "usr/logout'>LOGOUT</a>";

Also like this. pass your path to base_url() as argument:
<a class='btn-common' href='<?php echo base_url("usr/logout");?>'>LOGOUT</a>;
Don't forget to load url helper.using
$this->load->helper('url');

You need to concatenate the string.
echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";
I would recommend to use site_url with your controller in parameter (which will use your config file to build url automatically).
With Codeigniter, you can use $this->session->userdata('admin') for retrieving session data too.
if($this->session->userdata('admin')===1 || $this->session->userdata('admin')===0){
echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";
}else{
echo "<a class='btn-common2'></a>";
}

You should be using codeigniter links
<?php echo anchor("usr/logout",'Logout', 'title="Logout"');?>
You dont need to use base url, Codeigniter enters that for you

Related

PHP creating url with parameters

I am new to PHP and want to create a link with parameters.
So I used this :
<li>
<!-- $GLOBALS["ROOT_PATH"] is where my index.php file located -->
<a href="<?php echo $GLOBALS["ROOT_PATH"]."/Views/pages/profile.php"; ?>">
Profile
</a>
</li>
but when I click on the page it doesn't send me to the page or do anything. When I look at the URL it's something like file:///C:/bla/bla/bla/Views/pages/profile.php
Edit:
Basically, I want to use this in my header.php but my files are :
index.php
Views/pages/profile.php
and so on.
When I use the relative path for the header the path changes for these pages. How can I solve this?
So I solved it using some tricky way but it works :
function createLink($url,$text,$class){
echo "<li>";
// Gets the current php file name.
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
if($basename !== "index"){
$url = "../../".$url;
}
echo '<a href="'. $url . '">';
echo '<span class="'.$class.'"></span>';
echo ' '.$text.'';
echo "</a>";
echo "</li>";
}
So it just add the relative path if the file name is not index.php.

How can I add an image inside a shortcode

I have an image displayed in the following manner:
<img src="<?php bloginfo('template_directory')?>/images/logo/logo.png">
Now, however, I want to create a shortcode that has an attribute and uses that same image inside it. So I tried the following:
add_shortcode('scrollTop', function(){
echo "<a href='#content' class='content'>";
echo "<img src='bloginfo('"template_directory"')/images/logo/logo.png'">;
echo "skipping content";
echo "</a>";
});
Its a syntax error I assume, but am struggling to find a way around.
How can I add the image inside the shortcode?
Use the concatenation operator and correct single quotes inside the bloginfo function.
add_shortcode('scrollTop', function(){
echo "<a href='#content' class='content'>";
echo "<img src='" . bloginfo('template_directory') . "/images/logo/logo.png'>";
echo "skipping content";
echo "</a>";
});
Also make sure to not use echo inside your shortcode to display something. Use return instead. See this question for more information.
add_shortcode('scrollTop', function(){
return "<a href='#content' class='content'>
<img src='" . bloginfo('template_directory') . "/images/logo/logo.png'>
skipping content
</a>";
});

How to POST multiple Variable in JavaScript inside PHP?

I am trying to pass multiple variable using JavaScript and PHP, but not being able to do that.
echo "<a href=javascript:popcontact('btsdetails.php?uid=" . $row["bs_id_site"] . "&sid=" . substr($row['bs_id'], -1) . "')>" . $row['bs_id'] . "</a>";
Therefore, I am trying to POST "uid" and "sid" using & sign, but it is not working. It is only accepting the "uid".
Can anyone help me to solve this problem?
Here is the answer:
<script type="text/javascript">
function popcontact(uid, sid){
window.location = "btsdetails.php?uid="+uid+"&sid="+sid;
}
</script>
<?php echo $row['bs_id']; ?>
you can use http_build_query
<?php
$params = array("uid"=>$row["bs_id_site"], "sid"=>substr($row['bs_id'], -1));
$url = "btsdetails.php?".http_build_query($params);
?>
<?= $row['bs_id'] ?>

how to use urlencode( ) in my example?

I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.

Can't make PHP function work inside HTML inside PHP

I wrote this code, it gets an image from a link that varies according to where you are:
<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>
I want to make that code run if a PHP condition proves true, but I cannot make it work. It seems that the function doesn't return a value instead it takes the link textually. I mean it goes to http://chusmix.com/Imagenes/grupos/.jpg literally. However the code works correctly by itself.
This is the PHP code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
}
?>
You are already inside the php tag. So there is no need for <?php and ?>.
Try:
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr($search,1).".jpg'>";
Replace line
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
with
echo "<img src='http://chusmix.com/Imagenes/grupos/" . substr(get_search_query(), 1) . ".jpg'>";

Categories