Jump to content

help with associative arrays


EricOnAdventure
 Share

Recommended Posts

Hey guys I need a bit of help with associative arrays.

 I am using the mysql php generator to create a rather nice searchable database. The software uses smarty as to reate page templets, that may be useful for my issue.

 

My issue is this I want to echo an aspect of an array but I can get that to happen, if I print the array I get this:

Array ( [university] => Array ( [Caption] => University [Value] => Texas A & M International University [DisplayValue] => Texas A & M International University ) [sTABBR] => Array ( [Caption] => STABBR [Value] => TX [DisplayValue] => TX ) [PREDDEG] => Array ( [Caption] => PREDDEG [Value] => 3 [DisplayValue] => 3 ) [HIGHDEG] => Array ( [Caption] => HIGHDEG [Value] => 4 [DisplayValue] => 4 ) [CONTROL] => Array ( [Caption] => CONTROL [Value] => 1 [DisplayValue] => 1 )……and so on….

 

how would I go about echoing only the 'university'?

I’ve tried a lot of combinations, but nothing seemed to work, any idea?

Link to comment
Share on other sites

Going for displaying all the DisplayValue values something like the following should work:

 

 

foreach ($Grid.row as $key => $uniArray){ //iterate through the top level - $uniArray holds each "University" array one at a time
  foreach ($uniArray as $k =>$uni){ // iterate through the items of each "University" array (Caption / Value / DisplayValue etc...)
    if($k == 'DisplayValue')){
      echo $uni['DisplayValue']
    }
  }
}
Link to comment
Share on other sites

Thanks muddy,

I am sure that would work in normal conditions, unfortunately as this generator uses smarty the code runs into conflict with smarty's code...

Here is how the page creates an output of every bit of info in the array.

 

 

</table>
       <div class="">
             <div class="col-lg-8">
                {foreach from=$Grid.Row item=Cell}
                    <div class="form-group">
                        <label class="col-sm-3 control-label">
                            {$Cell.Caption}
                        </label>
                        <div>
                            <div class="form-control-static">
                                {$Cell.DisplayValue}
                            </div>
                        </div>
                    </div>
                {/foreach}
            </div>
        </div>

 

Might this help you beter understand how this system works? If I can just out the information associated with the university then I can figure out the rest.

Link to comment
Share on other sites

In fact, having had another look over the Smarty docs I think it's actually more like:

{foreach from=$Grid value=row}
  {$row.University.DisplayValue}
{/foreach}

If that doesn't go deep enough you could try:

{foreach from=$Grid value=row}
  {foreach from=$row value=Uni}
    {$Uni.DisplayValue}
  {/foreach}
{/foreach}

::Edit - missed off half the post :(

Edited by Muddy_Funster
Link to comment
Share on other sites

Ok thanks, that works, although I get quite a few warnings all related to line 26 in an associated file

<?php $_from = $this->_tpl_vars['Grid']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
    foreach ($_from as $this->_tpl_vars['row']):
?>
!!!!!!line 26--->  <?php echo $this->_tpl_vars['row']['University']['DisplayValue']; ?>

<?php endforeach; endif; unset($_from); ?>
Link to comment
Share on other sites

What a fun approach, anywhere here is what I get, (FYI I changed the university and therefor data row from the previous data)

array(0) {
}
  

bool(true)
  

string(28) "unidata.php?operation=return"
  

string(34) "unidata.php?operation=edit&pk0=552"
  

bool(false)
  

string(38) "unidata.php?operation=printrec&pk0=552"
  

array(0) {
}
  

string(7) "Unidata"
  

array(1) {
  ["id"]=>
  string(3) "552"
}
  

array(79) {
  ["University"]=>
  array(3) {
    ["Caption"]=>
    string(10) "University"
    ["Value"]=>
    string(23) "Saint Augustine College"
    ["DisplayValue"]=>
    string(23) "Saint Augustine College"
  }
  ["STABBR"]=>
  array(3) {
    ["Caption"]=>
    string(6) "STABBR"
    ["Value"]=>
    string(2) "IL"
    ["DisplayValue"]=>
    string(2) "IL"
  }
  ["PREDDEG"]=>
  array(3) {
    ["Caption"]=>
    string(7) "PREDDEG"
    ["Value"]=>
    string(1) "2"
    ["DisplayValue"]=>
    string(1) "2"
  }
Link to comment
Share on other sites

OK, I think it's because you have the STABBR and PREDDEG arrays on the same level as the "University" array, so when it's going through the results it's hitting these array key names and throwing our warnings because they don't match whet you are asking it to display.  Try:

 

<?php if($this->_tpl_vars['row']['University']){echo $this->_tpl_vars['row']['University']['DisplayValue'];} ?>
Link to comment
Share on other sites

I cant seem to make lasting changes to this file, at least not now, as the program is auto-changing this file as I make changes to my templet. I disabled warnings, thinking that would fix the issue, at least for now, and it did, except that it also helped me spot a new issue.

 

So I am not only getting the row sent back to me, but also u

 

for example

 

u u u U Saint Augustine College

u u u U IL,

u u u U 2,

 

This is what I get from my first three trials related to :

    {foreach from=$Grid item=row}
  {$row.XXXXXX.DisplayValue}
{/foreach}

XXXXXbeing whatever it is I am calling.

Link to comment
Share on other sites

The file you are talking about is an auto-generated compiled version of the template. You should not be changing this file except possibly for debugging problems. Smarty could re-generate it at any time by re-compiling the template.

 

What specific warning are you getting on that line? If things are done properly then you shouldn't be seeing any warnings.

Edited by kicken
Link to comment
Share on other sites

What a fun approach, anywhere here is what I get, (FYI I changed the university and therefor data row from the previous data)

Give your output of the $row values it would seem like you need to identify whatever key contains the university details and handle it differently.

 

What exactly does the $Grid/$row variable represent, and what output specifically are you trying to generate based on those variables?

 

Your $row variable is obviously not always an array. In some instances it is a string, or a boolean, or an array with a different structure.

Link to comment
Share on other sites

Hey Kicken, your right that is generated by smarty so editing that file is rather pointless

So let me display what print r gives me

for $grid.row and $grid I get this

 

Array ( [university] => Array ( [Caption] => University [Value] => Texas A & M International University [DisplayValue] => Texas A & M International University ) [sTABBR] => Array ( [Caption] => STABBR [Value] => TX [DisplayValue] => TX ) [PREDDEG] => Array ( [Caption] => PREDDEG [Value] => 3 [DisplayValue] => 3 ) [HIGHDEG] => Array ( [Caption] => HIGHDEG [Value] => 4 [DisplayValue] => 4 ) [CONTROL] => Array ( [Caption] => CONTROL [Value] => 1 [DisplayValue] => 1 )……and so on….

 

for $Grid I also get all of the above, except I also get a few other pieces of data such as ID and database name.

 

The warning I am getting is:

 

Warning: Illegal string offset 'University' in C:\xampp\htdocs\uni\templates_c\%%C6^C67^C6773CE7%%gridusa.tpl.php on line 26

Warning: Illegal string offset 'DisplayValue' in C:\xampp\htdocs\uni\templates_c\%%C6^C67^C6773CE7%%gridusa.tpl.php on line 26
u
 

Notice the "u" on the 2nd notice. I get 8 of these warnings and 4 "u"'s

 

Line 26 comes from the generated file, as follows

<?php $_from = $this->_tpl_vars['Grid']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
    foreach ($_from as $this->_tpl_vars['row']):
?>
//line 26 is the one below.
<?php echo $this->_tpl_vars['row']['University']['DisplayValue']; ?>

<?php endforeach; endif; unset($_from); ?>

I have no idea why the U even exists, and it exists for matter what I am calling for.

Any ideas?

Link to comment
Share on other sites

I have no idea why the U even exists, and it exists for matter what I am calling for.

It exists because as can be seen from your earlier post $row is not always an array. For example:

 

string(28) "unidata.php?operation=return"

 

So what you end up doing in your template is the equivalent of

$row = "unidata.php?operation=return";
echo $row['University']['DisplayValue'];
Obviously that is wrong. You get a 'u' out of that because $row['University'] become $row[0] due to casting the index as an integer which results in the first letter of the string, 'u'.

 

Probably what you want to do is figure out which key your university list is stored under within $Grid and only loop over that specific key, not the entire $Grid array.

Link to comment
Share on other sites

Thanks Kicken,

I've been trying to follow your advice but I haven't ended up anywhere better.

in $Grid, the 3rd array is the University Key

I tried everything I could think of to get it to work from this point, but everything failed. Any advice on which direction I should take this?

Link to comment
Share on other sites

Add var_dump($_from); just before the foreach in the generated file so you can see what $Grid contains. Find the key for the university list then change you template to

 

{foreach from=$Grid.whateverThekeyIs item=row}
Link to comment
Share on other sites

Thanks Kicken, I tried that but I can't seem to get it right. Here is what the var dump gives m

 

array(10) { ["Details"]=> array(0) { } ["HasEditGrant"]=> bool(true) ["CancelUrl"]=> string(28) "unidata.php?operation=return" ["EditUrl"]=> string(32) "unidata.php?operation=edit&pk0=1" ["PrintOneRecord"]=> bool(false) ["PrintRecordLink"]=> string(36) "unidata.php?operation=printrec&pk0=1" ["ExportButtons"]=> array(0) { } ["Title"]=> string(7) "Unidata" ["PrimaryKeyMap"]=> array(1) { ["id"]=> string(1) "1" } ["Row"]=> array(79) { ["University"]=> array(3) { ["Caption"]=> string(10) "University" ["Value"]=> string(24) "Alabama A & M University" ["DisplayValue"]=> string(24) "Alabama A & M University" } ["STABBR"]=> array(3) { ["Caption"]=> string(6) "STABBR" ["Value"]=> string(2) "AL" ["DisplayValue"]=> string(2) "AL" } ["PREDDEG"]=> array(3) { ["Caption"]=> string(7) "PREDDEG" ["Value"]=> string(1) "3" ["DisplayValue"]=> string(1) "3" } ["HIGHDEG"]=> array(3) {

 

every key combination I have tried has failed completely, any advice?

Link to comment
Share on other sites

If you look at the cleaned up version of that output, you can see that the key which contains the values you are interested in is Row. This means you want to be referencing $Grid.Row in your template.

array(10) { 
["Details"]=> array(0) { } 
["HasEditGrant"]=> bool(true) 
["CancelUrl"]=> string(28) "unidata.php?operation=return" 
["EditUrl"]=> string(32) "unidata.php?operation=edit&pk0=1" 
["PrintOneRecord"]=> bool(false) 
["PrintRecordLink"]=> string(36) "unidata.php?operation=printrec&pk0=1" 
["ExportButtons"]=> array(0) { } 
["Title"]=> string(7) "Unidata" 

["PrimaryKeyMap"]=> array(1) { ["id"]=> string(1) "1" } 

["Row"]=> array(79) { 
	["University"]=> array(3) { 
		["Caption"]=> string(10) "University" 
		["Value"]=> string(24) "Alabama A & M University" 
		["DisplayValue"]=> string(24) "Alabama A & M University" 
	}
	["STABBR"]=> array(3) { 
		["Caption"]=> string(6) "STABBR" 
		["Value"]=> string(2) "AL" 
		["DisplayValue"]=> string(2) "AL" 
	} 
	["PREDDEG"]=> array(3) { 
		["Caption"]=> string(7) "PREDDEG" 
		["Value"]=> string(1) "3" 
		["DisplayValue"]=> string(1) "3" 
	} 
	["HIGHDEG"]=> array(3) {
Now, what's next depends on what specifically you want. You say you only want to display the University, but University is an array with three values, so which value do you want? Or do you want all three?

 

I'm going to assume you only want the DisplayValue. If so, then you don't even need a loop at all, you'd just reference it as so: $Grid.Row.University.DisplayValue

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.