Append a Javascript Variable Name

meno19
meno19 used Ask the Experts™
on
This is most likely pretty simple but I am not getting it right. What I want to do is when creating a javascript variable is append a number to its name so instead of var test=5 have it be var test0 = 5. How do you append a variables name when creating it.

I am trying to append to the javascript since it is being created within a php loop.

<?php
	$sequence = 0;
		
	foreach($result_data as $test)
	{			
		?>
		<script type="text/javascript">	        					
			
			var contentString<?php echo $sequence;?> = '5';

		</script>        
		<?php
           $sequence++; 	
	}
	?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
Use a for loop instead of a foreach. Also, I'm assuming you just want one script with multiple variables? thus don't write the script tags in the loop.

<?php
      

        $count = sizeof($result_data); // result_data is an array?

        echo "<script type='text/javascript'>";
            
      for($i = 0; $i < $count; $i++)
      {                  
      echo "var contentString"  . $i . " = '5'";
        }

        echo "</script> ";
      

?>
Data Scientist
Most Valuable Expert 2014
Commented:
Actually you mightn't be able to use 'count' as a variable name - oops, sorry. It might be reserved.

<?php
      

        $numItems = sizeof($result_data); // result_data is an array?

           // alternatively:
           // $numItems = count($result_data);

        echo "<script type='text/javascript'>";
            
      for($i = 0; $i < $numItems; $i++)
      {                  
      echo "var contentString"  . $i . " = '5'";
        }

Open in new window


        echo "</script> ";
     

?>

Author

Commented:
Oh I see how your doing it. You are writing out the javascript via a php echo, doing the variable name setup in PHP. I've been trying to do it in javascript.

Why use a for instead of a foreach loop?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
a for loop will increment. A foreach just applies the same code to each item in the array

Author

Commented:
oh right, alright ill give that a shoot and see if setting the variable names like that would work. It should.
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
cool.

Author

Commented:
Nevermind I figured out my other question. Gonna try this next.

Author

Commented:
BRILLIANT. Worked like a charm 5 BILLION POINTS. Thank you for all the help.
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
That's great. For the 5 billion points just "Accept" an answer. :))

Koza.

Author

Commented:
LOL my Bad your all set now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial