How to get the link of current directory in php? - php

I have been trying to get the current directory link and come up with something like this as:
<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]paypal_success.php"; ?>
but really that didn't work as I got the output as :
http://localhost/php-login-new/invoice.phppaypal_success.php
so really I want to trim the invoice.php from it so if you can guide me please how I can do that..it will be great..!

Try to use dirname()
<?php
echo "http://$_SERVER[HTTP_HOST]".dirname($_SERVER[REQUEST_URI])."/paypal_success.php";
?>

$path = test/invoice.phppaypal_success.php
you should use basename($path), result is invoice.phppaypal_success.php
or you can use basename($path,".html"), result is invoice.phppaypal_success
$result_path = basename($path)
then you can use substr() in php
echo substr($result_path,5,-22);
Maybe this can help you

Related

Is it possible to add css style to print_r function in codeigniter php?

I have to display my username from a database and it's done using the print_r method. Now I would like to add some CSS functions to make the output large and bold.
Is it possible to add such a style to print_r?
<?php print_r($this->session->userdata['userName']); ?>
In your controller, use this
$data['username'] = $this->session->userdata['userName'];
And in you template file, use this
<div class="your-class"><?php echo $username?></div>
Its preety simple. I suggest you to spend sometime about mvc work flow.
Try to do in this way...
<?php
$styleArray = array('apple','banana','carrot');
echo '<pre><h1>';
print_r($styleArray);
echo '</pre></h2>';
?>
You can also add whatever class u wish.
u can use function in php like this
<?php
pre($this->session->userdata['userName']);
function pre($str = null){
echo "<pre>";
print_r($str);
echo "</pre>";
}
?>

Adding content between a PHP shortcode

I have a shortcode in a theme that displays only when a user is logged out. Usually, I would use the following shortcode to achieve this
[logged_out]Content[/logged_out]
How would I achieve this in PHP?
<?php echo do_shortcode("[logged_out]"); ?>
Can you just try the code like below,
<?php
$string = 'Hello world';
echo do_shortcode('[logged_out]'.$string.'[/logged_out]');
?>
you can assign any value to the variable $string ;
Or you can directly use like this,
<?php
echo do_shortcode('[logged_out]Hello world[/logged_out]');
?>
For more information please refer,
https://developer.wordpress.org/reference/functions/do_shortcode/
you can use following way in your php file.
echo do_shortcode('[logged_out] your content [/logged_out]');

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

Can I extract portions of a php include?

Still learning php as I go so this might just be something I haven't gotten to yet but it's the next roadblock in building my personal site. I have a basic understanding of includes such as linking:
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
to pull my includes but I've come to a small problem in that my generic art-gallery page needs to switch between a 'portfolio' header and an 'artwork' header. So I figured I could either build "art-gallery.php" AND "port-gallery.php" and go back and relink everything or just make it so that when you call the link like the above code I just specify which header goes with it. Unfortunately this would also require going back and changing every link. But I noticed that I did state:
...&menu=side-menu-portfolio...
and the pages are already calling side-menu-artwork or side-menu-portfolio so if I could just call in menu and cast aside the 'side-menu-" portion then it would just use artwork or portfolio and call the right header. Unfortunately this is where my limited knowledge of php and syntax come in. I have tried to produce the following code based on my php and js understanding:
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
but I don't know if my syntax is just wrong or if what I'm trying to do is impossible to begin with. Note that when I try this I get
Function Include error of "Warning: include(headlines/headline-.php)"
so it looks like everything else is reading correctly, I just don't know if or how I can extract the word I want from the rest of the menu name.
Should be, Assumed your included file name is headline-side-menu-portfolio.php
<?php
$filename = str_replace("side-menu-", "", $_GET['menu']); // headline-portfolio
include("headlines/headline-" . $filename . ".php"); // headline-portfolio.php
?>
Something like this :
<?php include("headlines/headline-" . $_GET["menu"].".php"); ?>
<!--gives include("headlines/headline-side-menu-portfolio.php")-->
where
$_GET["menu"] = 'side-menu-portfolio'
Try this:
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
Your code is wrong.
Instead of
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
try
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
You should check if the file exists before you try including it.
if (file_exists($filesrc)) { ... }
Better yet don't let the user change the menu through a $_GET variable. Instead link to a specific page or pass a variable then decide what menu to get. Like
switch ($_GET['menu']) {
case 'side-menu':
include("headlines/headline-side-menu.php");
break;
}
Just use
$_GET['menu']
, the "side-menu-" part is already in the content of your variable passed as param.
You propably want to do an if .... else so to include one header or another based on the $_GET variable menu.
So something like this will do this:
if($_GET['menu'] == 'side-menu-portfolio') {
include 'headliens/side-menu-portfolio.php';
} elseif($_GET['menu'] == 'side-menu-other') {
include 'headliens/side-menu-other.php';
}
okay....your are almost there....just quotes missing from include syntax...it should be
include("headlines/headline-.php"); /* notice the quotes*/
so it should be
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
where $_GET['menu'] should be in the url, like:
art.php?id=image id&name=This-is-my-title&menu=side-menu-portfolio
so what's happening her ??
Upon execution of the line :
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
$_GET is fetched from the url and replaced in the header tag, so now the header tag becomes :
<?php include("headlines/headline-"."side-menu-portfolio".".php"); ?> => <?php include("headlines/headline-side-menu-portfolio.php"); ?>
Also. may i suggest that for :
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
don't use space in the url, either replace it by - or _

PHP Include with a twist

I have an include
include ('myfile.php');
Now, I am using wordpress and to get the template path I use:
<?php echo get_template_directory_uri(); ?>
My question is:
How can I use both together?
Like:
<?php
include('<?php echo get_template_directory_uri(); ?>/myfile.php');
?>
Thanks
include(get_template_directory_uri() . "/myfile.php");
The include() function just takes a string parameter. Nothing special about it.
You can use the result of the function in the include, but not by echo:ing it. Just use it directly.
<?php
include(get_template_directory_uri() . '/myfile.php');

Categories