Change the content with the url on php? - php

I have a file named friend.php and I want change the friend.php content doing this:
friend.php?content=1, friend.php?content=2
Searching on google I can find change elements from url but I want change the content complete for example have content1.php, content2.php... but show that only doing this with the url.

Access this using $_GET superglobal:
<?php
if ($_GET["content"] == "1") {
// show content1.php
} else if ($_GET["content"] == "2") {
// show content2.php
}
?>

Related

Changing logos of site based on class - PHP Wordpress

I have this code in my header.php to change the website's logo based on the page's slug:
$logo_img = 'default';
if (is_page('brookside')) {
$logo_img = 'brookside';
} elseif (is_page('hilltop')) {
$logo_img = 'hilltop';
} elseif (is_page('reserve')) {
$logo_img = 'reserve';
}
Which works great. However, the pages a need to change the logo are query string URL, so they don't have slugs. I was able to add a unique class to each page. My question is:
How do I translate that code to "if a page has "x" class? So, what function should I use instead of is_page?
Any help would be very appreciated!
Thanks!
You can use:
if($_GET['ct_additional_features'] == 'brookside') {
$logo_img = 'brookside';
}
You could use get_class that outputs the Class Name and there solve your if with what are you saying

No navigation bar displayed after log in

I have 3 different navigation bars, a different 1 has to be loaded depending on the authority of the user. I have made a function in a different php file so i re use the code in each page. My problem is that after logging in , no navigation bar is being loaded. any suggestions to what might be the problem ?
Code in function.php file
function checkAuth()
{
session_start();
if(empty($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
include('regnav.php');
}
else if ($_SESSION['role'] =="admin"){
include('adminnav.php');
}
}
code in the begging of each page
<?php
require_once("function.php");
checkAuth();
?>
Try passing the session result into the function:
place session call at the top of the rendering page
session_start();
$this_session = $_SESSION['role'];
require_once("function.php");
place where you want to render menu
checkAuth($this_session);
the function
function checkAuth($this_session)
{
if($this_session == ''){include('menu.php');}
elseif($this_session == 'registered'){include('regnav.php');}
elseif($this_session == 'admin'){include('adminnav.php');}
}
I would personally restructure your pages just a little, like this:
At the top of each page put:
session_start();
include('function.php');
checkAuth();
Change the function to:
function checkAuth()
{
if (!isset($_SESSION['role'])) { require_once('menu.php'); }
if ($_SESSION['role'] == 'registered') { include('regnav.php'); }
if ($_SESSION['role'] == 'admin') { include('adminnav.php'); }
}
This assumes you aren't using functions in your navbar files, since you don't call any functions to write the navbar. You could, of course, put both navbars into functions and put both functions into a single navbar.php file that you would include at the top of every page (or put the functions into function.php), then call the appropriate navbar function from the checkAuth() function, like this:
if ($_SESSION['role'] == 'registered') { navbar_registered(); }
if ($_SESSION['role'] == 'admin') { navbar_admin(); }

PHP changing your page name

I have a portfolio that shows 3 rows of images in one page.
it is called portfolio.php.
I want to make it so when you will click on next button, it will go to the next page (portfolio2.php) with the same content, just different image URLs and rename that page to portfolio.php?page=2
but without using a pagination script.
Is there a way to change that pages name without using any advanced PHP script for paging?
Some friend told me to do this way:
<?php
// Get the page ID
$page_id = $_GET['page'];
// Create the page string
$page_string = "images_page" . $page_id . ".php";
// Include the page
include($page_string);
?>
But im not to sure how as I am getting Internal server error with this script.
Is there any way to rename a specify page to something like portfolio.php?page=2 ? something like (location: ) or something..
I want to do it this way because I am really clueless with PHP.
Thank you.
You can do it like this....
if(isset($_GET['page']) && $_GET['page'] != '') {
$pageno = $_GET['page']; /* Sanitize Here */
} else {
/* You can redirect user to home page */
}
if(isset($pageno)) {
if($pageno == '1') {
/* Page 1 content */
} elseif($pageno == '2') {
/* Page 2 content */
}
}
Note:- Always sanitize your data

WP WordPress theme issue

I am building a free WP theme but know next to nothing about PHP. I don't know what to put around this code and would greatly appreciate if somebody could put the correct PHP tags around it, as telling me what to put is a bit like speaking Latin to me. I tried putting <?php at the beginning and ?> at the end but it didn't work:
if ( has_post_format( 'aside' ) {
// aside format post content here
} else if (has_post_format('gallery')) {
// stuff to display the gallery format post here
} else if (has_post_format('link')) {
// link format post content here
} else if (has_post_format('video')) {
// video format post content here
} else if (has_post_format('audio')) {
// audio format post content here
} else if (has_post_format('status')) {
// status format post content here
} else if (has_post_format('chat')) {
// chat format post content here
} else if (has_post_format('quote')) {
// quote format post content here
} else if (has_post_format('image')) {
// image format post content here
}else {
// code to display the normal format post here
}
Put <?php in front of every line that does not hold any code (thus starts with if or }),
and end those lines with ?>.
Also, this first line is wrongs.
if ( has_post_format( 'aside' ) {
should be
if ( has_post_format( 'aside' ) ) {

PHP function to check if 2 variables are empty

I have an index page, I want it to include a page called splash.php and not display.php when a user lands on index.php, but once a user does something (sets a variable) ie if a user searches (variable "query") i want it to include display.php and not include splash.php
What is wrong with this code?
function hasGet()
{
return !empty($_GET['fact']);
return !empty($_POST['query']);
}
if (hasGet()) {
include("display.php");
}
else {
include("splash.php");
}
This question should be removed
Only the first return statement is executed. Try:
return !empty($_GET['fact']) && !empty($_POST['query']);
A better way to accomplish what you are trying to do is use sessions.
index.php
<?php
session_start();
if (!isset($_SESSION['visited'])) {
$_SESSION['visited'] = true;
include 'splash.php';
} else {
include 'display.php';
}
?>
This way after a user visits index.php for the first time, $_SESSION['visited'] is set to true and it won't show the splash page throughout their visit.
You cannot have two returns as you are doing. Try
return (!empty($_GET['fact']) && !empty($_GET['query']));
You might want to try this...
if($_SERVER["SCRIPT_NAME"] == "/index.php"){
include("splash.php");
}else{
include("display.php");
}
2.
if(!empty($_GET["fact"]) || !empty($_POST["query"])){
include("display.php");
}else{
include("splash.php");
}

Categories