Jump to content

Smarty, Arrays and Serialization


elmario

Recommended Posts

I have a serialized array stored in a database field in the following format:

 

Unserialized: '1' => '5000', '2' => '2000'

Serialized: a:2:{i:1;s:4:"5000";i:2;s:4:"2000";}

 

The key is a database ID and the value is just a number.

Next I have a table which stores some information about each ID

 

|ID|NAME|DESCRIPTION|ENABLED?|

-----------------------------------------------------

1 | Test | Description goes here| TRUE

2 | Hello | World                        | TRUE

 

What I want to do is fetch the serialized values from database, unserialize them and then loop through them using the smarty template engine. The thing is that I want to include some more information, that fetched from the database, based on the KEY of the array.

 

I suck at explaining crazy stuff like this so I'll just provide some images and code to help out

 

Current I have this PHP code ( I had no idea on how to approach the problem so I just wrote stuff I thought would work ):

<?php

require_once ( 'common-ingame.php' );
$template->assign ( 'page_title', 'Faction Standings' );

$unserialized = unserialize ( $characterInfo['charactersREPUTATION'] ); # IE: a:2:{i:1;s:4:"5000";i:2;s:4:"2000";}
$names = array ( );
foreach ($unserialized as $faction_id => $faction_reputation) { 
$data = mysql_Fetch_assoc ( mysql_query ( "SELECT `worldfactionNAME` FROM `world_factions` WHERE `worldfactionID` = {$faction_id}" ) );
$names[] = $data['worldfactionNAME'];
}

$final_array = array_combine ( $unserialized, $names );

$template->assign ( 'reputation', $final_array );

$template->display ( 'my-rep.tpl' );

 

Smarty Template File . TPL

{include file="include/header-ingame.tpl"}
<h3>Faction Standings</h3>
<table class='ng-table'>
<tr><th>Faction</th><th>Reputation</th></tr>
{foreach from=$reputation key=faction_name item=faction_reputation}
<tr><td>{$faction_reputation}</td><td>{$faction_name} / 10000</td></tr>
{/foreach}
</table>
{include file="include/footer-ingame.tpl"}

 

Screenshot of results is attached

 

What I want to do is add extra information, fetched from the database, to the table.

My table structure:

|FACTIONID|FACTION NAME|FACTION DESCRIPTION| FACTION ENABLED? ( NOT USED YET)|

-----------------------------------------------------------------------------------------------------------------------

1                  Horus Brotherhood    Description 1          TRUE

2                  Darkmist Clan            Description 2          TRUE

 

 

Currently all that's being displayed is the faction name, which is selected based on the array key, and the reputation value which is fetched from the array value. What I want to do is add the description column value to the table.

 

Anyone care to help? :)

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/216776-smarty-arrays-and-serialization/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.