Only one variable in an array will display.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dissonantallure
Forum Newbie
Posts: 21
Joined: Tue Feb 03, 2009 7:48 pm

Only one variable in an array will display.

Post by dissonantallure »

Hello all, I have an array with four keys and four values in it.

Code: Select all

function prepare_functionality()
        {
            $this->contents = array('Text' => 'Text',
                                     'Flash' => 'Flash',
                                     'Audio' => 'Audio',
                                     'Video' => 'Video');
        }




Code: Select all

function process_functionality(&$form)
        {
            $this->prepare_functionality();
            $content = $this->coalesce($form['content']);
 
            if (in_array($content, $this->contents))
                $this->setValue('content', $content);
            else
                $this->addError('content', 'Please make a selection:');
 
 
            return !$this->isError();
        }
on a separate page I try to display all values:

Code: Select all

<table>
            <tr>
              <td>Content:</td>
              <td>
 
                  <?php foreach ($wizard->contents as $b) { ?>
                    <label><?= $b ?><input name="content" type="checkbox" value="<?= $b ?>"<?php if ($wizard->getValue('content') == $b) { ?> checked="checked"<?php } ?>/></label>
                      
 
                  <?php } ?>
 
              </td>
              <td>
                <?php if ($wizard->isError('content')) { ?>
                  <?= $wizard->getError('content') ?>
                <?php } ?>
              </td>
            </tr>
          </table>

Code: Select all

<table><tr>
              <td>Content:</td>
              <td><?= $wizard->getValue('content') ?></td>
            </tr>
          </table>
My problem is that when I try to display any values that have been checked it only displays one value at a time. How can I display the full array of values?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Only one variable in an array will display.

Post by aceconcepts »

So, from the page where you try and display your array values it's just outputting one value from your array?
dissonantallure
Forum Newbie
Posts: 21
Joined: Tue Feb 03, 2009 7:48 pm

Re: Only one variable in an array will display.

Post by dissonantallure »

So, from the page where you try and display your array values it's just outputting one value from your array?
Yes that is correct. Allow me to clarify,

I am using the ZervWizard.class.php from phpriot.com and have used his example to create my own class. Below is an example of the format that the ZervWizard.class uses to take variables.

Code: Select all

$this->prepare_functionality();
            $content = $this->coalesce($form['content']);
 
            if (in_array($content, $this->contents))
                $this->setValue('content', $content);
            else
                $this->addError('content', 'Please make a selection:');
 
 
            return !$this->isError();



Code: Select all

<table>
            <tr>
              <td>Content:</td>
              <td>
 
                  <?php foreach ($wizard->contents as $b) { ?>
                    <label><?= $b ?><input name="content" type="checkbox" value="<?= $b ?>"<?php if ($wizard->getValue('content') == $b) { ?> checked="checked"<?php } ?>/></label>
                      
 
                  <?php } ?>
 
              </td>
              <td>
                <?php if ($wizard->isError('content')) { ?>
                  <?= $wizard->getError('content') ?>
                <?php } ?>
              </td>
            </tr>
          </table>

Code: Select all

 
<b>Content:</b>
              <?= $wizard->getValue('content') ?>

This displays the values of regular text boxes and drop down menus fine, but will not work for check boxes. It will only display one value at a time.

I realize this is because I am calling only one value "content". However, I cannot find the correct syntax to solve this issue.
frao_0
Forum Commoner
Posts: 27
Joined: Sat Aug 08, 2009 3:52 am
Location: Toulouse, France

Re: Only one variable in an array will display.

Post by frao_0 »

Hey

seems to me it's normal since all your check boxes have the same value for the name attribute. Try maybe

foreach ($wizard->contents as $k=>$b) { ?>
<label><?= $b ?><input name="content[<?= $k ; ?>]" type="checkbox" value="<?= $b ?>"<?php if ($wizard->getValue('content') == $b) { ?> checked="checked"<?php } ?>/></label>

And then fix a little bit your $wizard->getValue method so it can parse arrays
Post Reply