How do I display the values in an array for a many-to-many association?

Login or register to post comments
15 replies [Last post]
Offline
Joined: 12/10/2008

I have for each user an association of possible codes through an association table.

I want, for a logged on user, to display all the values of those codes.

Here is the array:

$this->objCodes = $this->objUser->GetGroupCodeArray();

The first value of the array is:

$this->objCodes[0]->SignupCode;

However, I want to display all of the values (and of course be able to surround them with an so that I can display the other Users with the same SignupCode value.

Thanks.

LaCeja's picture
Offline
Joined: 11/04/2009

kingwithin, I had two situations. In one, I used a drop down list box (QListBox) and in the other I created an array of QTextBoxes. In the first case, just use a foreach loop to populate the list box. In the second, just use a foreach loop to create the array of QTextBoxes, then you can render them in a for loop.

Offline
Joined: 12/10/2008

What is the format of the foreach loop? I basically want it to output the values of the code as a text string where I can wrap an tag around it:

foreach ($this->objCodes as $objCode) {

}

HOw did you output it in the template?

LaCeja's picture
Offline
Joined: 11/04/2009

For example, you might create your controls like this:

$row = 0;
$objAlumsoaktimedets = Alumsoaktimedets::LoadArrayByAlumsoaktimehdrsId($strContrilId);
      foreach ($objAlumsoaktimedets as $objDets){
        $this->lbldetId[$row] = new QIntegerTextBox($this->objParentObject);
        $this->lbldetId[$row]->Text = $objDets->Id;
        $this->lbldetId[$row]->Name = $objDets->Id;
        $this->txtdetLowThick = new QTextBox($this->objParentObject);
        $this->txtdetLowThick->Text = $objDets->txtdetLowThick;
        $this->txtdetLowThick->ToolTip = QApplication::Translate('Material Thickness');

...
        $row++
      }

You can create a control for any or all columns in the table and each control can be of whatever type dictated by the data and have any of the appropriate properties. A text box control would be QTextBox for example. Interestingly, each of those controls could even be something like an Autocomplete TextBox.

To render them, just use a for loop:

    <?php for ($row = 0; $row < $this->maxrows->Text; $row++) {

               
$this->txtdetLowThick[$row]->Render();
          ...

          }
?>

In the example, I used Render instead of RenderWithName, because I rendered the labels as headings in a list.

You could also choose to render them into a list box. In my case, I created a fixed number of controls, populated only those which had rows from the database, leaving the remaining rows available for the user to add new data.

Offline
Joined: 03/31/2008

If all you want to do is wrap it in tags, I'd recommend keeping it simple:

$codes = $this->objUser->GetGroupCodeArray();
foreach($codes as $code)
  echo '<a href="whatever.php">'.$code.'</a>';

Offline
Joined: 12/10/2008

Yes, this is what I was looking for. Thanks. All this is in the controller or is the a-tags out in the view?

Offline
Joined: 12/10/2008

Hmm...getting an error:

Fatal error: Method GroupCode::__toString() must not throw an exception in

Ideas?

Offline
Joined: 12/10/2008

Puzzled, I get a correct count for the array with:

$this->strCodeCount = count($this->objCodes);

But when I used the code or try:

<?php echo $this->objCodes[0] ?>

I get the error: Fatal error: Method GroupCode::__toString() must not throw an exception

Not sure why, I am just trying to output each value in the Array $this->objCodes

Offline
Joined: 01/09/2008

I think that $this->objCodes[0] will return an object.

You should use $this->objCodes[0]->Property

Kristof

Offline
Joined: 12/10/2008

But what will allow me to go through each element of the array one by one and display?

Offline
Joined: 12/10/2008

Hi Vexed Panda, still getting that odd error, any thoughts?

Offline
Joined: 03/31/2008

As was mentioned above, $code contains a GroupCode object. So you should either be outputting a property directly:

$codes = $this->objUser->GetGroupCodeArray();
foreach($codes as $code)
  //$code is of type GroupCode
  echo '<a href="whatever.php">'.$code->Code.'</a>';

Or fix your GroupCode's __toString definition, since apparently it's currently throwing an exception.

Offline
Joined: 12/10/2008

Yah, thanks....I figured out that I need to echo out the property....thanks!

In the future, how can I find the names of the property, I did it by outputting the array, but is there a way from Qcodo (e.g. it will be the column names exactly or do I need to go into one of the codegen classes and find it there?

Thank you!

alex94040's picture
Offline
Joined: 11/06/2008

Going into the codegen classes is your best bet. A good IDE like Komodo (or Eclipse) will also offer you code-completion with the property names.

Offline
Joined: 12/10/2008

ah you're right...thanks!

Offline
Joined: 12/10/2008

Hmm...but not doing it in .tpl.php file....using Aptana/Eclipse....