Jump to content

Array help


plastik77

Recommended Posts

Hi - just posting this one again as i've made no progress and got no replies the last time - I've set up a form for uploading multiple images and I am writing the urls to a database, which is queried and outputs the images on a web page. This works fine, however, I also want to add alt/title text for each image upload but can't quite seem to get it working. I'm pretty new to PHP so my knowledge of how to use arrays is fairly limited. If anyone could offer any help that would be appreciated. Code is as follows.

 

//html form

 

<form method="post" action="addimgck.php" id="newsForm" enctype="multipart/form-data">

<label for="user">Title</label>

<input type="text" name="title" value="" />

 

<label for="comments">Content:</label>

<textarea name="story"></textarea>

 

<p align="justify">

Upload images: <span id="alttext">Image Description</span>

 

 

<input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" />

 

<input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" />

 

<input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" />

 

<input type="file" name="images[]" size="40"><input type="text" name="alt[]" value="" />

 

</p>

<input type="submit" name="Submit" id="submitbutton" value="Submit" />

 

//addimgck.php script

 

<?php

include("functions.php");

dbconnect();

//get inputs

$title = $_POST['title'];

$story = $_POST['story'];

 

//insert story into dbs

$insert = mysql_query("insert into news (title,story) values ('$title','$story')");

if(!$insert) echo "insert problem";

$query = mysql_query("select id from news where title = '$title' and story = '$story'");

if(!query) echo "no data found";

else

  {

      $row = mysql_fetch_array($query);

      $id = $row['id'];

  }

//insert corresponding images into dbs 

while(list($key,$value) = each($_FILES[images][name]))

      {

        if(!empty($value))

        {

            $filename = $value;

            //$alt = $_POST[alt][name];

              $add = "news/$filename";

                      //echo $_FILES[images][type][$key];

              // echo "

";

              copy($_FILES[images][tmp_name][$key], $add);

              chmod("$add",0777);

            $insert_img = mysql_query("insert into image (url,alt,news_id) values ('$filename','$alt',$id) ");

            if(!insert_img) echo "didn't insert images";

        }

      }

header("location:newsbackup2.php"); // direct to news page

?>

Link to comment
https://forums.phpfreaks.com/topic/61736-array-help/
Share on other sites

specify a key for all of the corresponding inputs, such that no ambiguity occurs:

 

<input type="file" name="images[1]" size="40"><input type="text" name="alt[1]" value="" />

 

when processing each image, use the key from its input name to obtain the corresponding alt text.

Link to comment
https://forums.phpfreaks.com/topic/61736-array-help/#findComment-307337
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.