Jump to content

line breaks in json reply doesn't show text


exhaler

Recommended Posts

Hi,

i have an article section in my website, when the user is reading a specific article there are two button previous and next, these show the previous or next article relative to the opened one.

 

i'm using jquery ajax to get the article:

$(document).ready(function() {
var loading = '<img src="images/ajax-loader.gif" alt="" />';

// Get next article
$("a.prnex").click(function() {
	// Show loading image
	$("#news").slideToggle("normal", function() {$("#news").html(loading);});

	if ($(this).attr('id') == 'next') { 
		var id = $(this).attr('next_id');
	} else {
		var id = $(this).attr('prev_id');
	}
	var data = 'id=' + id;			

	// Get next article
	$.ajax({
		dataType: "json",
		type: "POST",
		url: "includes/get_news.php",
		data: data,
		cache: false,

		success: function(json){
			$("#news").slideToggle("normal", function() {$("#news").html(json['html']);});

			// Change title of document
			document.title = 'Pamtek - ' + json['title'];

			// Change id of next article relative to the current one
			if (json['next_id'] != 0) {
				$("#next").show();
				$("#next").attr('next_id', json['next_id']);
			}

			if (json['prev_id'] != 0) {
				$("#prev").show();
				$("#prev").attr('prev_id', json['prev_id']);
			}

			if (json['next_id'] == 0) {
				$("#next").hide();
			}

			if (json['prev_id'] == 0) {
				$("#prev").hide();
			}					
		}						   	
	});	
});
});

 

get_news.php:

// Get title and description
$query = "SELECT * FROM news ";
$query .= "WHERE id = $id_main ";
$query .= "LIMIT 1";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

$title = stripslashes($row['title']);
$description = fulltext($row['description']);

// echo should have one quote ' otherwise it won't work with json
$display = '<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td valign=\"top\" height=\"1\" align=\"left\" style=\"text-align: justify;\"><font class=\"font_style\"><b>' . $title . '</b></font></td></tr><tr><td valign=\"top\" align=\"left\" style=\"padding-top: 5px; text-align: justify;\"><font class=\"font_style\" color=\"#616161\">' . $description . '</font></td></tr></table>';

// Get id of next news
$query_next = "SELECT id FROM news ";
$query_next .= "WHERE id > $id_main ";
$query_next .= "ORDER BY id ASC ";
$query_next .= "LIMIT 1";

// create json content
echo "{";
echo "\"html\":\"$display\",";
echo "\"title\":\"$title\",";
echo "\"next_id\":\"$next_id\",";
echo "\"prev_id\":\"$prev_id\"";
echo "}";

 

the above code is working if $description has no line breaks in it. i had to put all the html tags in $display on one line.

 

i.e: if the ajax response was like this:

{"html":"<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td valign=\"top\" height=\"1\" align=\"left\" style=\"text-align: justify;\"><font class=\"font_style\"><b>a</b></font></td></tr><tr><td valign=\"top\" align=\"left\" style=\"padding-top: 5px; text-align: justify;\"><font class=\"font_style\" color=\"#616161\">asdsadsadasdasd</font></td></tr></table>","title":"a","next_id":"0","prev_id":"97"}

 

it works fine, but if it was like this:

{"html":"<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td valign=\"top\" height=\"1\" align=\"left\" style=\"text-align: justify;\"><font class=\"font_style\"><b>22</b></font></td></tr><tr><td valign=\"top\" align=\"left\" style=\"padding-top: 5px; text-align: justify;\"><font class=\"font_style\" color=\"#616161\"><p>22

22</p></font></td></tr></table>","title":"22","next_id":"99","prev_id":"96"}

 

nothing shows up

i just need a way to fix the line breaks of json response.. any ideas??

 

Link to comment
Share on other sites

i found this code that helped

function parse($text){
    $parsedText = str_replace(chr(10), "", $text);
    return str_replace(chr(13), "", $parsedText);

}

 

if u have a better way to solve the problem please post it

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.