The Ultrashock Ultra Bundle
  • Home
  • Community
  • Forum
  • Development
  • Server Side
  • Thread
  •  
  • Previous topic
  • Next topic
Sign up to post

Development
 Server Side

  • Andy M Author 
    • 30797 
    • 0 
    • 6 
    POST an array?
    Nutrox

    Last reply Jul 09 2008, 11:29 PM

    by Nutrox

    Posted: Jul 08 2008, 11:28 AM

    by Andy M

     

Running into some trouble here with a php form and wondering if anyone has any ideas in terms of how to get around it.

I’ve got a list of input boxes that are declared like this:

<?php

    $_SESSION[
"quant1"] = $_POST["quant1"]; 
    echo 
"<input type=\"text\" name=\"quant1\" size=\"1\" maxlength=\"2\" value=\"" . $quant1 . "\">

?> 

Problem is that I have a quant0, quant1, quant2, quant3, etc. The number of items is the size of a csv file.

Now what I would like to do is have it as a for loop so that I can generate n number of the above and adjust n given the size of the csv (which is stored as an array).

I was trying to do the for loop for the input box as an array:

<?php

for ($i = 0; $i <= $sizeOfArray; $i++){
    $_SESSION[
"quant[$i]"] = $_POST["quant[$i]"];  // this won't work!
    
echo "<input type=\"text\" name=\"quant[$i]\" size=\"1\" maxlength=\"2\" value=\"" . $quant[$i] . "\">  // this won't work either!
}

?> 

Anyone have any ideas? Thanks!

  6 REPLIES
 
Nutrox
1  
Nutrox

You don’t need to define the array indexes in forms. If you give the array of form elements the same name, and use closed brackets after each name, they will automatically be converted to an array when they reach the server.

For example…

<form ... >
    <
input type="text" name="fruit[]" value="apple"/>
    <
input type="text" name="fruit[]" value="banana"/>
    <
input type="text" name="fruit[]" value="cherry"/>
</
form> 
<?php

echo( $_POST[ "fruit" ][ 0 ] ); // apple
echo( $_POST[ "fruit" ][ 1 ] ); // banana
echo( $_POST[ "fruit" ][ 2 ] ); // cherry

?> 

Is that what you are looking for?

smilie

  • 08 July 2008 12:27 PM
  •  
Andy M Author 
2  
Andy M

Hmm I suppose so. Except the value must be whatever is stored in $_POST[ “fruit” ][ i ]. So would this work?

<form ... >
    <
input type="text" name="fruit[]" value="fruit[]"/>
    <
input type="text" name="fruit[]" value="fruit[]"/>
    <
input type="text" name="fruit[]" value="fruit[]"/>
</
form>Code:
<?php

echo( $_POST[ "fruit" ][ 0 ] );
echo( 
$_POST[ "fruit" ][ 1 ] );
echo( 
$_POST[ "fruit" ][ 2 ] );
?> 


I suppose I can also declare the session as:

$_SESSION['fruit'] = $fruit; 

?

  • 08 July 2008 03:09 PM
  •  
Nutrox
3  
Nutrox

That wouldn’t work because all of your posted input values would be “fruit[]”. I took another look at your PHP code and spotted a few errors so I fixed them, see if this works for you now…

<?php

foreach( $quant as $value )
{
    $_SESSION[ $value ] 
= $_POST[ $value ];
    echo( 
"<input type=\"text\" name=\"{$value}\" size=\"1\" maxlength=\"2\" value=\"{$value}\">" );
}

?> 

I’m guessing that $quant is an array of values for the form inputs.

  • 08 July 2008 03:33 PM
  •  
Andy M Author 
4  
Andy M

Still not quite working. $quant is an array that holds the form inputs yes.

Well, starting back from the beginning, here’s sort of the big picture:

<?php 

$_SESSION[
"quant"] = $_POST["quant"];

// get data
$filestream = file_get_contents("$order_menu1");
$line_separated = explode("\n",$filestream);    // separate by line

for ($i = 0; $i < sizeof($line_separated); $i++){
    
    
// separate line by item
    
$comma_separated = explode(",",$line_separated[$i]);

    
$item[$i] = $comma_separated[0];
    
$price[$i] = $comma_separated[1];
    
$description[$i] = $comma_separated[2];
    
    
    if (
$item[$i] != null){
        
echo "<input type=\"text\" name=\"$quant[$i]\" size=\"1\" maxlength=\"2\" value=\"".$quant[$i]."\"> ";
        echo 
$item[$i]; echo "@";
        echo 
"$"; printf ("%01.2f", $price[$i]);
        echo 
"<br/>";

    
}
    
}


?> 

Now it works if this line:
echo “<input type=\“text\” name=\”$quant[$i]\” size=\“1\” maxlength=\“2\” value=\”“.$quant[$i].”\”> “;

is changed to this

echo “<input type=\“text\” name=\“quant\” size=\“1\” maxlength=\“2\” value=\”“.$quant.”\”> “;

(i.e. it’s “quant” as opposed to the array “quant”)


Thanks for your help so far Si!

  • 09 July 2008 03:59 AM
  •  
Andy M Author 
5  
Andy M

Got it to work using the first post from Si.

If anyone else is interested, here is what I did:

for ($i = 0; $i < sizeof($line_separated); $i++){
    
    
// separate line by item
    
$comma_separated = explode(",",$line_separated[$i]);

    
$item[$i] = $comma_separated[0];
    
$price[$i] = $comma_separated[1];
    
$description[$i] = $comma_separated[2];
    
    
    if (
$item[$i] != null){
        
echo "<input type=\"text\" name=\"quant[]\" size=\"1\" maxlength=\"2\" value=\"".$quant[$i]."\"> ";
        echo 
$item[$i]; echo "@";
        echo 
"$"; printf ("%01.2f", $price[$i]);
        echo 
"<br/>";

    
}
    
} 


The part that helped was assigning name=quant[]. Then it worked. smilie

Thanks Si for your help!

  • 09 July 2008 11:20 PM
  •  
Nutrox
6  
Nutrox

Hehe… I’m glad you got that working Andy, I have to admit it was confusing me a little. smilie

  • 09 July 2008 11:29 PM
  •  
  •   Log in or join for free to make a comment.
 
Topic actions
  •  Share on Facebook
  •  Share on Twitter
Topic Categories
  •  Show All Topics
  •  Development
    •  Server Side
    •  Client Side
  •  Creative Software
    •  Web
    •  Video
    •  3D
    •  Illustrator
    •  Photoshop Battles
    •  Photoshop
  •  Design
    •  Typography
    •  Resources & Insight
    •  Checkpoint
  •  Career
    •  Copyright Matters
    •  Advice & issues
    •  Job Seekers
    •  Job Offers
  •  Flash
    •  UltraMath
    •  OOP
    •  Third Party Tools
    •  Open Source alternatives
    •  Data Communication
    •  Components
    •  Flex
    •  AIR
    •  Flash Lite
    •  Flash Professional
    •  Flash Newbie
    •  ActionScript
    •  XML
  •  Lounge
    •  Polls
    •  Random Chat
    •  Showcase And Critique
    •  BombShock Award Nominations
  •  Community Essentials
    •  BombShock Award Winners
    •  Tutorials
    •  Interviews
    •  News
    •  Bitmap tutorials
Popular Topics
  • Sort by: 
  • Activity
  • Views
  • Comments
  • Likes
Advertise with us
  • Your advertisement here!
  • loading
Ultrashock
  • Creative Assets
  • Community
  • Blog
  1. Home
  2. Forum
+/-
Creative Assets
  • Categories
  • Contributors
  • How to buy
Make Money
  • Commission Rates
  • Referral Program
  • Contributor Program
Community
  • Activity Feed
  • Forum
  • Profiles
About
  • Quick Tour
  • Our History
  • Banners & Logos
Support
  • Contact Ultrashock
  • Advertise with us
  • Legal Information
  •  Keep up to date
  • Flash 775  Flash
  • Audio 6,481  Audio
  • Vector 2,130  Vectors
  • Image 12,338  Images
  • Creative Assets 21,724  Assets
  • Profiles 282,659  Members
  • Topics 93,762  Topics
  • Blog 4  Blog
  • Facebook 1,680  Facebook
  • Twitter 1,165  Twitter
  • Join our FREE monthly newsletter!
  • Archive
  • Invalid email address. Please try again.
Subscribe
  • ©2012 Ultrashock LLC - All rights reserved
  • Terms of Use
  • Privacy Policy
  • Switch to dark theme
  • RSS Feeds
  • Top

©2012 Ultrashock LLC - All rights reserved

Printed on Sat, February 04, 2012 - 18:44:04