0

I want to loop through the values of array which contain $i variable in it,

My code for array looks like this,

foreach($fields_names as $k => $v) {
      $handle_data[] ='$rowdata[$i][$fields_names[\''.$v.'\']]';
}

for($i=0;$i<$number_rows_excel;$i++){

$sql.= "('".implode("','", array_values($handle_data))."'),". "<br/>";

        }

When I am echoing $sql, I am getting like this ,

INSERT INTO eximport (`S.No`, `Patent#`, `Title`) VALUES ('$rowdata[$i][$fields_names['S.No']]','$rowdata[$i][$fields_names['Patent#']]','$rowdata[$i][$fields_names['Title']]')

It should have to display like this

INSERT INTO eximport (`S.No`, `Patent#`, `Title`) VALUES ('$rowdata[0][$fields_names['S.No']]','$rowdata[0][$fields_names['Patent#']]','$rowdata[0][$fields_names['Title']]')

How can I use the variable $i to loop through array values?

3 Answers 3

1

i have used this code which helps me to insert the row, appending ',' for each values for insert

 foreach($val_ary as $new_data_string){
    $newArray[] = implode("','",$new_data_string);
        }

//Inserting data into the table     
        for($tr=0;$tr< $data->sheets[0]['numRows']-2;$tr++)
        {
         $sql ="INSERT INTO eximport($fieldnames)values('$newArray[$tr]')";
         mysql_query($sql) or die("Error in Query: " . mysql_error());  
         //$p1 = $db->insert_id;
         }
Sign up to request clarification or add additional context in comments.

Comments

0

I assume you don't want to have the $rowdata[] parts written in the query, but the actual value of that array-entry?

So, you should simple nest your loops, like so. Remember, you are looping every Field, which means, you dont need to do any implode on the data. I assume that your data Sets are inside the Array $data and $field_names is just a List of fieldnames.:

$data = array(
   array("name" => "DataSet1", "value" => 56),
   array("name" => "DataSet2", "value" => 5)
);

$field_names = array("name","value");

$sql = "INSERT INTO table (";
$sql .= implode($fields_names,",");
$sql .= ")VALUES";

for($i=0; $i<count($data); $i++){
   $sql .= "(";
   foreach($fields_names as $k => $v) {
      $sql.= "'".$data[$i][$v]."',";

   }
   //remove trailing ","
   $sql = substr($sql,0,-1);
   $sql .= "),";
}
//remove trailing ","
$sql = substr($sql,0,-1);

This will generate:

INSERT INTO table (name,value)VALUES('DataSet1','56'),('DataSet2','5')

3 Comments

ia m getting field names from this code foreach($fields_names as $k => $v) { $handle_data[] ='$rowdata['.$i.'][$fields_names[\''.$v.'\']]'; } and data from this for($i=0;$i<$number_of_fields;$i++) { $fields_names[] =mysql_field_name($res, $i); } actually $rowdata['this_is_row_number']['this_will_get_name_of_field'] , how can i combine the field name with its associative data
I don't get why you are adding '$rowdata['.$i.'][$fields_names[\''.$v.'\']] to the handle array. This is a string and will not be resolved into anything of value. Please provide a example dataset of your arrays, containing example values. Then we can help you.
i have used this code which helps me to insert the row, ppeding ',' for each values for insert foreach($val_ary as $new_data_string){ $newArray[] = implode("','",$new_data_string); } //Inserting data into the table for($tr=0;$tr< $data->sheets[0]['numRows']-2;$tr++) { $sql ="INSERT INTO eximport($fieldnames)values('$newArray[$tr]')"; mysql_query($sql) or die("Error in Query: " . mysql_error()); //$p1 = $db->insert_id; }
0

Try this

$i=0;
foreach($fields_names as $k => $v) {
    $handle_data[] ='$rowdata['.$i.'][$fields_names[\''.$v.'\']]'; $i++;
}

3 Comments

first thanks for the reply , it is diaplaying like this ('$rowdata[0][$fields_names['S.No']]','$rowdata[1][$fields_names['Patent#']]','$rowdata[2][$fields_names['Title']]'), ('$rowdata[0][$fields_names['S.No']]','$rowdata[1][$fields_names['Patent#']]','$rowdata[2][$fields_names['Title']]'), instead i want like this ('$rowdata[0][$fields_names['S.No']]','$rowdata[0][$fields_names['Patent#']]','$rowdata[0][$fields_names['Title']]'), ('$rowdata[1][$fields_names['S.No']]','$rowdata[1][$fields_names['Patent#']]','$rowdata[1][$fields_names['Title']]'),
Can you please explain what is your expected result.Because the problem is in the loop of $fields_names only
your code is running like this'$rowdata[0][$fields_names['S.No']]','$rowdata[1][$fields_names['Patent#']]','$‌​rowdata[2][$fields_names['Title']]'), all i want is like '$rowdata[0][$fields_names['S.No']]','$rowdata[0][$fields_names['Patent#']]','$‌​rowdata[0][$fields_names['Title']]'), and after that increment the array values to '$rowdata[1][$fields_names['S.No']]','$rowdata[1][$fields_names['Patent#']]','$‌​rowdata[1][$fields_names['Title']]')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.