php-general Digest 19 Feb 2013 20:44:43 -0000 Issue 8129

Topics (messages 320241 through 320245):

Re: parsing select multiple="multiple"
        320241 by: tamouse mailing lists
        320242 by: John Taylor-Johnston
        320243 by: David Robley
        320244 by: tamouse mailing lists
        320245 by: John Taylor-Johnston

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Mon, Feb 18, 2013 at 6:54 PM, John Taylor-Johnston
<john.taylor-johns...@cegepsherbrooke.qc.ca> wrote:
> I am capable with <select name="DPRpriority">. (I suppose I did it
> correctly? :p )
> But I haven't the first clue how to parse a <select multiple> and multiply
> select name="DPRtype".
> Would anyone give me a couple of clues please? :)
> Thanks,
> John
>
>            Priority:
>            <select name="DPRpriority" form="DPRform">
>              <option value="1" <?php if ($_POST["DPRpriority"] == "1") {echo
> "selected";} ?>>1</option>
>              <option value="2" <?php if ($_POST["DPRpriority"] == "2") {echo
> "selected";} ?>>2</option>
>              <option value="3" <?php if ($_POST["DPRpriority"] == "3") {echo
> "selected";} ?>>3</option>
>              <option value="4" <?php
>              if (empty($_POST["DPRpriority"])) {echo "selected";}
>              if ($_POST["DPRpriority"] == "4") {echo "selected";}
> ?>>4</option>
>            </select>
>
>
>            <select multiple="multiple" name="DPRtype" form="DPRform">
>              <option value="1. Crimes Against Persons">1. Crimes Against
> Persons</option>
>              <option value="2. Disturbances">2. Disturbances</option>
>              <option value="3. Assistance / Medical">3. Assistance /
> Medical</option>
>              <option value="4. Crimes Against Property">4. Crimes Against
> Property</option>
>              <option value="5. Accidents / Traffic Problems">5. Accidents /
> Traffic Problems</option>
>              <option value="6. Suspicious Circumstances">6. Suspicious
> Circumstances</option>
>              <option value="7. Morality / Drugs">7. Morality /
> Drugs</option>
>              <option value="8. Miscellaneous Service">8. Miscellaneous
> Service</option>
>              <option value="9. Alarms">9. Alarms</option>
>            </select>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Do test this, but I think all that's required is you make the name an array:

<select name="DPRpriority[]" form="DPRform">

--- End Message ---
--- Begin Message ---

         <select multiple="multiple" name="DPRtype" form="DPRform">
             <option value="1. Crimes Against Persons">1. Crimes Against
Persons</option>
             <option value="2. Disturbances">2. Disturbances</option>
             <option value="3. Assistance / Medical">3. Assistance /
Medical</option>
             <option value="4. Crimes Against Property">4. Crimes Against
Property</option>
             <option value="5. Accidents / Traffic Problems">5. Accidents /
Traffic Problems</option>
             <option value="6. Suspicious Circumstances">6. Suspicious
Circumstances</option>
             <option value="7. Morality / Drugs">7. Morality /
Drugs</option>
             <option value="8. Miscellaneous Service">8. Miscellaneous
Service</option>
             <option value="9. Alarms">9. Alarms</option>
           </select>



Do test this, but I think all that's required is you make the name an array:

<select name="DPRpriority[]" form="DPRform">
Something like this?

if $DPRpriority[0] == "7. Morality / Drugs" { echo "selected"; }

I hate arrays. :D

http://php.net/manual/en/language.types.array.php

<?php
$DPRpriority[]  = array(
   1    => "a",
   "1"  => "b",
   1.5  => "c",
   true => "d",
);
var_dump($array);
?>


--- End Message ---
--- Begin Message ---
tamouse mailing lists wrote:

> On Mon, Feb 18, 2013 at 6:54 PM, John Taylor-Johnston
> <john.taylor-johns...@cegepsherbrooke.qc.ca> wrote:
>> I am capable with <select name="DPRpriority">. (I suppose I did it
>> correctly? :p )
>> But I haven't the first clue how to parse a <select multiple> and
>> multiply select name="DPRtype".
>> Would anyone give me a couple of clues please? :)
>> Thanks,
>> John
>>
>>            Priority:
>>            <select name="DPRpriority" form="DPRform">
>>              <option value="1" <?php if ($_POST["DPRpriority"] == "1")
>>              {echo
>> "selected";} ?>>1</option>
>>              <option value="2" <?php if ($_POST["DPRpriority"] == "2")
>>              {echo
>> "selected";} ?>>2</option>
>>              <option value="3" <?php if ($_POST["DPRpriority"] == "3")
>>              {echo
>> "selected";} ?>>3</option>
>>              <option value="4" <?php
>>              if (empty($_POST["DPRpriority"])) {echo "selected";}
>>              if ($_POST["DPRpriority"] == "4") {echo "selected";}
>> ?>>4</option>
>>            </select>
>>
>>
>>            <select multiple="multiple" name="DPRtype" form="DPRform">
>>              <option value="1. Crimes Against Persons">1. Crimes Against
>> Persons</option>
>>              <option value="2. Disturbances">2. Disturbances</option>
>>              <option value="3. Assistance / Medical">3. Assistance /
>> Medical</option>
>>              <option value="4. Crimes Against Property">4. Crimes Against
>> Property</option>
>>              <option value="5. Accidents / Traffic Problems">5. Accidents
>>              /
>> Traffic Problems</option>
>>              <option value="6. Suspicious Circumstances">6. Suspicious
>> Circumstances</option>
>>              <option value="7. Morality / Drugs">7. Morality /
>> Drugs</option>
>>              <option value="8. Miscellaneous Service">8. Miscellaneous
>> Service</option>
>>              <option value="9. Alarms">9. Alarms</option>
>>            </select>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
> 
> Do test this, but I think all that's required is you make the name an
> array:
> 
> <select name="DPRpriority[]" form="DPRform">

More info at http://www.php.net/manual/en/language.variables.external.php 
(search for multiple) and 
http://www.php.net/manual/en/faq.html.php#faq.html.select-multiple

-- 
Cheers
David Robley

Know what I hate? I hate rhetorical questions!


--- End Message ---
--- Begin Message ---
On Mon, Feb 18, 2013 at 7:28 PM, John Taylor-Johnston
<john.taylor-johns...@cegepsherbrooke.qc.ca> wrote:
>
>>>          <select multiple="multiple" name="DPRtype" form="DPRform">
>>>              <option value="1. Crimes Against Persons">1. Crimes Against
>>> Persons</option>
>>>              <option value="2. Disturbances">2. Disturbances</option>
>>>              <option value="3. Assistance / Medical">3. Assistance /
>>> Medical</option>
>>>              <option value="4. Crimes Against Property">4. Crimes Against
>>> Property</option>
>>>              <option value="5. Accidents / Traffic Problems">5. Accidents
>>> /
>>> Traffic Problems</option>
>>>              <option value="6. Suspicious Circumstances">6. Suspicious
>>> Circumstances</option>
>>>              <option value="7. Morality / Drugs">7. Morality /
>>> Drugs</option>
>>>              <option value="8. Miscellaneous Service">8. Miscellaneous
>>> Service</option>
>>>              <option value="9. Alarms">9. Alarms</option>
>>>            </select>
>>>
>>>
>>>
>>
>>
>> Do test this, but I think all that's required is you make the name an
>> array:
>>
>> <select name="DPRpriority[]" form="DPRform">
>>
>
> Something like this?
>
> if $DPRpriority[0] == "7. Morality / Drugs" { echo "selected"; }
>
> I hate arrays. :D
>
> http://php.net/manual/en/language.types.array.php
>
> <?php
> $DPRpriority[]  = array(
>    1    => "a",
>    "1"  => "b",
>    1.5  => "c",
>    true => "d",
> );
> var_dump($array);
> ?>
>

Not exactly that -- I think I goofed up your variable names.
$DPRpriority is the set of values that can be selected, and DPRType
are the values returned from the form.

Here's a small snippet showing how it works, I hope:

<?php

// Initial value for demo
$DPRpriority = array(array('name' => "Crimes Against Persons",
'selected' => false),
                     array('name' => "Disturbances", 'selected' => false),
                     array('name' => "Assistance / Medical", 'selected' => 
false),
                     array('name' => "Crimes Against Property", 'selected' => 
false),
                     array('name' => "Accidents / Traffic Problems", 'selected' 
=> false),
                     array('name' => "Suspicious Circumstances", 'selected' => 
false),
                     array('name' => "Morality / Drugs", 'selected' => false),
                     array('name' => "Miscellaneous Service", 'selected' => 
false),
                     array('name' =>"Alarms", 'selected' => false));

echo "<h1>Initial value of DPRpriority:</h1><ul>\n";
foreach ($DPRpriority as $item => $value) {
  echo "<li> ".$item.": ".$value['name']." selected:
".$value['selected']." </li>\n";
}
echo "</ul>\n";

if (count($_POST) > 0) {
  // something was posted:
  echo "<h1>\$_POST:</h1><pre><code>\n";
  var_dump($_POST);
  echo "</code></pre>\n";

  echo "<h2>Items selected:</h2><ul>\n";
  foreach ($_POST['DPRType'] as $item) {
    $DPRpriority[$item]['selected'] = true;
    echo "<li>".$item.": ".$DPRpriority[$item]['name']."</li>\n";
  }
  echo "</ul>\n";

  echo "<h1>Final value of DPRpriority:</h1><ul>\n";
  foreach ($DPRpriority as $item => $value) {
    echo "<li> ".$item.": ".$value['name']." selected:
".$value['selected']." </li>\n";
  }
  echo "</ul>\n";
}

?>

<form method="post">

<select name="DPRType[]" id="DPRType[]" multiple onchange=""
size="<?php echo count($DPRpriority) ?>">
  <?php foreach ($DPRpriority as $index => $value) { ?>
<option value="<?php echo $index; ?>"<?php if ($value['selected'])
{echo ' selected="selected"';} ?>><?php echo
$value['name'];?></option>
     <?php } ?>
</select>

<input type="submit" name="submit" value="submit" />

</form>


(gist link: https://gist.github.com/tamouse/4982630 )

--- End Message ---
--- Begin Message ---

tamouse mailing lists wrote:
>I hate arrays. :D
Here's a small snippet showing how it works, I hope:

foreach ($DPRpriority as $item => $value) {
   echo "<li> ".$item.": ".$value['name']." selected:
".$value['selected']." </li>\n";
}

Question 1: when did we have to add [] to a <input> name to turn it into an array?

<input type="checkbox" name="DPRlocationdetails[]" value="Unknown">

According to phpinfo() it still comes out as $_POST['DPRlocationdetails'] without [].

Are the [] necessary?
----------------------------------------------
Question 2:
I was looking at some code in the Manual, where someone used isset and is_array.

How necessary is if(isset($_POST['DPRlocationdetails']))

and then to use: if(is_array($_POST['DPRlocationdetails']))

That seems like over kill?
----------------------------------------------
Question 3:

My code works, perfectly. In this case, I decided to attack some check-boxes first. The resulting function will work for <select multiple> too..

Does anyone see me doing something wrong in my code below?

My questions are:

Is this the only way to pass "Unknown", "Family Home" or "Apartment" into the function?

Is this correct?

---- if ($_POST['DPRlocationdetails'] == "Unknown")

Somebody once told me I had to do it this way?

---- if ("Unknown" == $_POST['DPRlocationdetails'])

John

--------------------snip---------------------------

<form action="foo.php" id="DPRform" method="post"><input value="Update" type="submit"> <input type="checkbox" name="DPRlocationdetails[]" value="Unknown" <?php filter_value($_POST['DPRlocationdetails'],"Unknown"); ?>> Unknown <input type="checkbox" name="DPRlocationdetails[]" value="Family Home" <?php filter_value($_POST['DPRlocationdetails'],"Family Home"); ?>> Family Home <input type="checkbox" name="DPRlocationdetails[]" value="Apartment" <?php filter_value($_POST['DPRlocationdetails'],"Apartment"); ?>> Apartment
</form>

<?php
function filter_value($tofilter,$tofind) {
foreach($tofilter as $value){
    if ($value == $tofind) echo "checked";
    }
}
?>



--- End Message ---

Reply via email to