How do I display the values in an array for a many-to-many association?
Sun, 02/21/2010 - 00:16
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.

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.
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?
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.
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>';
Yes, this is what I was looking for. Thanks. All this is in the controller or is the a-tags out in the view?
Hmm...getting an error:
Fatal error: Method GroupCode::__toString() must not throw an exception in
Ideas?
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
I think that $this->objCodes[0] will return an object.
You should use $this->objCodes[0]->Property
Kristof
But what will allow me to go through each element of the array one by one and display?
Hi Vexed Panda, still getting that odd error, any thoughts?
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.
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!
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.
ah you're right...thanks!
Hmm...but not doing it in .tpl.php file....using Aptana/Eclipse....