Jump to content

Using 2 arrays at the same time in a foreach


drisate

Recommended Posts

Hey guys i am trying to get 2 form arrays tot to work at the same time in a foreach. I tryed a few things but nothing is working.

 

foreach ($_POST["mp3"] as $mp3 and $_POST["nom"] as $nom) {

foreach ($_POST["mp3"] as $mp3, $_POST["nom"] as $nom) {

Link to comment
Share on other sites

 

I don't think it can be done this way. However, if both $_POST arrays have same number of elements, you can use something like this:

<?php
foreach ($_POST["mp3"] as $i=>$mp3)
{
  $nom = $_POST["nom"][$i];
  // do stuff with $mp3 and $nom
}
?>

Link to comment
Share on other sites

Yeah let me show you the complete script

 

First i have the HTML forme. It's a Javascript that generates the feilds when pressing the add button

function add_mp3_form($oups, $id){include("config.php");

print ("<tr>
            <td width='33%'><font size='1' face='Verdana' color='#F9B324'>Ajouter 
		des MP3</font></td>
            <td width='33%' align='center'><b><font face='Verdana' size='1' color='#F9B324'></font></b></td>
            <td width='33%' align='right'><font face='Verdana' size='1' color='#F9B324'>Profile d'album</font></td>
          </tr>
        </table>
        </td>
      </tr>
      <tr>
        <td width='100%' bgcolor='#55BCEA' height='7'></td>
      </tr>
      <tr>
        <td width='100%' bgcolor='#C1CDDA'>
        <table border='0' cellpadding='3' cellspacing='3' style='border-collapse: collapse' bordercolor='#111111' width='100%' id='AutoNumber4'>
          <tr>
            <td width='100%'>
		            <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='602' id='AutoNumber6' height='22'><form name='etape3' method='POST' action='catalogue.php?mod=13&id=$id&send=1' enctype='multipart/form-data'>
              <tr>
                <td width='602' height='22' colspan='2'>
			<font face='Verdana' size='1' color='#FF0000'>$oups</font></td>
              </tr><tr>
                <td width='20%' colspan='2' height='22'>");  
?>
<script type="text/javascript">
var fields;

function HTMLBlock(el) {
var instance = 1, elementNames = [], i, item, container;

/* remove source element */
container = el.parentNode;
container.removeChild(el);

/* cache input elements' name attributes */
for (i = 0; item =
el.getElementsByTagName("input").item(i); i++) {
elementNames[i] = item.getAttributeNode("name");
}

/* method to modify and insert a clone of the original element */
this.insert = function () {
for (var i = elementNames.length; i--; ) {
/* replace digits with instance # */
elementNames[i].nodeValue =
elementNames[i].nodeValue.replace(/\d+/, instance);
}
instance++;
container.appendChild(el.cloneNode(true));
};
}

function createHTMLBlock(id) {
var d = document, el;

/* test browser supports the methods we'll be using */
if (d.getElementById && (el = d.getElementById(id)) &&
el.getElementsByTagName && el.cloneNode &&
el.getAttributeNode) {
/* ... etc. */
return new HTMLBlock(el); // yes
}
return null; // no
}

function addHTML() {
if (fields) {
fields.insert();
}
}
</script>

<center><input type="button" onclick="addHTML()" value="Add Fields"></center><br>

<div id="new_link_fields"><div id="form_source"><span class="r">Nom: <input type='text' name='nom[]' value='' size='20'><input type='hidden' name='i[]' value='1'> <input type='file' name='mp3[]' size='30'><br></span></div></div>

<script type="text/javascript">
/* remove the HTML and set up the object down here, before
anything's displayed */
fields = createHTMLBlock("form_source");
</script>

<?php				            
		  
            print ("</td>
              </tr></table>
            </td>
          </tr>
        </table>
        </td>
      </tr>
      <tr>
        <td width='100%' bgcolor='#C1CDDA'>
        <table border='0' cellpadding='3' cellspacing='3' style='border-collapse: collapse' bordercolor='#111111' width='100%' id='AutoNumber5'>
          <tr>
            <td width='100%'>
            <input type='submit' value='Suivant' name='btn_name3' style='float: right'></td></form>
          </tr>
        </table>
        </td>
      </tr>");		
}

 

And this is the PHP code that takes the result in charge

foreach ($_POST["mp3"] as $i=>$mp3)
{
  $nom = $_POST["nom"][$i];

// Debug line
//echo "We are taking care of $nb loops";

$file_name = $mp3['name'];
$file_tmp = $mp3['tmp_name'];

echo "<br><br><b>This is loop $i</b><br>"; 
echo "<b>The name of the file is $file_name</b><br>";	
echo "<b>The var is mp3_$i</b><br><br>";
  
//Ici je regarde si l'extantion du code est autoriser
$ext = strrchr($mp3['name'],'.');
if (($extlimit == "OUI") && (!in_array($ext,$limitedext))) {
echo "Le MP3 num $i n'a pas un extension autoriser. (mp3 seulement)<br>";
}else{

// Ici je regarde si le fichier existe déjà.
if(file_exists($upload_dir.$mp3['name'])){
$random_digit=rand(0000,9999)."_";
}else{unset($random_digit);}

if (move_uploaded_file($file_tmp,$_SERVER['DOCUMENT_ROOT']."/admin/site/".$upload_dir.$random_digit.$mp3['name'])){

chmod($_SERVER['DOCUMENT_ROOT']."/admin/site/".$upload_dir.$random_digit.$mp3['name'], 0755);   
$mp3 = new mp3($upload_dir.$random_digit.$mp3['name']);
$mp3_1 = $mp3->extract(0,30);
$newname=rand("100000", "999999")."_";
$mp3_1->save($upload_demo_dir.$newname.$random_digit.$mp3['name']);

$insert=mysql_query("INSERT INTO `distadp_adpsite`.`artiste_mp3` (id, nom_mp3, nom_demo, num_albume, nom)VALUES (NULL , '".$random_digit.$mp3['name']."', '".$newname.$random_digit.$mp3['name']."', '$_GET[id]', '$nom')");

echo "<br>MP3 numéro $i uploadé! (<a href='http://distributionadp.info/admin/site/$upload_dir$random_digit".$mp3['name']."'>".$mp3['name']."</a>)<br>";
echo "Démo de 30 sec créé avec succès! (<a href='http://distributionadp.info/admin/site/$upload_demo_dir$newname$random_digit".$mp3['name']."'>$newname".$mp3['name']."</a>)<br><br>";

}else{
echo "Il ya eu une éreure inconnu dans le transfère du fichier du mp3 num $i (".$mp3['name'].")<br>";
}
}
}

print ('<embed hidden=true loop=false src="http://distributionadp.info/s.mp3"></embed>

<meta http-equiv="refresh" content="10;url=http://distributionadp.info/admin/site/catalogue.php?mod=12&id='.$_GET[id].'">


          </td>
          </tr>
        </table>
        </td>
      </tr>');

}else{
add_mp3_form($err, $_GET[id]);
}
}

 

The current foeach sugested returns and error

Warning: Invalid argument supplied for foreach() in catalogue.php on line 1067

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.