TrippedStackers
TrippedStackers

Reputation: 423

PHP URL configuring

I have a quick question.

<a href="#/media/<?php echo $row['id'] ?>.<?php echo $row['news_title'] ?>">Read More</a>

Basically, it'll show my URL once I click on "Read More". I added the

.<?php echo $row['news_title'] ?>

before the ">

basically, it'll show ID.THE-TITLE-HERE

However, it's not working, it does work when i manually add in the "-" in the url for the article after the ".", but it doesn't work.

You'll see what I mean if you go to http://www.krissales.com/#/media/blog

hit "Read More" and it wont show anything, but if you add a "-" between "testing" and "2" it'll work.

Would anyone know how I could go about fixing this? Just tell me what I can do, I wanna learn at the same time while doing it.

the full .php page is at krissales.com/blog.txt

Thanks for your time.

Upvotes: 1

Views: 80

Answers (3)

Michael Muryn
Michael Muryn

Reputation: 91

I noticed someone else gave the simple answer while I was typing this, but I think my answer will help you understand a bit further.

First the tests:

  • "http://www.krissales.com/media/3" works
  • "http://www.krissales.com/#/media/3" works
  • "http://www.krissales.com/#/media/3.Testing 3" does not works.
  • "http://www.krissales.com/#/media/3.Testing-3" works.
  • "http://www.krissales.com/#/media/3.Testing%203" works (%20 being the URL Encoded equivalent of a space).

The problem is probably how you handle the hash tag, etc.

First, with what I know, you could get it to works multiple way:

  1. Instead of: <?php echo $row['news_title'] ?> You could use <?php echo urlencode($row['news_title']) ?> Which will make sure there is no space used.

  2. If you prefer to replace space with dash, then you can use str_replace() function (http://www.php.net/str_replace) like this: <?php echo str_replace(' ', '-', $row['news_title']); ?>

This will work, however, I do not feel this fix the problem at core, which is probably where you handle the call (code in your /_lib/_js/core.js file), etc.

P.S. You will want to make sure you escape URL variable, HTML content, etc. -- see these function in PHP urlencode(), htmlspecialchars() -- but if you build code with other language (like javascript), the same logic apply. This might somehow hide a core problems in your code with how you handle URL, etc. It is the source of multiple problems in software development. ;-)

Upvotes: 2

doublesharp
doublesharp

Reputation: 27599

If $row['id'] is 99 and $row['news_title'] is "The title" then the following code will use str_replace to output "99.The-title".

<a href="#/media/<?php echo $row['id'] ?>.<?php echo str_replace(" ", "-", $row['news_title']); ?>">Read More</a>

Upvotes: 1

Oras
Oras

Reputation: 1096

Change

echo $row['news_title']

To

echo str_replace(" ","-",$row['news_title']);

This will replace the white space with hyphen

Upvotes: 2

Related Questions