Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

displaying array values in a custom HTML table

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
tacobreath9000
Smarty n00b


Joined: 22 May 2018
Posts: 4

PostPosted: Tue May 22, 2018 2:55 am    Post subject: displaying array values in a custom HTML table Reply with quote

Hello, I’m not a programmer so please bear with me.

I’m trying to modify an existing Smarty template that we’re using to display results from an in-house database application. When I select an entry from the app and choose to "print" it, the template takes over and displays the data in a basic HTML table. Instead of using that basic table, I’m trying to have the array values populate a custom HTML table that I’ve created. The custom table resembles an invoice one could create in Word document. We're hoping to generate this "invoice" though the template.

Here’s the original code:

Code:
<table>
    {foreach key=Key item=Column from=$Rows}
        <tr>
            <td><b>{$Column->GetCaption()}</b></td>
            <td>{$Rows[0][$Key]}</td>
        </tr>
    {/foreach}
</table>



Here is the original output:

Code:
<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="components/assets/css/print.css">
</head>
<body style="background-color:white">
        <table>
            <tr>
            <td><b>Id</b></td>
            <td>10</td>
        </tr>
            <tr>
            <td><b>Date Ordered</b></td>
            <td>05-08-2018</td>
        </tr>
            <tr>
            <td><b>Date Completed</b></td>
            <td>05-15-2018</td>
        </tr>
            <tr>
            <td><b>Requesting Person</b></td>
            <td>John Smith</td>
        </tr>
            <tr>
            <td><b>Agency</b></td>
            <td>outside agency</td>
        </tr>
</table>



However, I’m trying to get the data to populate the form like this:

Code:
<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="components/assets/css/print.css">
</head>
<body>
     <table width="100%" align="center">
   <tr>
    <th rowspan="2"></th>
    <th colspan="4">Our Company Name Here</th>
    <th rowspan="2"></th>
  </tr>
  <tr>
    <td colspan="4">Our Form Name Here</td>
  </tr>
  <tr>
    <td colspan="2">Client Name: <b>Jeff Appleseed</b></td>
    <td colspan="3">Location: <b>123 Main St.</b></td>
    <td>Order #: <b>10</b></td>
  </tr>
  <tr>
    <td colspan="3">Client Address: <b>4545 South Ave.</b></td>
    <td colspan="2">Order Date: <b>05-01-2018</b></td>
    <td>Due Date: <b>05-22-2018</b></td>
  </tr>
  <tr>
    <td colspan="2">Requesting Person: John Smith</td>
    <td colspan="2">Agency: outside agency</td>
    <td>Contact phone #: 555-555-5555</td>
    <td>Contact address: 3333 North Rd.</td>
  </tr>
  </tr>
  </table>
</body>
</html>


In other words, my table would present the data out of order, and the HTML is static rather than dynamically produced by the Smarty code.

When I run {debug} in the template, it shows me that the $Rows array presents data in this manner:


{$Rows} Array (32)
0 => "10"
1 => "05-08-2018"
2 => "05-15-2018"
3 => "John Smith"
4 => "outside agency"

... and so on.


I tried this (below), thinking the array values would loop through the array. But the result was that that first value, “10” (that is, id=10), gets populated in the first HTML cell. After that, each cell is blank.

<tr>
<td colspan="2">Order #: <b>{$Rows[0][$Key]}</b></td>
<td colspan="3">Date Ordered: <b>{$Rows[1][$Key]}</b></td>
<td>Date Completed: <b>{$Rows[2][$Key]}</b></td>
</tr>

I also tried {$Rows[0][$Key]} in every field, but of course each field has the same value.

Is this possible, or am I way off track? Should I be using {section] instead of {foreach}? I'm guessing it would probably be best if I defined the variables in the PHP code before the data got to the template and then list those variables in the template itself, but I don’t know enough to do that without breaking the code. I’ve tried altering the code with the limited coding knowledge I have, but nothing seems to work. Thank you for whatever assistance you can provide.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 22, 2018 5:55 pm    Post subject: Reply with quote

You don't need Foreach at all, if what you shows is true.
Your $Rows array is just a list of values related to a specific order.
Back to top
View user's profile Send private message
tacobreath9000
Smarty n00b


Joined: 22 May 2018
Posts: 4

PostPosted: Tue May 22, 2018 11:38 pm    Post subject: Reply with quote

Thanks, AnrDaemon. I've removed the {foreach} statement from the template with no adverse affect. Now it looks like I can just reference the variables in the template.

Would you happen to know the proper formatting of the variables to get the values to appear? I've tried variations of the following code (using https://www.smarty.net/docs/en/language.syntax.variables.tpl for reference), but I'm having no success:

{$Rows[0][$Key]}
{$Rows[1][$Key]}
{$Rows[2][$Key]}

Thank you for whatever help you or anyone else can provide.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed May 23, 2018 5:17 am    Post subject: Reply with quote

It seems the PHP code is not populating $Rows is the way you are wanting.

For example, in $Row, you are wanting the first array element to be:
$Row["Id"] = "10"

But the PHP is putting that element in the array as:
$Row[] = "10"

Do you have access to the PHP that assembles $Row?
Back to top
View user's profile Send private message
tacobreath9000
Smarty n00b


Joined: 22 May 2018
Posts: 4

PostPosted: Wed May 23, 2018 7:31 pm    Post subject: Reply with quote

Thanks for the reply, bsmither. Yes, I do have the original PHP file that refers to the template file. I also have several other PHP files referenced in the main PHP file. Unfortunately I cannot figure out how the variables are being passed from that file to the template. There's no reference I can find that creates the $Rows array.

I'll keep looking and playing with the template. I appreciate the help.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed May 23, 2018 7:44 pm    Post subject: Reply with quote

Typically, the code you would be wanting to find is:
Code:
$Smarty->assign("Rows",$the_array_of_row_data);
where $Smarty is the instantiated class object.
Back to top
View user's profile Send private message
tacobreath9000
Smarty n00b


Joined: 22 May 2018
Posts: 4

PostPosted: Thu May 24, 2018 3:22 am    Post subject: Reply with quote

Thank you, bsmither. With your suggestions, I looked a little more into the origin of that array. Eventually found the problem in the syntax I was using.

When I changed the code from this

Code:
{$Rows[0][$Key]}
{$Rows[1][$Key]}
{$Rows[2][$Key]}


to this

Code:
{$Rows[0][1]}
{$Rows[0][2]}
{$Rows[0][3]}


the values appeared in the table cells as expected. Hallelujah!

So thanks to both of you for guiding me in the right direction. Much appreciated!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP