Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2007, 02:14 PM   PM User | #1
tannu
New Coder

 
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
tannu is an unknown quantity at this point
formatting the data

Hello all,

I am having a PHP script that generates the xml using the data in a mysql database. We have "nl2br" function in PHP that preserves the format (<br /> <p> etc..). When i use this method to generate the xml, the xml file that gets generated contains the <br /> tag in the node tree. Is there any other way to preserve the format even in the generated xml, without having the <br /> tags.

Following is the PHP:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("xplane");
$query = "SELECT * FROM categorythreads";
$result = mysql_query($query);
header("Content-type: text/xml");
$xml = "<?xml version='1.0' encoding='utf-8'?>";
$xml .= "<category>";
while($fetchedThreads = mysql_fetch_object($result))
{
$xml .= "<threads>";
$xml .= "<threadID>" .$fetchedThreads -> threadID. "</threadID>";
$xml .= "<threadCategory>" .$fetchedThreads -> threadCategory ."</threadCategory>";
$xml .= "<threadTitle>" .$fetchedThreads -> threadTitle ."</threadTitle>";
$xml .= "<threadDetails>" .$fetchedThreads -> threadDetails ."</threadDetails>";
$xml .= "<postedDate>" .$fetchedThreads -> postedDate ."</postedDate>";
$xml .= "<permaLink>" .$fetchedThreads -> permaLink ."</permaLink>";
$xml .= "</threads>";
}
$xml .= "</category>";
echo nl2br($xml);
mysql_free_result($result);
mysql_close();
?>

generated xml:
<category>
<threads>
<threadDetails>"I typically use this column to look at future trends."
<br />
"I typically use this column to look at future trends."</threadDetails>
</threads>
</category>

Javascript statement that places the nodevalue:
var threadDetails = result.getElementsByTagName("threadDetails")[i].firstChild.nodeValue;
document.getElementById("thread").innerHTML = threadDetails;

html:
<div id="thread">"I typically use this column to look at future trends."</div>

expected html:
<div id="thread">
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."
</div>

please do help me out,

thanks in advance,
tannu
tannu is offline   Reply With Quote
Old 06-26-2007, 02:16 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Try to use \n instead of <br>

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 06-26-2007, 02:43 PM   PM User | #3
tannu
New Coder

 
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
tannu is an unknown quantity at this point
<br /> tag is added by the php script generating the xml. Basically what i am trying to do is to fetch and display the two paragraphs stored in the database. Please tell me how to make the xml parser to display two seperate lines as two seperate lines instead of one.
ex:
<?xml version="1.0" encoding="utf-8" ?>
<category>
<thread>
"This is first line..."
"This is second line..."
</thread>
</category>
if i use javascript to read this xml and display the output, the output shuld read
"This is first line..."
"This is second line..."

not
"This is first line...""This is second line..."

please do help me out,

thanks,
tannu

Last edited by tannu; 06-26-2007 at 02:51 PM.. Reason: was not complete
tannu is offline   Reply With Quote
Old 06-26-2007, 03:14 PM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
document.getElementById("thread").innerHTML = threadDetails.replace(/\n/gi,"<br/>");

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 06-26-2007, 04:03 PM   PM User | #5
tannu
New Coder

 
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
tannu is an unknown quantity at this point
Hello Eric,

its not working.

dynamically generated xml is (thru php, using data stored in mysql, two parargraphs that read:"I typically use this column to look at future trends.")

<category>
<threads>
<threadDetails>"I typically use this column to look at future trends."
<br />
"I typically use this column to look at future trends."</threadDetails>
</threads>
</category>

I am using DOM to generate the div and related elements and appending the related result. Following is the snippet:

xmlhttp_categoryThreads is XMLHTTPRequest object

result = result = xmlhttp_categoryThreads.responseXML;

var threadDetails = result.getElementsByTagName("threadDetails")[i].firstChild.nodeValue;
var p_threadDetails = document.createElement("p");
var txt_threadDetails = document.createTextNode(threadDetails);
p_threadDetails.appendChild(txt_threadDetails);
div_categoryThread.appendChild(p_threadDetails);

intended o/p is:
<div id="div_categoryThread">
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."
</div>

o/p i am getting:
<div id="div_categoryThread">
"I typically use this column to look at future trends."
</div>

the problem, what i think may be the <br /> tag that the XML is having,

is there any way to generate the xml having the same format as the data fetched by the database?


the data stored in the mysql:
"I typically use this column to look at future trends."
"I typically use this column to look at future trends."

I am using the nl2br formatting function in php, which basically preservs the format, in this case a <br /> tag.

please tell me where i am going wrong, and suggest any other alternative to achieve the desired result.

thanks again,
tannu
tannu is offline   Reply With Quote
Old 06-26-2007, 04:23 PM   PM User | #6
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Man I am not with it today! LOL

If you have HTML tags in there, you should look into using cdata

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 06-27-2007, 02:12 AM   PM User | #7
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
Remember for small data sets with ajax you do not have to use XML at all. I programmed an ajax site using PHP and just used comma delimited data strings so I could parse them easily back in the client with JS. XML wasn't needed at all to pass the data back to the client thru the "ajax tunnel".
__________________
RalphF
Business Text Messaging Services
https://www.MobileTextingService.com
rfresh is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:12 AM.


Advertisement
Log in to turn off these ads.