6 Replies - 4260 Views - Last Post: 23 April 2011 - 11:02 AM

#1 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

How to insert a number from variable to a string ?

Posted 22 April 2011 - 04:44 PM

I want to know how I can get the values of variables passed to a string.

here is my code

echo"
<html>
<script type=\"text/javascript\" src=\"http:/mywebsite/folder_libs/jquery-ui-1.8.2.custom.min.js\"></script>
<script type=\"text/javascript\">
$(\"div,a,b,button,img\").draggable(); 
$(\"#contact_button\").draggable(\"handle: #try \");
$(\"#contact_button\").wrap( \"<div id='try'/>\" );
 $(\"#mad\").css(\"left\",\"-23px\");
  $(\"#contact_button\").css({\"left\":\"0px\",\"top\":\"10px\"});
  $(\"#contact_txt\").css({\"left\":\"38px\",\"top\":\"23px\"});
  $(\"*\").css(\"position:\",\"absolute\");
    $(\"input\").wrap(function(){
  var position_location = $(this).offset();
  var top_area = position_location.top;
   var left_area = position_location.left;
   var wide = $(this).css(\"width\");
   var subman = $(this).css(\"height\");
   var subnow = top_area - subman;
   var change = '0';
   
 return '<div id=\"try\" style=\"position:absolute;height:20px;width:\' + wide + \'px;background-color:blue;left:\' + left_area + \'px;top:\' + subnow + \'px;z-index:6;\'></div>';
 change++;
  });
</script></html>
";


I am using jquery inside a php file under a echo.

The problem.... you can see that the variable wide left_area are supposed to hold the css value and pass it to the html elements css values. Yet what I get is the name of the variable and no numbers tossed to it.

In the css values I just get the text of the variable no number. So of course the position goes haywire.

Any ideas how to get this to work???

Is This A Good Question/Topic? 0
  • +

Replies To: How to insert a number from variable to a string ?

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to insert a number from variable to a string ?

Posted 22 April 2011 - 05:37 PM

Hey.

The problem is that you are escaping the single-quotes in the return strings that are supposed to be closing/opening the string.
// This:
return '...\' + wide + \'...';

// Should be:
return '...' + wide + '...';




Also, check out the heredoc syntax for PHP strings. Makes printing large amounts of complex output a lot easier.
Was This Post Helpful? 0
  • +
  • -

#3 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: How to insert a number from variable to a string ?

Posted 22 April 2011 - 10:30 PM

View PostAtli, on 22 April 2011 - 06:37 PM, said:

Hey.

The problem is that you are escaping the single-quotes in the return strings that are supposed to be closing/opening the string.
// This:
return '...\' + wide + \'...';

// Should be:
return '...' + wide + '...';




Also, check out the heredoc syntax for PHP strings. Makes printing large amounts of complex output a lot easier.



Even, if this was inside a php script inside a echo statement?
Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to insert a number from variable to a string ?

Posted 22 April 2011 - 11:34 PM

Yes. Escaping single-quotes inside a double-quoted string doesn't work, and vice versa. (There is no need for it.)
The \ char will be included in the string as well.

Just take a look at the output your PHP generates:
<html>
<script type="text/javascript" src="http:/mywebsite/folder_libs/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript">
$("div,a,b,button,img").draggable(); 
$("#contact_button").draggable("handle: #try ");
$("#contact_button").wrap( "<div id='try'/>" );
 $("#mad").css("left","-23px");
  $("#contact_button").css({"left":"0px","top":"10px"});
  $("#contact_txt").css({"left":"38px","top":"23px"});
  $("*").css("position:","absolute");
    $("input").wrap(function(){
  var position_location = $(this).offset();
  var top_area = position_location.top;
   var left_area = position_location.left;
   var wide = $(this).css("width");
   var subman = $(this).css("height");
   var subnow = top_area - subman;
   var change = '0';
   
 return '<div id="try" style="position:absolute;height:20px;width:\' + wide + \'px;background-color:blue;left:\' + left_area + \'px;top:\' + subnow + \'px;z-index:6;\'></div>';
 change++;
  });
</script></html>


This post has been edited by Atli: 22 April 2011 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: How to insert a number from variable to a string ?

Posted 23 April 2011 - 12:16 AM

View PostAtli, on 23 April 2011 - 12:34 AM, said:

Yes. Escaping single-quotes inside a double-quoted string doesn't work, and vice versa. (There is no need for it.)
The \ char will be included in the string as well.

Just take a look at the output your PHP generates:
<html>
<script type="text/javascript" src="http:/mywebsite/folder_libs/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript">
$("div,a,b,button,img").draggable(); 
$("#contact_button").draggable("handle: #try ");
$("#contact_button").wrap( "<div id='try'/>" );
 $("#mad").css("left","-23px");
  $("#contact_button").css({"left":"0px","top":"10px"});
  $("#contact_txt").css({"left":"38px","top":"23px"});
  $("*").css("position:","absolute");
    $("input").wrap(function(){
  var position_location = $(this).offset();
  var top_area = position_location.top;
   var left_area = position_location.left;
   var wide = $(this).css("width");
   var subman = $(this).css("height");
   var subnow = top_area - subman;
   var change = '0';
   
 return '<div id="try" style="position:absolute;height:20px;width:\' + wide + \'px;background-color:blue;left:\' + left_area + \'px;top:\' + subnow + \'px;z-index:6;\'></div>';
 change++;
  });
</script></html>




When I tried your code. I get a parse error in my php file.

This code is inside a php echo statement using " " like echo" javascript code ";

that is why I have the / before the " ...

this is the error : Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /host/mysite.php on line 13

That is why I use the / before my " so that the php script dosen't close the echo statement ahead of time.
this is the typical error saying that echo " code " ; meaning if I don't \ a " then it thinks I am closing the echo statement. Which I won't until the end of the script.

Any ideas how to get around that?
Was This Post Helpful? 0
  • +
  • -

#6 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to insert a number from variable to a string ?

Posted 23 April 2011 - 12:31 AM

The code I posted was pure HTML/Javascript, not PHP. It is what your original PHP code generates. - I posted it to point out the \' chars in the return statement, not to be put into your code.


You do of course have to escape the double-quotes inside the double-quoted string, but you MUST NOT escape the single-quotes, unless you actually want the whole \' string to appear in your client-side code. (Which, in this case, you do not.)

// This is basically what you are doing:
echo "return '<div style=\"left: \' + left + \'px;\">';";

// But this is what you should be doing:
echo "return '<div style=\"left: ' + left + 'px;\">';";

// The former prints this:
return '<div style="left: \' + left + \'px;">';

// While the latter prints this:
return '<div style="left: ' + left + 'px;">';


See what I mean?

This post has been edited by Atli: 23 April 2011 - 12:32 AM

Was This Post Helpful? 1
  • +
  • -

#7 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: How to insert a number from variable to a string ?

Posted 23 April 2011 - 11:02 AM

View PostAtli, on 23 April 2011 - 01:31 AM, said:

The code I posted was pure HTML/Javascript, not PHP. It is what your original PHP code generates. - I posted it to point out the \' chars in the return statement, not to be put into your code.


You do of course have to escape the double-quotes inside the double-quoted string, but you MUST NOT escape the single-quotes, unless you actually want the whole \' string to appear in your client-side code. (Which, in this case, you do not.)

// This is basically what you are doing:
echo "return '<div style=\"left: \' + left + \'px;\">';";

// But this is what you should be doing:
echo "return '<div style=\"left: ' + left + 'px;\">';";

// The former prints this:
return '<div style="left: \' + left + \'px;">';

// While the latter prints this:
return '<div style="left: ' + left + 'px;">';


See what I mean?



Thanks, I understand better now. It's working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1