- How to get current page using start index, items per page, total items, and total pages? (PHP)
- 4 Answers 4
- PHP Get Contents of a URL or Page
- 6 Answers 6
- How to get current page menu item ancestor name wordpress
- 3 Answers 3
- How to get URL of current page in PHP [duplicate]
- 5 Answers 5
- PHP to get current page specific content
- 2 Answers 2
How to get current page using start index, items per page, total items, and total pages? (PHP)
What would $currentpage be, based on $startIndex? I’m working on pagination and it’s kicking my butt. I know it’s probably some very simple math, but I can’t think right now.
How do you pass the current page index? If by get, you’ll use something like $currentpage = $_GET[‘currentpage’];
4 Answers 4
With 10 items per page and assuming your item count/numbering starts at 1, page 1 contains items 1 to 10, page 2 contains items 11 to 20, page 3 contains 21 to 30, and so on.
$currentPage = ceil(($startIndex - 1) / $itemsPerPage) + 1;
I used ceil() to make sure you have an integer number, which is rounded up so the current page number is correct.
If your item count starts at 0, so page 1 contains items 0 to 9, you can skip the — 1 and + 1 parts of the formula:
$currentPage = ceil(($startIndex) / $itemsPerPage);
I don’t believe this answer is correct. Consider a set of 14 records, with a limit of 7 per page. If your startIndex = 7, the page should be 2, but ceil(7/7) = 1. With 100 records, a limit of 10 and a startIndex of 50 the page should be 6, but 50/10 = 5. The answer of Sammitch is correct, ((startIndex-1)/itemsPerPage) + 1, which for these two examples gives ceil(((7-1)/14)+1) = 2, and ceil((50-1/10)+1) = 6. The other consideration is startIndex of 0 which you have to handle separately.
You are right, the answer is a little ‘ambiguous’, the formula is/was correct when item count starts at zero, but my explanation used item 1 as first one. The answer of Sammitch is lacking the ceil() part (and any explanation at all), so I updated my answer to have the correct/full formula and explanation.
PHP Get Contents of a URL or Page
I am trying to create a PHP script which can request data, such as HTML content, from an external server, then do something with the received content. Here is a generalized example of what I am trying to accomplish:
//Get the HTML generated by http://api.somesite.com/ //Now tack on the Unix timestamp of when the data was received $myFetchedData = $dataFromExternalServer . "\n Data received at: ". time(); echo $myFetchedData;
I’m thinking I should use curl in here somewhere, but I am not sure after that. Could someone post a generalized example of how I could do this?
6 Answers 6
If you only need GET and allow_url_fopen is enabled on your server, you can simply use
$data = file_get_contents('http://api.somesite.com');
Yes, I just need a simple GET request. I know allow_url_fopen isn’t enabled by default on some web-hosts, especially on the budget hosts. To allow maximum compatibility, which do you recommend, file_get_contents() or the curl library?
This is how you would use cURL to get contents from a remote url. You would define the function and make calls like url_get_contents(«http://example.com/»);
function url_get_contents($url, $useragent='cURL', $headers=false, $follow_redirects=true, $debug=false) < // initialise the CURL library $ch = curl_init(); // specify the URL to be retrieved curl_setopt($ch, CURLOPT_URL,$url); // we want to get the contents of the URL and store it in a variable curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // specify the useragent: this is a required courtesy to site owners curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // ignore SSL errors curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // return headers as requested if ($headers==true)< curl_setopt($ch, CURLOPT_HEADER,1); >// only return headers if ($headers=='headers only') < curl_setopt($ch, CURLOPT_NOBODY ,1); >// follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up if ($follow_redirects==true) < curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); >// if debugging, return an array with CURL's debug info and the URL contents if ($debug==true) < $result['contents']=curl_exec($ch); $result['info']=curl_getinfo($ch); >// otherwise just return the contents as a variable else $result=curl_exec($ch); // free resources curl_close($ch); // send back the data return $result; >
How to get current page menu item ancestor name wordpress
I would like to know how to retrieve as variable the name of the current page. For exemple, My menu is like :
Item 1 & 2 are custom links. sub-item1 is my page. When I’m on this page, I want to retrieve «Item 2» name (to build my custom breadcumbs) Thanks
3 Answers 3
BREADCRUMBS:
Here is a breadcrumbs function that work with the hierarchy of your menu. Add this to your theme’s ‘functions.php’.
function my_menu_breadcrumb($theme_location, $separator = ' > ') < $locations = get_nav_menu_locations(); if ( isset( $locations[ $theme_location ] ) ) < $menu = wp_get_nav_menu_object( $locations[ $theme_location ] ); $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context( $menu_items ); $breadcrumbs = array(); foreach ( $menu_items as $menu_item ) < if ($menu_item->current) < $breadcrumbs[] = "\">title>"; > else if ($menu_item->current_item_ancestor) < $breadcrumbs[] = "\" title=\"title>\">title>"; > > echo implode($separator, $breadcrumbs); > >
You can then call as where ‘header-menu’ is the menu location name. In the loop, $menu_item->title will return page title and $menu_item->url it’s URL.
PARENT MENU TITLE:
Here is the function to get parent menu item title(s) of current page — add this to your theme’s ‘functions.php’.
function my_menu_parent($theme_location) < $locations = get_nav_menu_locations(); if ( isset( $locations[ $theme_location ] ) ) < $menu = wp_get_nav_menu_object( $locations[ $theme_location ] ); $menu_items = wp_get_nav_menu_items($menu->term_id); _wp_menu_item_classes_by_context( $menu_items ); $breadcrumbs = array(); foreach ( $menu_items as $menu_item ) < if ($menu_item->current_item_ancestor) < $breadcrumbs[] = $menu_item->title; > > return $breadcrumbs; > >
You can then call as below where ‘header-menu’ is the menu location name.
$parentitems = my_menu_parent( 'header-menu' ); foreach ( $parentitems as $parentitem ) < echo $parentitem."
"; >
How to get URL of current page in PHP [duplicate]
In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example .
I find it funny that this question is marked as a duplicate of another despite being asked two years earlier
@cameronjonesweb And that the other question has a totally different scope (getting the full URL), as opposed to this one (getting the current page only)
5 Answers 5
For more details on what info is available in the $_SERVER array, see the PHP manual page for it.
If you also need the query string (the bit after the ? in a URL), that part is in this variable:
iirc, PHP_SELF and REQUEST_URI will have different values if the page was redirected via mod_rewrite — the former has the path to the actual script, the latter has the originally requested path.
$_SERVER[‘REQUEST_URI’] is also contaning all query strings, why should I use $_SERVER[‘QUERY_STRING’] ?
If you want just the parts of URL after http://domain.example , try this:
If the current URL was http://domain.example/some-slug/some-id , echo will return only /some-slug/some-id .
If you want the full URL, try this:
how about replacing http with REQUEST_SCHEME: $_SERVER[‘REQUEST_SCHEME’]. «://» . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]
This will give you the requested directory and file name. If you use mod_rewrite, this is extremely useful because it tells you what page the user was looking at.
If you need the actual file name, you might want to try either $_SERVER[‘PHP_SELF’] , the magic constant __FILE__ , or $_SERVER[‘SCRIPT_FILENAME’] . The latter 2 give you the complete path (from the root of the server), rather than just the root of your website. They are useful for includes and such.
$_SERVER[‘PHP_SELF’] gives you the file name relative to the root of the website.
$relative_path = $_SERVER['PHP_SELF']; $complete_path = __FILE__; $complete_path = $_SERVER['SCRIPT_FILENAME'];
PHP to get current page specific content
I am trying to find out if it’s possible & what code to use: to load the current page’s contents and echo out a relative path to a specific page (c.html) that’s w/in «#navbar a» using PHP or PHP Simple Html DOM Parser. My code so far:
"; // "http://www.partiproductions.com/copyr/index.php" - Correct $cfile = basename($cpath); echo 'Current File: ' . $cfile . "
"; // "index.php" - Current pg, Correct $html = file_get_html($cpath); // Getting Warning: URL file-access is disabled in the server configuration & failed to open stream: no suitable wrapper could be found in.. & Fatal error: Call to a member function find() on a non-object in. foreach($html->find(sprintf('#navbar a[href=%s]', $pg)) as $path) < echo 'Path: ' . $path."
"; > ?>
Try $html = file_get_html(‘index.php’); and see if you get anything. If not, is index.php in the same directory as your script? If not, try getting $cpath .
Hi @cpilko Tried just index.php, but didn’t work. The index.php is the current page. See comment to Wally below
I think I’ve gotten confused with what you’re trying to do. Are you trying to display a link to ‘c.html’ inside of your page? or are you trying to actually include the contents of ‘c.html’ inside of the index page?
@ Wally Lawless — Thanks so much for coming back — much appreciated. Taking a step back — I ultimately want to bring in specific content from a specified pg ‘c.html’ into the current page(index.php in this example) This was just the 1st step towards that, then initially thought I’d use jquery for bringing in content, BUT if we can do this all in php — that would be great ! I had developed a jquery way to bring in css file from c.html, but could never bring in js file & get it working. — Thanks
2 Answers 2
The main problem you have is with your call to file_get_html($cfile).
$cfile in your example would contain something like /copyr/index.php
When you pass this into file_get_html(), it is going to be looking for a directory /copyr in your server’s root directory, and an index.php file inside of that. Based on the warning that you’ve indicated, you don’t actually have this folder structure at the root of your server.
What you actually need to do is include the full URL in front of the URI that you currently have, like this:
$cpath = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];