prakash Posted October 16, 2007 Share Posted October 16, 2007 I need and idea regarding how to merge two seperate data from the form. The first field form type is "textbox" and second is "file". and the two fields were in an array form like. <form name="myform" method="post" action="action.php"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"> </form> so how can I merge and loop two data through php. I need to use the array value on my script if...else condition. loop { if (....both array is not blank) { //do something print first array; print second array } } Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/ Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 Try this <?php foreach($_POST['myfield'] as $K => $V) { echo "textBox=$V<br>"; echo "File=".$_FILES[$K]['name']."<br>"; //Add If here if( !empty($_FILES[$K]['name']) && !empty($V) ) { //Both NOT empty //NOTE: i used $V for the text, i could also do $_POST[$K]['myfield'] } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370586 Share on other sites More sharing options...
prakash Posted October 16, 2007 Author Share Posted October 16, 2007 ok I got the idea, in the above example $K is used as indexes and $v for the value. and the value of FILE is retrieved on each loop via a array indexes i.e. $k am I right? Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370600 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 Why don't you change your form to something like: <form name="myform" method="post" action="action.php"> <input type="text" name="myinput[1][myfield]"><input type="file" name="myinput[1][myimage]"> <input type="text" name="myinput[2][myfield]"><input type="file" name="myinput[2][myimage]"> <input type="text" name="myinput[3][myfield]"><input type="file" name="myinput[3][myimage]"> <input type="text" name="myinput[4][myfield]"><input type="file" name="myinput[4][myimage]"> <input type="text" name="myinput[5][myfield]"><input type="file" name="myinput[5][myimage]"> </form> Then you can process it like: <?php for ($i=1;$i<6;$i++) { if ($_POST[$i]['myfield'] != '' && $_POST[$i]['myimage'] != '') { // // do something // } ?> With your current form definition, there is not a one-to-one correspondence between the array fields. If someone entered some text in the first "myfield" and nothing in the first "myimage" and then put nothing in the second "myfield" and put something in the second "myimage", both would show up as the first entry in the array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370603 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 $K = Key (AKA index number) So as you loop through the post (via the key) you also check the FILES with the same key or use kenrbnsn idea Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370604 Share on other sites More sharing options...
prakash Posted October 16, 2007 Author Share Posted October 16, 2007 Try this <?php foreach($_POST['myfield'] as $K => $V) { echo "textBox=$V<br>"; echo "File=".$_FILES[$K]['name']."<br>"; //Add If here if( !empty($_FILES[$K]['name']) && !empty($V) ) { //Both NOT empty //NOTE: i used $V for the text, i could also do $_POST[$K]['myfield'] } } ?> mmm how can $_FILES[$K]['name'] get details for myimage[]. I think it should be $_FILES[$K]['myimage']['name'] or something like that ???? Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370624 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 Ahhh, your right, kinda missed that (read my signature, i don't test this stuff LOL) $_FILES[$K]['myimage']['name'] looks about right remember the $K is just the index(key) mmm how can $_FILES[$K]['name'] get details for myimage[]. I think it should be $_FILES[$K]['myimage']['name'] or something like that ???? Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370627 Share on other sites More sharing options...
prakash Posted October 16, 2007 Author Share Posted October 16, 2007 I got problem. Both the script below is not working for $_FILES <form name="myform" method="post" action="test.php"> <input type="hidden" name="cmd" value="_runscript"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br><br> <input type="submit"> </form> <?php if ($_POST['cmd']=="_runscript") { foreach($_POST['myfield'] as $K => $V) { echo "textBox=$V<br>"; echo "File=".$_FILES[$K]['myimage']['name']."<br>"; //Add If here if( !empty($_FILES[$K]['name']) && !empty($V) ) { //Both NOT empty //NOTE: i used $V for the text, i could also do $_POST[$K]['myfield'] } } } ?> <form name="myform" method="post" action="test.php"> <input type="hidden" name="cmd" value="_runscript"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br><br> <input type="submit"> </form> <?php if ($_POST['cmd']=="_runscript") { foreach($_POST['myfield'] as $K => $V) { echo "textBox=$V<br>"; echo "File=".$_FILES[$K]['myimage']['name']."<br>"; //Add If here if( !empty($_FILES[$K]['myimage']['name']) && !empty($V) ) { //Both NOT empty //NOTE: i used $V for the text, i could also do $_POST[$K]['myfield'] } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370706 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 try $_FILES['myimage'][$K]['name'] instead.. Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370711 Share on other sites More sharing options...
prakash Posted October 16, 2007 Author Share Posted October 16, 2007 it's not working at all Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370723 Share on other sites More sharing options...
prakash Posted October 16, 2007 Author Share Posted October 16, 2007 the following code works $_FILES['myimage']['name'][$K] Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370730 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 tested also note the enctype="multipart/form-data" <form name="myform" method="post" action="test.php" enctype="multipart/form-data" > <input type="hidden" name="cmd" value="_runscript"> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br> <input type="text" name="myfield[]"><input type="file" name="myimage[]"><br><br> <input type="submit"> </form> <?php print_r($_FILES); if ($_POST['cmd']=="_runscript") { foreach($_POST['myfield'] as $K => $V) { echo "textBox=$V<br>"; echo "File=".$_FILES['myimage']['name'][$K]."<br>"; //Add If here if( !empty($_FILES['myimage']['name'][$K]) && !empty($V) ) { //Both NOT empty //NOTE: i used $V for the text, i could also do $_POST[$K]['myfield'] } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73465-merging-two-array/#findComment-370733 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.