Get slug and write content with dashes - php

i'm having a problem with getting content from pages where the slug is seperated with dashes. for example if i go to www.example.com/home it works fine but if i go for example to www.example.com/about-us it gets an error Trying to get property of non-object wich as the error says is regarding this lines;
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
I have a function that get's the slug from the database that looks like this;
function getSlug($name = null) {
global $db;
if ($name) {
$query = $db->prepare('SELECT * FROM pages WHERE `Title` = ? LIMIT 1');
$query->execute(array($name));
return $query->fetchObject();
}
}
I also have a function that creates the slug that looks like this;
function createSlug($string){
$string = preg_replace( '/[«»""!?,.!#£$%^&*{};:()]+/', '', $string );
$string = strtolower($string);
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
In my htaccess i have got the rule RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?name=$1 [L].
to get the content i used the following code;
<?php
include 'header.php';
$slug = create_slug($_GET['name']);
$page = getSlug($slug);
?>
<div class="container">
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
<?php include 'footer.php'; ?>
I really don't have any clue what the problem is. Any help would be much appreciated.
Thanks in advance. Kind regards

Check ModRewrite module enabled and also, in .htaccess check the below line is added in top.
RewriteEngine On

I have solved the problem by adding a row in my databse called Slug, i think this will also be better if i want the slug to be different then the title.
Thanks for the efforts #sk8terboi87

Related

Simple Shortcode (Wordpress) does not work

This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php
For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );
put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>
Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.

CodeIgniter title won't work

I understand an approach to naming declaring a title for a page in CI can be done like this, which has in the past and if anything within an existing project works.
$data['title'] = "myTitle";
$this->load->view("content_home", $data);
But for the life of me, now it just will not work!! :(
Anybody any ideas as to why?
In order to declare a title to a page, you type this in the controller relevant to a page example:
public function home(){
$data["title"] = "my Title";
$this->load->view("content_home", $data);
}
Within the view in order for this to work this must be inserted:
<title><?php echo $title; ?></title>
I had not realised, but this worked for me now after I went back and checked my files properly in my previous project, sorry guys and thanks for the responses.
just fyi you can check if title has been set and if not show something else
<title><?php
// check if $title has been set
if( isset($title) ){ echo $title; }
// else display generic title
else { echo "A Website Title" ; }
?></title>
that way you will always be covered

PHP: How to set dynamic titles while auto_prepend_file is being used?

The title says it all: I want the page to determine the title, but the title is being set before the page is being read (I think). Is there a way to accomplish this, or am I doomed to include the header on each individual page?
Here's what I have:
php.ini:
auto_prepend_file = "header.php"
header.php:
<?php
if (isset($title) == false) {
$title = "foobar";
}
$title = "My Site : " . $title;
?>
<title><?php echo($title) ?></title>
index.php
<?php
$title = "Home"; // ideally this would make the title "My Site : Home"
?>
Instead of using auto_prepend_file, I would just use:
include 'header.php';
An important reason why I wouldn't use auto_prepend_file is, if you move to another server, you'll have to remember to edit the php.ini. If you just include the file, you can move your code to any server.
Also, just like Fred-ii- said, I wouldn't use parenthesis. Also, you are missing a semi-colon after the echo.
To take that a step further, I would create a file called something like $config.php or $vars.php. Include that before everything and have it define all your global variables and constants.
I would check this out: http://php.about.com/od/tutorials/ht/template_site.htm
This is not an ideal answer, but I could use CGI variables to get the name of the page, then turn that into a title.
function get_title($page){
$title = str_replace("/", "", $page);
$title = str_replace("_", " ", $title);
$title = str_replace(".php", "", $title);
$title = ucfirst($title);
if($title = "Index"){
$title = "Home";
} elseif ($title == "") {
$title = 'Foobar';
}
return $title;
}
$title = get_title($_SERVER["PHP_SELF"]);
$title = 'My Site: ' . $title;
As a follow-up to my original comment, I'm posting this as an answer because while it doesn't specifically solve the problem, it addresses the underlying cause.
Disclaimer: The code below has many problems, especially security, it's not meant to be copied directly but only explains the concept.
What you need to do is have a container file that includes your headers and whatever else, and each PHP file is included from there. For example, name your container index.php, and have the following in it:
<?php
include 'header.php';
if ($_GET['page'])
include $_GET['page'].'.php';
include 'footer.php';
?>
Then each PHP page you have will be wrapped in the index.php file, and you can add whatever you want in the header file which will be included in all of your files. That way you don't have to include anything in the individual page files.
The client will access your pages with a query string, such as: index.php?page=test
Again, for security reasons you will still want to include basic checks in each individual file, but technically this can be avoided in you plan for this. You definitely won't need to include huge headers in each file, like MySQL connections etc. Also for security you should have stringent checks on your $_GET variables to make sure that only the pages you want can be included.
I'd define a writeTitle (or similar) function in the header.php file which you're auto_prepending:
header.php
<?php
function writeTitle($title = 'foobar') {
$title = "My Site : " . $title;
return '<title>' . $title . </title>';
}
And then you can just call the function from your page scripts instead of setting a variable:
index.php
<?php
echo writeTitle('Home');

output 2 string in one function

Ok, I must admit, I am a rather PHP noob who struggles with what seems to me a simple issue.
What i want to accomplish is that I can put 2 strings in 1 function and echo them in different places.
In common.php i have this:
<?
function printHeader($titel) {
?>
Later on in the file i echo it between
And in index.php i have this:
<?
include "common.php";
printHeader('this is my title');
?>
This works alright.... now what i like to do is to ad another String to the printheader to not only echo the title but also the H1, so i tried this:
Common:
<?
function printHeader($titel . $headtitle) {
?>
Index:
<?
include "common.php";
printHeader('This is my title!' . 'This is my H1');
?>
This does not seem to work. Are there any phpsavvy guys out here who can help me wit this simple problem?
If it is not to much to ask I would also like to Echo some standard value if $headtitle is empty, but that is waaaay of my league :)
EDIT: thanks to you guys the first problem is fixed. Now i want to try and fix the IF empty part. So I came up with this:
<?
function printHeader($titel, $headtitle) {
if (empty($headtitle)) {
echo 'title is empty'; }
?>
html goes here + this: <h1><?=$headtitle; ?></h1> more HTML
<? } ?>
This does not seem to work...
any help would be appreciated!
Thanks in advance,
A simple webdesigner ;)
Function arguments should seperate by a comma ,
function printHeader($titel , $headtitle)
And obviously same for calling,
printHeader('This is my title!' , 'This is my H1');
You have to seperate the second the argument by , like this
function printHeader($titel , $headtitle)
and change while calling also
printHeader("string1","String2");
or keep like this
function printHeader($titel){..... }
printHeader("string1"."String2");
so $title1 will have appended string.

Variable auto_prepend_file?

Okay, I suck at titling my questions XD If anyone has a better title, please edit!
Anyway, I have this in my php.ini file:
auto_prepend_file = ./include/startup.php
auto_append_file = ./include/shutdown.php
startup.php looks something like this:
<?php
chdir(__DIR__."/..");
require("include/db.php");
// more setup stuff here
if( $_SERVER['REQUEST_METHOD'] == "AJAX") { // yup, custom HTTP method :p
// parse file_get_contents("php://input")
}
else {
$output_footer = true;
require("include/header.php");
}
shutdown.php goes something like this:
<?php
if( isset($output_footer)) require("include/footer.php");
Now, on to the main problem.
header.php includes the line <title>My shiny new site</title>. I want that title to depend on the page that is currently being viewed. At the moment, I have some ugly hack that involves JavaScript:
document.title = document.getElementsByTagName('h1')[0].textContent;
Obviously, this is not ideal! Any suggestions on how to adjust the title when said title is being output as a result of an auto_prepend_file?
I don't think you should use the append and prepend functionality like that But that's just my opinion.
Why not create header.php to allow for a variable title text. And drop the prepend and append scripts completely
<title><?php echo (isset($title)) ? $title : 'default title'; ?></title>
And whenever you need a title tag.
$title = "some title";
require("include/header.php");

Categories