Jump to content

php arrays


wrathican

Recommended Posts

hey guys

im trying to do a mass update in a db. the new values will be coming from a form.

 

ive thought about how to do this going backwards. so starting with the actual update.

for this i am going to use a foreach loop.

so what i need to do is pass an array full of values from my php fom to my processing script.

 

my form is displayed like so:

<?php
$query = "select * from table";
$result = mysql_query($query);
?>
<form>
<?php
while($row = mysql_fetch_array($result)){
$img = $row['table_image'];
$title = $row['table_title'];
$imgId = $row['table_id'];
?>
<img src="<?php echo $image; ?>" alt="<?php echo $title; ?>" /><br />
<input type="hidden" name="imageid" value="<?php echo $imgId; ?>" />
<input type="text" name="title" />
<?php
}
?>
<input type="submit" name="send" value="Send" />
</form>

 

the only thing is i dont know how to start an array and store the values i need to in it.

 

my array would(in a perfect world) look like this:

$images['3']['title'];

 

so '3' is the id of the image in the table, and 'title' is the title that the user would enter in the form input field.

im just confused as to how i would display that in my form

 

help?

 

thanks in advanced

Link to comment
Share on other sites

you could do

<?php
while($row = mysql_fetch_array($result)){
$img = $row['table_image'];
$title = $row['table_title'];
$imgId = $row['table_id'];
$images[] = $imgId;
?>

 

then somewhere along the line you could do:

 

$images[$_POST['imageid']]['title'] = $_POST['title'];

Link to comment
Share on other sites

well you can get the form array values (That you want to store in DB) through $_REQUEST.

make a new array

$MyArray = array();

$MyArray =$_REQUEST;

now $MyArray contains the submitted data, it is easy to store in to the DB.

 

Don't use $_REQUEST.

Link to comment
Share on other sites

while($a = mysql_fetch_object($result)) {
$images[] = array("img " => $a->table_image, "title" => $a->table_title, "imgId" => $a->table_id);

 

this puts the stuff from the db into an array?

 

if im right then its not what i need.

 

the while loop on my display page is fine, except for one thing.

 

the main problem is my processing script.

 

all my display script does is repeat some form fields.

 

so if i had 3 items in my db it would look like this:

<form>
<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid" value="2"/>
<input type="text" name="title" />

<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid" value="4"/>
<input type="text" name="title" />

<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid" value="7"/>
<input type="text" name="title" />

<input type="submit" />
</form>

 

so, i have multiple form fields called the same thing. i would have 3 fields called imageid, 3 called title.

 

so what i need on my processing script is a way of getting the values of 'title' and 'imageid' to correspond with eachother. so the first imageid matches the title for it.

 

i really hope im making sense.

Link to comment
Share on other sites

You'll need to submit the form fields as an array, otherwise only the last form field value will be received by your script.

<form>
<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid[]" value="2"/>
<input type="text" name="title[]" />

<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid[]" value="4"/>
<input type="text" name="title[]" />

<img src="someimage" alt="sometitle"/><br />
<input type="hidden" name="imageid[]" value="7"/>
<input type="text" name="title[]" />

<input type="submit" />
</form>

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.