0
<table class="table table-striped table-framed table-centered">
    <thead>
        <tr>
            {foreach from=$columns key=num item=columninfo}
                <th><font color="black">{$columninfo.columnname}</font></th>
            {/foreach}
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        {foreach from=$rows key=num item=rowinfo}
            <tr>
                <td><font color="black">{$rowinfo}</font></td>
                <td><a href="database.php?action=viewtable&table={$tableinfo.tablename}&database={$currentdatabase}"><input class="btn btn-danger" type="button" value="Delete" onclick="return confirm('Are you sure you would like to delete this row?');"/></td></a>
            </tr>
        {/foreach}
    </tbody>
</table>

I've been trying to make it output all the data in the arrays, but the second array, $rows, does not output anything.

The first one works great, and outputs all the names of the columns.

The second array looks like this.

Array ( [0] => Array ( [ID] => 3 [Name] => Eirik [Age] => 20 ) )

What I'm trying to do is to create a database management system where you can access your databases and see tables, columns and rows. Currently I'm only missing rows to complete. It fetches all the column names, and makes the table header, then goes over to rows. The problem here is that when I'm trying to print the rows into the table, I can't use the column name to access the array, due to the column names being different for every database.

2
  • Does it output "blank" strings or the "Array" string? Commented Sep 19, 2013 at 10:21
  • Array, and I know I have to access the elements, but I don't know how, as the element names are the same as the column names which are produces by the array above it. Commented Sep 19, 2013 at 10:23

1 Answer 1

1

You're on the right track: Just follow the same pattern used inside the first foreach.

$rowinfo is an array, and you need to access its elements if you want to print them. Simply write $rowinfo['ID'] (or $rowinfo.ID) to access the ID element of the $rowinfo array.

Repeat for every element you want to print.

A side note: you can use the cleaner PHP syntax for your foreach block:

{foreach $rows as $num => $rowinfo}

is equivalent to

{foreach from=$rows key=num item=rowinfo}

but in my opinion much more readable.

Regarding the EDIT, how to print a dynamic array which has varying columns (names and possibly their number) you can try the following approach:

 {foreach $rows as $num => $rowinfo}
      <tr>
          <td>
          {foreach $rowinfo as $key => $value}
              {$key}: {$value} {* /*of course you'll need to decide HOW to render them */ *}
          {/foreach}
          </td>
          <td>[...]</td></a>
      </tr>
 {/foreach}

Basically you're cycling all the rows you've got in your second array with the outer foreach. Inside the inner one you're cycling over the content of a single row, picking its keys (the column name) and their associated values.

Sign up to request clarification or add additional context in comments.

5 Comments

The problem is that the element names will change. I'm creating a database management system where you get all the names of the columns and rows. The column names will be different for every single database.
Can you please specify that in your question, then? The problem is quite different.
Sorry, just did. Is it possible using multiple variables inside a smarty tag? Something like this, {$rowinfo.$columnname}
That worked very well. Thank you very much. I might be able to finish this system now. I tried putting up the {foreach} the way you did, because as you said, it is easier to read, but it didn't work. Might be because that I'm using WHMCS.
@EirikUtvik Or probably it's using an older version of smarty. The above syntax is tested on Smarty 3.0, i cannot guarantee if it works in Smarty 2.X or earlier.

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.