Jump to content

space in link text problem


samoht

Recommended Posts

Hello,

 

I am having a problem with a link text when ever there is a text that has multiple words.

(e.g. 2 Cor prints out with the white space but when I click on the text the url is printed without the text as 2Cor and so it doesn't find a match)

 

here is my code:

<?php 
if($_GET['book'] == ''){
$books = $wpdb->get_results("
	SELECT book_name 
	FROM {$wpdb->prefix}sb_books_sermons 
	LEFT JOIN {$wpdb->prefix}sb_books ON {$wpdb->prefix}sb_books_sermons.book_name = name 
	GROUP BY book_name 
	ORDER BY {$wpdb->prefix}sb_books.id 
	ASC 
	");

	foreach($books as $book){
		echo '<li><div class="droidlist"><a href="bible/?book='.htmlentities($book->book_name).'">'.$book->book_name.'</a></div></li>'."\n\t";
	}
}

 

Link to comment
https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/
Share on other sites

alright,

 

I changed the htmlentities to preg_replace - to search for any spaces and replace with %20  like so:

<?php 
	foreach($books as $book){
		echo '<li><div class="droidlist"><a href="bible/?book='.preg_replace('[\s]', '%20', $book->book_name).'">'.$book->book_name.'</a></div></li>'."\n\t";
	}
}

 

This loos like it should work but again when I click on the link the url removes the %20 and the space and I am back with 2Cor

??

 

 

ok,

 

I am trying rawurlencode() now and it is writing in the %20

then ON the page reload when I get the variable from the url I am using rawurldecode()

 

but the space is still being removed before I run my query??

 

<?php
if($_GET['book'] == ''){
$books = $wpdb->get_results("
	SELECT book_name 
	FROM {$wpdb->prefix}sb_books_sermons 
	LEFT JOIN {$wpdb->prefix}sb_books ON {$wpdb->prefix}sb_books_sermons.book_name = name 
	GROUP BY book_name 
	ORDER BY {$wpdb->prefix}sb_books.id 
	ASC 
	");

	foreach($books as $book){
		echo '<li><div class="droidlist"><a href="bible/?book='.htmlentities(rawurlencode($book->book_name)).'">'.$book->book_name.'</a></div></li>'."\n\t";
	}
}else {
$book = $wpdb->escape(rawurldecode($_GET['book']));

So, I got it to work sort of!

 

I removed the relative link and for some reason that worked. So then I tried just shortening the relative link to

<a href="?book='.rawurlencode($book->book_name).'">'

Which also worked. - not sure why adding the /bible/ in front of the query broke it?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.