Jump to content

merging two array


prakash

Recommended Posts

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

}

}

Link to comment
Share on other sites

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']
}
}
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 ????

Link to comment
Share on other sites

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 ????

Link to comment
Share on other sites

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']
}
}
}
?>

Link to comment
Share on other sites

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']
}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.