Jump to content

Recommended Posts

Hello, I have read several examples about posting arrays but still have few questions that I will post here consecutively (thanks in advance)

 

I have hundreds of variables that I need to use after posting, I am talking about a books price list, e.g:

Accounting_Code    ISBN    Title      Size    Price

============    ====  ====    ===    ====

 

 

Q1) When I query the books table to view them in a form for modification, what is the correct syntax?

I mean how do i set the array for each <input> so i can link (price/size/Title/ISBN) to its relative Accounring_Code?

i saw smthg like value=[][]

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/158422-posting-arrays/
Share on other sites

Hi,

 

i never used a multidimensional array

 

1) How do I loop over the following posted form?

2) is this array correct (logic and syntax)  if I want to get the value of (title/price/language/pages) for a specific isbn?

 

THANK YOU

 

<form method="POST" action="extract.php">
<p dir="ltr">row 1: 
<input type="text" name=isbn[123456789][] size="20" value="123456789">
<input type="text" name=title[123456789][] size="20" value="Book_1">
<input type="text" name=price[123456789][] size="20" value="50">
<input type="text" name=language[123456789][] size="20" value="En">
<input type="text" name=pages[123456789][] size="20" value="16"></p>
<p dir="ltr">row 2: 
<input type="text" name=isbn[123456790][] size="20" value="123456790">
<input type="text" name=title[123456790][] size="20" value="Book_2">
<input type="text" name=price[123456790][] size="20" value="60">
<input type="text" name=language[123456790][] size="20" value="Fr">
<input type="text" name=pages[123456790][] size="20" value="17"></p>
<p dir="ltr"> </p>
<p dir="ltr"><input type="submit" value="Submit" name="B1"></p>
</form>

Link to comment
https://forums.phpfreaks.com/topic/158422-posting-arrays/#findComment-835873
Share on other sites

No, I'd setup the form as:

<form method="POST" action="test.php">
   <p dir="ltr">row 1:<br />
   <input type="text" name=books[0][isbn] size="20" value="123456789"><br />
   <input type="text" name=books[0][title] size="20" value="Book_1"><br />
   <input type="text" name=books[0][price] size="20" value="50"><br />
   <input type="text" name=books[0][lang] size="20" value="En"><br />
   <input type="text" name=books[0][pages] size="20" value="16"></p>
   <p dir="ltr">row 2:<br />
   <input type="text" name=books[1][isbn] size="20" value="123456790"><br />
   <input type="text" name=books[1][title] size="20" value="Book_2"><br />
   <input type="text" name=books[1][price] size="20" value="60"><br />
   <input type="text" name=books[1][lang] size="20" value="Fr"><br />
   <input type="text" name=books[1][pages] size="20" value="17"></p>
   <p dir="ltr"> </p>
   <p d

That way when you submit the form, all the info for the books will be within the $_POST['books'] array which will like:

rray
(
    [0] => Array
        (
            [isbn] => 123456789
            [title] => Book_1
            [price] => 50
            [lang] => En
            [pages] => 16
        )

    [1] => Array
        (
            [isbn] => 123456790
            [title] => Book_2
            [price] => 60
            [lang] => Fr
            [pages] => 17
        )

)

Which will now allow you to easily process the data. Example:

if(isset($_POST['books']))
{
    // loop through the books
    foreach($_POST['books'] as $book)
    {
        echo '<h1>'.$book['title'].'</h1>';
        echo 'ISBN: ' .$book['isbn'].'<br />';
        echo 'Price: ' .$book['price'].'<br />';
        echo 'Langauge: ' .$book['lang'].'<br />';
        echo 'Pages: ' .$book['pages'].'<hr />';
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/158422-posting-arrays/#findComment-835887
Share on other sites

Many thanks wildteen88, i really appreciate your help,

the code works perfect.

 

 

I have one last question, lets say that this form is an edit form, all values are read from my DB.

When posting this form, how I would know that the (title/price/pages/language) are related to this ISBN so the query update can work correctly.

Link to comment
https://forums.phpfreaks.com/topic/158422-posting-arrays/#findComment-835894
Share on other sites

Replace the 0's and 1's in your form with the value of the Accounting_Code field in your database.

 

Now when you process the form use the following

if(isset($_POST['books']))
{
    // loop through the books
    foreach($_POST['books'] as $account_code => $book)
    {
        echo '<h1>'.$book['title'].'</h1>';

        echo 'Account Code: ' .$account_code.'<br />';

        echo 'ISBN: ' .$book['isbn'].'<br />';
        echo 'Price: ' .$book['price'].'<br />';
        echo 'Langauge: ' .$book['lang'].'<br />';
        echo 'Pages: ' .$book['pages'].'<hr />';
    }
}

Link to comment
https://forums.phpfreaks.com/topic/158422-posting-arrays/#findComment-835900
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.