Jump to content

Array Help


RCS

Recommended Posts

What he did for me works great to create the form but how would I add the conditions and submit the info to my database.

 

With one it's easy

 

<?PHP
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/tif")
|| ($_FILES["file"]["type"] == "image/psd")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pct")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
include("dbinfo.inc");
include("file_array.inc");
$con = mysql_connect($db_host, $db_user, $db_passwd) or die ("Could not connect to database.");
mysql_select_db($db_name) or die ("Unable to connect to select database");
$query = "INSERT INTO upload(name, TYPE, size, content, make, description, price)".
"VALUES ('$fileName', '$fileType', '$fileSize', '$content','$_POST[make]','$_POST[description]','$_POST[price]')";
$result=mysql_query($query) or die ("Could not complete query.");
?>

 

I don't understand how to INSERT INTO upload the array info from

 

<?php
$imagedir = "x";
if (isset($_POST['btnSub']))
{
    $desc = $_POST['desc'];
    $price = $_POST['price'];
    $make = $_POST['make'];
    
    foreach ($_FILES['picfile']['name'] as $k => $name)
    {
        
        $tmpname = $_FILES['picfile']['tmp_name'][$k];
        $type = $_FILES['picfile']['type'][$k];
        
        // process each pic here
    }
    
    // update db here
}


?>

<form method='post' enctype='multipart/form-data'>

<?php


    echo "Make <input type='text' name='make[]' /><br/>";
    echo "Description  <input type='text' name='desc[]' size='60' /><br/>";
    echo "Price  <input type='text' name='price[]' /><br/>";
    for ($i=1; $i<=10; $i++)
    {
        echo "<fieldset>\n";
        echo "<legend>Picture $i</legend>\n";
        echo "<input type='file' name='picfile[]' size='60' /><br/>";
        echo "</fieldset><br/>\n";
    }
?>
<input type='submit' name='btnSub' value='Submit'>
</form>

Link to comment
Share on other sites

ok now that's the question that I've been trying to pull out.

 

Barand's script creates an array of files and the associated info.  Then there's a foreach loop (www.php.net/foreach) that loops through each file one at a time.  Treat that ONE file just as you would treat any other single file.

 

I'll try and explain arrays a little bit and hopefully that'll help.  For me to try and tell you how to apply Barand's code to your code as written would be akin to rewriting your code for you and I don't feel like doing that.  Plus, if I can explain arrays enough that you're able to take it and apply it to your own code, you'll learn a lot more.

 

That being said let's start with a basic array.

 

1d array:

//if I don't give it a key name when I create the array element, PHP will assume that I want to use the next (numerically) available key.
$ar1[]="test1";
$ar1[]="test2";

 

having done that, $ar1[0] would be "test1".

 

//I can also specify what the keys would be 
$ar2[0]="test1";
$ar2[1]="test2";

 

having done that, $ar2[0] would still be "test1".

 

 

//I can also specify string based key names: 
$ar3['key1']="test1";
$ar3['key2']="test2";

 

having done that, $ar3['key1'] would be "test1".

 

Now you can take all of that and do the same with a 2 array.

 

$arr4[0][0]="Test-0-0";
$arr4[0][1]="Test-0-1";
$arr4[0][2]="Test-0-2";

$arr4[1][0]="Test-1-0";
$arr4[1][1]="Test-1-1";
$arr4[1][2]="Test-1-2";

$arr4[2][0]="Test-2-0";
$arr4[2][1]="Test-2-1";
$arr4[2][2]="Test-2-2";

 

So given all of that and looking at Barand's code if the filenames given were file1.jpg, file2.jpg, file3.jpg.......file10.jpg, then $_FILES['picfile']['name'] would be  an array (a third dimension) of filenames.

 

The foreach loop looks at each element of that array and on each pass creates a new variable named k.

 

Pass 1:  $k = file1.jpg

Pass 2:  $k = file2.jpg

Pass 3:  $k = file3.jpg

......

Pass 10:  $k = file10.jpg

 

Link to comment
Share on other sites

So I would INSERT data to database like this

 

$query = "INSERT INTO upload(id, name, TYPE, size, content, make, description, price,  name2, type2, size2, content2, name3, type3, size3, content3, name4, type4, size4, content4, name5, type5, size5, content5, name6, type6, size6, content6, name7, type7, size7, content7, name8, type8, size8, content8, name9, type9, size9, content9, name10, type10, size10, content10,)".
"VALUES ($_POST[id],'$fileName', '$fileType', '$fileSize', '$content','$_POST[make]','$_POST[description]','$_POST[price]','$fileName2', '$fileType2', '$fileSize2', '$content2', '$fileName3', '$fileType3', '$fileSize3', '$content3', '$fileName4', '$fileType4', '$fileSize4', '$content4', '$fileName5', '$fileType5', '$fileSize5', '$content5', '$fileName6', '$fileType6', '$fileSize6', '$content6', '$fileName7', '$fileType7', '$fileSize7', '$content7', '$fileName8', '$fileType8', '$fileSize8', '$content8', '$fileName9', '$fileType9', '$fileSize9', '$content9', '$fileName10', '$fileType10', '$fileSize10', '$content10',)";

 

Is this not to much code to process at one time to the database?? or is this even correct??

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.