Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

make table with array, but how ?

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
ShoAn
Smarty Rookie


Joined: 04 May 2004
Posts: 5

PostPosted: Sat May 08, 2004 3:52 am    Post subject: make table with array, but how ? Reply with quote

hallo,

i don't know how to make from this array (debug-console):

Quote:
{$fleet}

Array (3)
0 => Array (6)
apollo => 15
athena => 1
hercules => 0
medusa => 0
ulysses => 2
ursa => 99999
1 => Array (6)
apollo => 100
athena => 0
hercules => 111
medusa => 0
ulysses => 666
ursa => 0
2 => Array (6)
apollo => 100
athena => 22
hercules => 0
medusa => 444
ulysses => 111
ursa => 0


.... a table like this:

Code:
shipname | home  | fleet 1 | fleet 2 |
--------------------------------------
apollo   | 15    | 100     | 100     |
athena   | 1     | 0       | 22      |
hercules | 0     | 111     | 0       |
medusa   | 0     | 0       | 444     |
ulysses  | 2     | 666     | 111     |
ursa     | 99999 | 0       | 0       |


i have allready tryed many different variances, but nothing is like my imagination Sad
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat May 08, 2004 11:30 am    Post subject: Reply with quote

Code:

{foreach from=$fleet.0 key=ship item=unused}
<tr>
  {foreach from=$fleet key=col item=unused}
  <td>{$fleet.$col.$ship}</td>
  {/foreach}
</tr>
{/foreach}


of course things would be much easier if the array was ordered by ship in the first dimension.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ShoAn
Smarty Rookie


Joined: 04 May 2004
Posts: 5

PostPosted: Wed Jun 09, 2004 8:40 pm    Post subject: Reply with quote

thanks messju for the inspiration

my solution (sorry, about the bad english Wink):

here is the code i use. don't be scared about the game-class Wink
i don't know it goes easyer, but it works ^^

[php:1:3e91b200ed]<?php
$game->doQuery("SELECT
building
FROM
build_values
WHERE
cat = 'ships'");

while($row = mysql_fetch_assoc($game->_result))
{
$building_name[] = $row["building"];
}


$game->doQuery("SELECT
*
FROM
built_ships
WHERE
username = '".$game->_user["username"]."'
ORDER BY
fleet
ASC");

$i = 0;

while($row = mysql_fetch_assoc($game->_result))
{
for($loop = 0; $loop < count($building_name); $loop++)
{
$fleet[$i][$building_name[$loop]] = $row[$building_name[$loop]];
}

$i++;
}

$smarty->assign("fleet", $fleet);
$smarty->display("fleets.tpl");
?>[/php:1:3e91b200ed]

Code:
<table class="main" cellspacing="2" cellpadding="5">
<tr>
  <td>name</td>
{section name=i loop=$fleet}
  <td>
{if $smarty.section.i.index == 0}
  orbit
{else}
  fleet {$smarty.section.i.index}
{/if}
  </td>
{/section}
</tr>
{foreach from=$fleet.0 key=ship item=unused}
<tr>
  <td>{$ship}</td>
{foreach from=$fleet key=col item=unused}
  <td><input type="text" name="{$ship}" value="{$fleet.$col.$ship}"></td>
{/foreach}
</tr>
{/foreach}
<tr><td colspan="{$smarty.section.i.total+1}" class="center"><input type="submit" name="{$form.submit}" value="change"></td></tr>
</table>


and the result looks like this:
Code:
<table class="main" cellspacing="2" cellpadding="5">
<tr>
  <td>name</td>
  <td>
  orbit
  </td>
  <td>
  fleet 1
  </td>
</tr>
<tr>
  <td>apollo</td>
  <td><input type="text" name="apollo" value="15"></td>
  <td><input type="text" name="apollo" value="100"></td>
</tr>
<tr>
  <td>athena</td>
  <td><input type="text" name="athena" value="1"></td>
  <td><input type="text" name="athena" value="0"></td>
</tr>
<tr>
  <td>hercules</td>
  <td><input type="text" name="hercules" value="0"></td>
  <td><input type="text" name="hercules" value="111"></td>
</tr>
<tr>
  <td>medusa</td>
  <td><input type="text" name="medusa" value="0"></td>
  <td><input type="text" name="medusa" value="0"></td>
</tr>
<tr>
  <td>ulysses</td>
  <td><input type="text" name="ulysses" value="2"></td>
  <td><input type="text" name="ulysses" value="666"></td>
</tr>
<tr>
  <td>ursa</td>
  <td><input type="text" name="ursa" value="99999"></td>
  <td><input type="text" name="ursa" value="0"></td>
</tr>
<tr><td colspan="3" class="center"><input type="submit" name="submit" value="change"></td></tr>
</table>
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development 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