Single index.php PHP script to load different page using URL Variable

Содержание
  1. Single index.php PHP script to load different page using URL Variable
  2. #1: Strategy behind Coding:
  3. #2: The main code to produce different page from the same script:
  4. Welcome to the home page
  5. Welcome to the about page
  6. Welcome to Contact page
  7. Find and Extract All links From a HTML String in PHP
  8. Example 1: Get All Links From HTML String Value
  9. Example 2: Get All Links From a Web Page
  10. Related Posts:
  11. css - Style multi page links (index.php?first_page) - PHP
  12. Answer
  13. Solution:
  14. Share solution ↓
  15. Additional Information:
  16. Didn't find the answer?
  17. Similar questions
  18. Write quick answer
  19. About the technologies asked in this question
  20. PHP
  21. CSS
  22. HTML
  23. Welcome to programmierfrage.com
  24. Get answers to specific questions
  25. Help Others Solve Their Issues
  26. Latest questions:
  27. 778 php - Posts List not displaying in WordPress admin panel
  28. 151 php - How do I can resolve the error regarding "installable set of packages in Laravel 5.5 ( laravelcollective/html couldn't be install. )
  29. 665 php - Laravel and passport: POST method returns MethodNotAllowedHttpException
  30. 519 php - Socialite stateless auth
  31. 323 php - Save multiple inputs incremented
  32. 86 javascript - Client not receiving emails from page
  33. 116 Nested if statement in PHP MySQL SELECT
  34. 670 php - The queries on symfony4 / twig
  35. 121 php - Add a custom chosen date to cart item on add to cart action in Woocommerce
  36. 307 php - Change a specific product button text in a Woocommerce product category page
  37. Users questions:
  38. mysql
  39. Warning: Undefined array key "program" in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key "enter_course" in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key "enter_address" in E:\xampp\htdocs\pi\alumni.php on line 32
  40. Rename existing images with keyword in WordPress PHP
  41. codeigniter
  42. Class "web_profiler.controller.profiler" does not exist after upgrading symfony 3.4 to 4.4 version
Читайте также:  Почему string final java

Single index.php PHP script to load different page using URL Variable

Y ou should have many links like http://www.domain.com/index.php?page=home OR http://www.domain.com/index.php?page=about where different pages are shown from a single script. [In this case index.php is the script]. Now the question is how to make such kind of system…

diff-page-same-script

Basically, what we can do is, use PHP GET method to pass variable URL to the server and response different pages accordingly. Have a look at the online demo… If, you think you can get the concept from the source code, then here is the link:

Read on below to know how we can implement this…

#1: Strategy behind Coding:

  • We shall fetch the page variable from the URL using PHP GET method.
  • We shall make a function in which we will pass the page variable using argument.
  • Inside the function there will be a switch case to determine what page the client has requested for. We shall use different echo for different page requests…

#2: The main code to produce different page from the same script:

Keeping the above strategy in mind, here is the code… Note that we have assumed the script name to be diff-page.php. Change the links to match your file. Also check the Download Package to know how exactly it works:

'; case '': //When it is null case 'home': echo '

Welcome to the home page

'; echo '

This is the home page.

'; break; case 'about': echo '

Welcome to the about page

'; echo '

This is the about page

'; break; case 'contact': echo '

Welcome to Contact page

'; echo '

This is contact page

'; > > ?>

Home Page | About Page | Contact Page

That was pretty simple, right? Note that when we pass the URL as diff-page.php?page=contact PHP GETs the $_GET[‘page’] variable as contact. Then it passes that through the layout function which calls switch to echo relevant pages.

So, the only thing you have to take care is, providing exact file name of the script file [diff-page.php] and the page variables [home, contact, about].

Well that’s it. Although there are many other methods of doing this, but personally I use this as my preferable method. Do give us your feedback and suggestions. Also if you think you know a better algorithm, do share it with us!

Источник

Inside this article we will see the concept of find and extract all links from a HTML string in php. Concept of this article will provide very classified information to understand the things.

This PHP tutorial is based on how to extract all links and their anchor text from a HTML string. In this guide, we will see how to fetch the HTML content of a web page by URL and then extract the links from it. To do this, we will be use PHP’s DOMDocument class.

DOMDocument of PHP also termed as PHP DOM Parser. We will see step by step concept to find and extract all links from a html using DOM parser.

Inside this example we will consider a HTML string value. From that html value we will extract all links.

Create file index.php inside your application.

Open index.php and write this complete code into it.

  Google Youtube Online Web Tutor  "; //Create a new DOMDocument object. $htmlDom = new DOMDocument; //Load the HTML string into our DOMDocument object. @$htmlDom->loadHTML($htmlString); //Extract all anchor elements / tags from the HTML. $anchorTags = $htmlDom->getElementsByTagName('a'); //Create an array to add extracted images to. $extractedAnchors = array(); //Loop through the anchors tags that DOMDocument found. foreach($anchorTags as $anchorTag)< //Get the href attribute of the anchor. $aHref = $anchorTag->getAttribute('href'); //Get the title text of the anchor, if it exists. $aTitle = $anchorTag->getAttribute('title'); //Add the anchor details to $extractedAnchors array. $extractedAnchors[] = array( 'href' => $aHref, 'title' => $aTitle ); > echo "
"; //print_r our array of anchors. print_r($extractedAnchors);

Concept

When we run index.php. Here is the output

Inside this example we will use web page URL to get all links.

Create file index.php inside your application.

Open index.php and write this complete code into it.

loadHTML($htmlString); //Extract all anchor elements / tags from the HTML. $anchorTags = $htmlDom->getElementsByTagName('a'); //Create an array to add extracted images to. $extractedAnchors = array(); //Loop through the anchors tags that DOMDocument found. foreach($anchorTags as $anchorTag)< //Get the href attribute of the anchor. $aHref = $anchorTag->getAttribute('href'); //Get the title text of the anchor, if it exists. $aTitle = $anchorTag->getAttribute('title'); //Add the anchor details to $extractedAnchors array. $extractedAnchors[] = array( 'href' => $aHref, 'title' => $aTitle ); > echo "
"; //print_r our array of anchors. print_r($extractedAnchors);

When we run index.php. Here is the output

We hope this article helped you to Find and Extract All links From a HTML String in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Источник

I'm trying to figure out how to style my multi page links. I have a navbar in my index.php that opens other pages on the same page. I have another navbar on which I have some style:

This works for the normal links but I can't seem to use this on the multi page links. Am I doing it wrong? The link I want to style looks like this:

Answer

Solution:

I got the answer from my teacher. I'd like to share it with you.

This is from the multipage.php:

 [ "title" => "Intro", "subpage" => __DIR__ . "/multipage/index.php", ], "print-get" => [ "title" => "Print variable", "subpage" => __DIR__ . "/multipage/print-get.php", ] ]; $newPage = $multiPages[$pageReference] ?? null; if (!$newPage) < die("Fel, fel, fel. "); >$title = ""; $m_subpage = $newPage["subpage"]; include __DIR__ . "/view/header.php"; include __DIR__ . "/view/multi_page.php"; include __DIR__ . "/view/footer.php"; 

From the view/multi_page.php:

 

Multipage

You have selected something '' that does not map to an actual page.

Share solution ↓

Additional Information:

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

CSS

CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language. It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css

HTML

HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Latest questions:

778 php - Posts List not displaying in WordPress admin panel

151 php - How do I can resolve the error regarding "installable set of packages in Laravel 5.5 ( laravelcollective/html couldn't be install. )

665 php - Laravel and passport: POST method returns MethodNotAllowedHttpException

519 php - Socialite stateless auth

323 php - Save multiple inputs incremented

86 javascript - Client not receiving emails from page

116 Nested if statement in PHP MySQL SELECT

670 php - The queries on symfony4 / twig

121 php - Add a custom chosen date to cart item on add to cart action in Woocommerce

307 php - Change a specific product button text in a Woocommerce product category page

Users questions:

mysql

Warning: Undefined array key "program" in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key "enter_course" in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key "enter_address" in E:\xampp\htdocs\pi\alumni.php on line 32

Rename existing images with keyword in WordPress PHP

codeigniter

Class "web_profiler.controller.profiler" does not exist after upgrading symfony 3.4 to 4.4 version

© 2021-2023 Programming problem solving site for beginners and advanced. Answers to questions related to coding.

This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK

Источник

Оцените статью