Jump to content


Photo

Splitting array results


  • Please log in to reply
3 replies to this topic

#1 sabadell

sabadell
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 June 2006 - 01:55 PM

Hi I am working on a PHP script to upload files to the server and write the names in a mysql dbase. The upload script always works and when using a single file upload I get it to write the file name into the dbase. With multiple uploads I have been reading the manual and can't work out how to split the array values as  want to then allocated them to variable names to then insert into individual columns in the dbase.

If I try :

<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}

?>

It prints out the whole list of file names stored in the array and if I try the following:

<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}
    echo $file_array['name'][2]."<br>\n";

?>

It will print out the list of file names held in the $file_array['name'] array and then the corresponding letter in the last file name uploaded which is held in the array. Iin this case the third one.

As such want I need is to split the arrays and then give then variable names such as the following:

$var1 = the first uploaded file name from the
$var2 = the second uploaded file name etc etc.

Any help would be appreciated. Full script below:

regards



<html>
<?php require_once('../../Connections/Punks_conn.php'); ?>

<head>
<title>form variable insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 
$name = $_POST[Firstname];
$surname =$_POST[Surname];
$group = $_POST[Group];

//folder creation using chmod change in permissions to 777
$file_dir = "../test7/uploads/";
if( !file_exists( $file_dir ) )
{
  mkdir( $file_dir );
  chmod( $file_dir, 0777 ); 
                          
}
?>



<?php 
foreach($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";

    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die ("Couldn't copy");
        echo "file was uploaded!<br><br>";
    }
}


?>

<?php
/* This prints out a list of the uploaded file names but I need to separate the names and give them variable names*/
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}
    echo $file_array['name'][2]."<br>\n";

?>



<?php // upload the form details

mysql_select_db($database_Punks_conn, $Punks_conn); 
$sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1, Photo2,Photo3,Photo4 ) VALUES ('$name','$surname','$group', '$photo1', '$photo2','$photo3','$photo4')";
//execute the SQL Statement
if (mysql_query($sql, $Punks_conn)) {
	echo "new listing created";
	}
	else {
	echo "This one never made it";
	}
//error identifier
?>

</body>
</html>



#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 30 June 2006 - 02:35 PM

First, I would change your database structure so there aren't multiple images in a row.  I think that it would be better form to just enter each photo in as its own row and have a relational column to connect them.

If you do what I just suggested then you can use a foreach loop to insert the photos on at a time...
$query = ''; // create the string
foreach ($photos as $photo)
{
$query .= "INSERT INTO $table ... ;\n"; // add a new query to the string, remember the ";" 
}
$result = @mysql_query... ; // ok so you just entered them all in now check for errors

Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 sabadell

sabadell
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 July 2006 - 10:41 AM

HI thanks for your help but I don't think I can use it I need to keep the database structure as it is and so it doesn't really solve the problem. Once again it is merely a question of taking out the values as are displayed with this code.......


<?php
foreach($_FILES as $file_name => $file_array) {
    echo $file_array['name']."<br>\n";
}

?>

When I have tried to print out separate parts of the array such as the second (out of a possible four) it will just print out a letter of the name of the last file uploaded. (This is explained in the earlier posted details.) 

So what i would like to do is split the array and allocate them to variables or any suggestion that allows me to input each file name which has been uploaded in a column in the mysql database. eg. $Var1 into photo1 , $var2 into photo2 etc. 

Hope someone can help!  




#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 01 July 2006 - 02:21 PM

See if this helps:

<?php require_once('../../Connections/Punks_conn.php'); ?>
<html>
<head>
<title>form variable insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 
$name = $_POST['Firstname'];
$surname = $_POST['Surname'];
$group = $_POST['Group'];

//folder creation using chmod change in permissions to 777
$file_dir = "../test7/uploads/";
if (!file_exists($file_dir)) {
	mkdir( $file_dir );
	chmod( $file_dir, 0777 ); 
}

$i = 0;
foreach ($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";

    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or echo ("Couldn't copy!!<br><br>");
        echo "file was uploaded!<br><br>";
    }
	$photo{$i} = $file_array['name'];
	
}

mysql_select_db($database_Punks_conn, $Punks_conn); 
$sql = "INSERT INTO punks (FirstName, Surname, `Group`, Photo1, Photo2,Photo3,Photo4 ) VALUES ('$name','$surname','$group', '$photo1', '$photo2','$photo3','$photo4')";
//execute the SQL Statement
if (mysql_query($sql, $Punks_conn)) {
	echo "new listing created";
} else {
	echo "This one never made it";
}
?>
</body>
</html>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users