1

hi all in my view I'm getting this error when trying to print out information in my array.

here is my function in the controller

public function view($name)
$fields = $this->Template->Field->find('list',array( 
          'fields'=> array('name'),
          'conditions' => array(
          'template_id'=> $name)));
$this->set('field', $fields);

here is the view

</br><h2>Here is your template fields</h2></br>

            <?php if(!empty($field))
                  {
                    foreach($field as $name)
                    {?>
                        <tr>
                        <td>
                        <?php echo $name['Field']['name']; ?>
                        </tr></br>
                        <?php
            }
            }
            else
            {?>
             <tr> <td>No Templates Found.</td></tr>
             <?php 
             }?>
1
  • Fatal error: Cannot use string offset as an array in relation to this line <?php echo $name['Field']['name']; ?> Commented Aug 10, 2012 at 12:28

2 Answers 2

2

As I already answer some of your questions. So what I understood about your project is "Template hasMany Fields" and you already defined an association-ship in the corresponding models.

You should use the following code into your view:

<?php if(!empty($field))
              {
                foreach($field as $name)
                {?>
                    <tr>
                    <td>
                    <?php echo $name; ?>
                    </td>
                    </tr>
                    <?php
        }
        }
        else
        {?>
         <tr> <td>No Template Fields Found.</td></tr>
         <?php 
         }?>
Sign up to request clarification or add additional context in comments.

6 Comments

it is going straight to the else statement :/
Array ( [1] => amount [2] => description [3] => totalacctowing [4] => pmtinfo ) list of field names
it prints it out in a horizontal line but i need to access other information to do with those fields and can't access them without ['field']['id'] aswell
no, because if i try print out the information related to that input it goes straight to else
If there is no result for any template, then it will always lie in else condition. Check your query.
|
0

you have operation with type list so the result will be an array in [field.id] => [field.name]

then you will require following in view

<?php foreach($field as $name): ?>
<?= $name ?>
<?php endforeach ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.