for-loop

Use value of array in for loop outside for loop

冷暖自知 提交于 2020-01-06 20:12:02
问题 I have the below code. I have a form setup that has 10 input fields set up, 5 for column1 and 5 for column2. try { $count = count($_POST['column1']); $sql = 'INSERT INTO tablename (column1, column2) VALUES '; for ($i=0; $i< $count; $i++) { $sql2 = '(' .$_POST['column1'][$i] . ', ' . $_POST['column2'][$i] . ')'; if ($i <$count - 1) { $sql2 .= ','; } echo "$sql2 'indside'<br>"; } echo "$sql2 'outside'<br>"; $sql3 = "$sql $sql2"; $pdo->exec($sql3); } catch (PDOException $e) { $output = 'Error: '

Local variable cannot be resolved?

拟墨画扇 提交于 2020-01-06 19:57:03
问题 In this code, I am putting the values of MySQL database into String array(list[count]) and count=number of rows in table in database. And while I am applying for-loop to print these values in the drop-down list in javascript, using j variable, compiler is not able to recognize the j . I am not able to understand why. Please help me. <%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Driver"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql

Local variable cannot be resolved?

匆匆过客 提交于 2020-01-06 19:56:43
问题 In this code, I am putting the values of MySQL database into String array(list[count]) and count=number of rows in table in database. And while I am applying for-loop to print these values in the drop-down list in javascript, using j variable, compiler is not able to recognize the j . I am not able to understand why. Please help me. <%@page import="java.sql.DriverManager"%> <%@page import="com.mysql.jdbc.Driver"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql

Javascript - loop through the same array more than once

依然范特西╮ 提交于 2020-01-06 19:48:52
问题 How do I get the below for loop statement to go through my array more than once? var clients = new Array("client1", "client2", "client3", "client4", "client5"); var indexCounter; var holmesminutes = 0; for (indexCounter = 0; indexCounter < clients.length; indexCounter++) { holmesminutes = holmesminutes + 15; document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>"); } 回答1: Put another loop around the loop. for (var repeatCounter = 0; repeatCounter < 5; repeatCounter++) {

Javascript - loop through the same array more than once

筅森魡賤 提交于 2020-01-06 19:48:09
问题 How do I get the below for loop statement to go through my array more than once? var clients = new Array("client1", "client2", "client3", "client4", "client5"); var indexCounter; var holmesminutes = 0; for (indexCounter = 0; indexCounter < clients.length; indexCounter++) { holmesminutes = holmesminutes + 15; document.write(clients[indexCounter] + " testroom " + holmesminutes +"<br>"); } 回答1: Put another loop around the loop. for (var repeatCounter = 0; repeatCounter < 5; repeatCounter++) {

loop and combine every two items

[亡魂溺海] 提交于 2020-01-06 18:02:31
问题 Ive been trying to combine two items together on each loop, in the result there is a null, how do i check the "items" so not to get null's? var oTitle = [], items = ['a1', 'a2', 'b1', 'b2', 'c1', 'c2']; for (var i=0; i < items.length; i++) { oTitle[i] = { one: '' + items[i], // items[i].value two: '' + items[++i] }; } console.log(JSON.stringify(oTitle)); // [{"one":"a1","two":"a2"},null,{"one":"b1","two":"b2"},null,{"one":"c1","two":"c2"}] 回答1: Because you change the i in the for loop's body.

Deleting an object based on the id in javascript

纵饮孤独 提交于 2020-01-06 17:01:11
问题 This is a follow up of Pushing an object into array where I was pushing an object into the array by identifying the parentActivityId. Now I wanted to remove the object based on its id.I have tried the below code based on the follow up question but its not working.Can anyone tell me what I'm doing wrong here? function getParent(r, a) { return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r); } var node = data.reduce(getParent, {}); 'items' in node && node.items.splice(child,1

Deleting an object based on the id in javascript

陌路散爱 提交于 2020-01-06 17:01:05
问题 This is a follow up of Pushing an object into array where I was pushing an object into the array by identifying the parentActivityId. Now I wanted to remove the object based on its id.I have tried the below code based on the follow up question but its not working.Can anyone tell me what I'm doing wrong here? function getParent(r, a) { return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r); } var node = data.reduce(getParent, {}); 'items' in node && node.items.splice(child,1

MATLAB Slicing variable for PARFOR loops

我与影子孤独终老i 提交于 2020-01-06 15:18:28
问题 I am trying to make the following loop parallel-friendly in MATLAB so that I can use parfor : for ivert = 1 : nVerts b = obj.f( obj.neighIDs{ ivert } ); x = obj.coeffMatrix{ ivert } \ b; obj.solution( ivert, : ) = x( 1 : 3 ); end I tried to slice the variables according to MATLAB documentation posted here: parfor ivert = 1 : nVerts i = obj.neighIDs{ ivert }; b = obj.f( i ); A = obj.coeffMatrix{ ivert } x = A \ b; obj.solution( ivert, : ) = x( 1 : 3 ); end But MATLAB complains that: Valid

Convert For Loop to List Comprehension: Testing if elements in list 2 are partial match for elements in list 1

随声附和 提交于 2020-01-06 15:18:15
问题 somewhat of a python/programming newbie here, I have come up with a code of 2 nested for loops to test if the elements of the the 2nd loop are a partial (or complete) match of the elements in the first list, and if so those elements are removed from the 1st list. Here is the code: >>> lst = ["my name is Bob Jenkins", "Tommy Cooper I am", "I be Bazil", "I Tarzan"] >>> names = ["Bob", "Tarzan"] >>> for l in lst: for n in names: if n in l: lst.remove(l) >>> print lst ['Tommy Cooper I am', 'I be