Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can i make a loop like this one in Javascript?

foreach ($viewData['cure'] as $cure) 

In this loop I would like to print the result in JS

$("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="<?php echo (string)$cure["id_type"] ?>"/>');
share|improve this question
1  
What's wrong with a normal for loop? –  Amal Murali Feb 11 at 16:54
    
Try jQuery's .each() function api.jquery.com/jquery.each –  Ruben Feb 11 at 17:06

5 Answers 5

Javascript doesn't have foreach like PHP, you need to use a regular for loop.

var a = ["a", "b", "c"];
for (var index = 0; index < a.length; ++index) {
    console.log(a[index]);
}

(from https://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript)

share|improve this answer

You can simply combine both like this

<script type="text/javascript">
<?php foreach ($viewData['cure'] as $cure): ?>
$("#new").append('<label for="nice_text<?php echo $cure["id"] ?>">ID Type</label><input type="text" id="nice_text<?php echo $cure["id"] ?>"/>" name="cureIdtype" class="input-text" value="<?php echo (string)$cure["id_type"] ?>"/>');
<?php endforeach; ?>
</script>

Just make sure your Ids are unique (I added $cure["id"] as an example), you might also wanna change the name="cureIdtype", depending on what you want to do with the input afterwards.

share|improve this answer

If you want to do this in PHP and echo the content to the browser, it would look like this:

foreach ($viewData['cure'] as $cure) {
    echo '$("#new").append("<label for=\'nice_text\'>ID Type</label><input type=\'text\' id=\'nice_text\' name=\'cureIdtype\' class=\'input-text\' VALUE=\'' . (string)$cure['id_type'] . '\'/>")';
}

If you have a JavaScript array and want to handle this there, it would look something like this:

for (var i = 0; i < viewData['cure'].length; ++i) {
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="' + viewData['cure'][i]['id_type'] + '"/>")';
}

These examples are untested, and littered with a variety of single/double quotes and escapes, so I could have made some typos in there.

share|improve this answer

Javascript Array forEach() Method


Description:

Javascript array forEach() method calls a function for each element in the array.

Syntax:

array.forEach(callback[, thisObject]);

Here is the detail of parameters:

  • callback : Function to test each element of the array.
  • thisObject : Object to use as this when executing callback.

Return Value:

Returns created array.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

Example:

<html>
<head>
<title>JavaScript Array forEach Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function printBr(element, index, array) {
  document.write("<br />[" + index + "] is " + element ); 
}

[12, 5, 8, 130, 44].forEach(printBr);

</script>
</body>
</html>

This will produce following result:

[0] is 12
[1] is 5
[2] is 8
[3] is 130
[4] is 44 

To understand it in better way you can Try it yourself.

SOURCE

share|improve this answer
var a = ["a", "b", "c", "d"];
a.forEach(function(data) {
    console.log(data);
});

I don't understand what is the problem with your code...

<script>
var array = [];
<? foreach ($viewData['cure'] as $cure) {?>
    array.push(<? echo $cure?>);
<? }?>
array.forEach(function(value){
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="'+value+'"/>')
});

</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.