kosaks17 Posted January 9, 2012 Share Posted January 9, 2012 Hello to all I have problem a with my foreach loop.. First : Is it possible to rename each element that's been outputted everytime the loop execute? <?php $arr = array(mikel, 17, cambodia, 50kgs, 5 feet, anna, 21, peru, 45kgs, 6 feet); foreach ($arr as &$value) { $value = $value; } ?> What i would like on the result of the foreach loop is name : mikel Age : 17 address : cambodia weight : 50kgs height : 5 feet is this possible? Second : i want to echo the result of the for each loop in every 5 turns. result 1 = mikel, 17, cambodia, 50kgs, 5 feet result 2 = anna, 21, peru, 45kgs, 6 feet Can anyone help me on this? thanks Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/ Share on other sites More sharing options...
blacknight Posted January 9, 2012 Share Posted January 9, 2012 for what you want .. i think you need a multi dementional array... try $arr = array(array('mikel', '17', 'cambodia', '50kgs', '5 feet'), array('anna', '21', 'peru', '45kgs', '6 feet')); foreach ($arr as $value => $e) { foreach ($e as $b => $a) { echo $a.'<br>'; } } to a further extent $names = array('name','Age','address','weight','height'); $arr = array(array('mikel', '17', 'cambodia', '50kgs', '5 feet'), array('anna', '21', 'peru', '45kgs', '6 feet')); foreach ($arr as $value => $e) { foreach ($e as $b => $a) { echo $names[$b] .' - '.$a.'<br>'; } echo '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305853 Share on other sites More sharing options...
kosaks17 Posted January 9, 2012 Author Share Posted January 9, 2012 Hello This is my array.. Array ( [0] => Array ( [1] => Mike [2] => 17 [3] => camboia [4] => 50kgs [5] => 5 feet ) [1] => Array ( [1] => anna [2] => 21 [3] => peru [4] => 45kgs [5] => 6 feet ) ) Im using foreach loop. This is the code foreach($array as $h => $value) { foreach($value as $elements) { echo $elements; // i want to divide the data into 5 elements then save it to database } echo '<br/><b> --> Break <-- </b>'; } What i want is that to break the data everytime the second loop reach 5 turns then save it to database.. I know how to save it into the database but i dont know how to divide the data on the foreach loop.. Could you help me? Im stuck on this problem.. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305862 Share on other sites More sharing options...
Psycho Posted January 9, 2012 Share Posted January 9, 2012 You should "save up" all the data and do one INSERT into the database. Where is the array of data coming from: a form POST, a file import or what? It appears that each 'record' in the array contains more data than you actually want to save to the database. Ideally, each element in the sub-array would have named keys (e.g. 'name', 'age, etc.) If not, you would need a process to translate what field is what Here is a general solution: $insertValues = array(); //Temp array to store values foreach($array as $id => $record) { $name = $record[0]; $age = $record[1]; $addr = $record[2]; $weight = $record[3]; $height = $record[4]; $insertValues[] = "('{$name}', '{$age}', '{$addr}', '{$weight}', '{$height}')"; } //Create/run insert quer $query = "INSERT INTO table_name (`name`, `age`, `address`, `weight`, `height`) VALUES " . implode(', ', $insertValues); $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305865 Share on other sites More sharing options...
blacknight Posted January 9, 2012 Share Posted January 9, 2012 same idea basicly $query = 'INSERT INTO `yourtable` VALUES '; foreach ($arr as $value => $e) { $query .= "("; foreach ($e as $b => $a) { $query .= "'".$a."',"; } $query = preg_replace('/,$/', '', $query); $query .= "), "; } $query = preg_replace('/, $/', ';', $query); echo $query.'<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305866 Share on other sites More sharing options...
kosaks17 Posted January 9, 2012 Author Share Posted January 9, 2012 Thank you so much for your help guys.. I really appreciate it.. Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305869 Share on other sites More sharing options...
Psycho Posted January 9, 2012 Share Posted January 9, 2012 same idea basicly $query = 'INSERT INTO `yourtable` VALUES '; foreach ($arr as $value => $e) { $query .= "("; foreach ($e as $b => $a) { $query .= "'".$a."',"; } $query = preg_replace('/,$/', '', $query); $query .= "), "; } $query = preg_replace('/, $/', ';', $query); echo $query.'<br>'; yeah, but his original post showed that there were more fields in the record than he was saving to the database. That code would try and add all the field to the values. Plus, adding a comma at the end of each value record and having to remove the last one is kinda hokey in my opinion. The code I provide could be much more compact without creating the temp variables. But, that can be helpful to keep the values strait when you review the code and if you need to do validations on them. Otherwise it could simply have been: $insertValues = array(); //Temp array to store values foreach($array as $id => $record) { $insertValues[] = "('{$record[0]}', '{$record[1]}', '{$record[2]}', '{$record[3]}', '{$record[4]}')"; } //Create/run insert query $query = "INSERT INTO table_name (`name`, `age`, `address`, `weight`, `height`) VALUES " . implode(', ', $insertValues); $result = mysql_query($query); Much simpler. Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1305871 Share on other sites More sharing options...
kosaks17 Posted January 27, 2012 Author Share Posted January 27, 2012 This is the code that i've been using.. Please see below You should "save up" all the data and do one INSERT into the database. Where is the array of data coming from: a form POST, a file import or what? It appears that each 'record' in the array contains more data than you actually want to save to the database. Ideally, each element in the sub-array would have named keys (e.g. 'name', 'age, etc.) If not, you would need a process to translate what field is what Here is a general solution: $insertValues = array(); //Temp array to store values foreach($array as $id => $record) { $name = $record[0]; $age = $record[1]; $addr = $record[2]; $weight = $record[3]; $height = $record[4]; $insertValues[] = "('{$name}', '{$age}', '{$addr}', '{$weight}', '{$height}')"; } //Create/run insert quer $query = "INSERT INTO table_name (`name`, `age`, `address`, `weight`, `height`) VALUES " . implode(', ', $insertValues); $result = mysql_query($query); The code works fine but have problems when a field contain quotes, double quotes and commas. Ex : when $addr = "address 1, sample's street." This would cause an error when i try to save it into the database. the query string would give me this. Please see below <?php /* for example the implode results would be ('thomas','17','5 feet','60 kilograms','address 1, sample's street.') */ $query = "INSERT INTO table_name (`name`, `age`, `height`, `weight`, `address`) VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.'); ?> as you can see the string on Values has an error since the bold information has a wrong format.. VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.'); can someone help me modify the code so that i could save my information whether or not it has a comma, quote and double quote? thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1311719 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2012 Share Posted January 27, 2012 mysql_real_escape_string is what you need to be using on the string type data you're using in a database query string. Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1311733 Share on other sites More sharing options...
Psycho Posted January 27, 2012 Share Posted January 27, 2012 mysql_real_escape_string is what you need to be using on the string type data you're using in a database query string. Just add it to where the values are defined $name = mysql_real_escape_string($record[0]); $age = mysql_real_escape_string($record[1]); $addr = mysql_real_escape_string($record[2]); $weight = mysql_real_escape_string($record[3]); $height = mysql_real_escape_string($record[4]); Quote Link to comment https://forums.phpfreaks.com/topic/254667-problem-with-foreach-loop/#findComment-1311779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.