/    Sign up×
Community /Pin to ProfileBookmark

Javascript String problem

Hello,
I work in PHP and dont know much about javascript. I am trying to create a XML Parser using PHP.I want the output in a javascript string.

This is the part where i am saving the code in javascript string.

if(count($new_array)<=0)
{
[COLOR=”Blue”]$add=”code=”<table width=’500′ cellpadding=’4′ align=’center’ bgcolor=’#CCCCCC’>”;”;
$add.=”code+=”<tr><td align=’center’ class=’text’>”;”;
$add.=”code+=”<b>No Match Found….</b>”;”;
$add.=”code+=”</td></tr></table>”;”;[/COLOR]

}
else
{
////////some php code///////////////

if(is_array($bid_id_array))
{

[COLOR=”Blue”]$add=”code=”<table width=’500′ cellpadding=’2′ align=’center’><tr><td>”;”;[/COLOR]
foreach ($bid_id_array as $key => $val )
{
$limit_count++;
if($limit_count>$final_total_rec) break;
$title=$new_array[‘title’][$key] ;
$link=$new_array[‘link’][$key] ;
$desc=$new_array[‘desc’][$key];
$bid=$new_array[‘bid’][$key];
[COLOR=”Blue”]$add.=”code+=”<p id=’results’><a href='”.$link.”‘ target='”.$target.”‘><b>”.$title.”</b></a><br>”.$desc.”<br></p>”;”;[/COLOR]

}//for each loop

[COLOR=”Blue”]$add.=”code+=”</td></tr></table>”;”;[/COLOR]
}//if
else
{
[COLOR=”Blue”]$add.=”code+=”<table width=’500′ border=’0′ cellpadding=’2′ align=’center’ bgcolor=’#CCCCCC’><tr><td class=’text’ align=’center’><b>No Match Found….</b></td></tr></table>”;”;[/COLOR]
}
[COLOR=”Blue”]$add.=”document.write(code);”;
print(html_entity_decode($add));[/COLOR]

} //else

I am getting the output properly but in between [COLOR=”Red”]“code=”[/COLOR] and [COLOR=”Red”]“;code+=”[/COLOR] is displayed between the output lines.Also [COLOR=”Red”]“;document.write(code);[/COLOR] is displayed at end of output page.

I know this problem has to do something with unterminated string.So please the above code and let me know where excatly is the problem with javascript string concatenation.

thanks in advance,
coolbuddy

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@davidbaltAug 21.2008 — Save yourself a heck of a lot of trouble and use a JSON function or class in PHP to create the JS strings for you. Google Services_JSON and you'll find a very easy to use class that will do this for you. Your code will end up looking like this:

$J = new Services_JSON;

$js = $J->encode( $big_messy_string );

echo "code = eval("$js")"

// or maybe

echo "code = "$js"";

Hope that helps.
×

Success!

Help @coolbuddy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...