Link to home
Start Free TrialLog in
Avatar of global_expert_advice
global_expert_adviceFlag for India

asked on

passing data arrays to multiple columns using smarty php and mysql

dear experts,
After a long try, i got 90% success in what i'm trying to acheive.
What i want?
is to display the list of all the features from amenities column of my database in to 3 or 4 columns.
in regular sql query we get like
amenities1
amenities2
amenities3
amenities4
....
but i want in this manner
amenities 1          amenites2       amenities3
amenities 4          amenites5       amenities6
amenities 7          amenites8       amenities9

I want this in smarty way...
the code which i'm using is giving all the 11 columns instead dividing them to 3 columns.

My code is below
smarty code:

<?

      require("libs/Smarty.class.php");      
      $smarty = new smarty;
      $smarty->allow_php_templates= true;
      $smarty->compile_check = true;
      $smarty->caching = true;
      $smarty->cache_lifetime = 100;

      
      include("dbconnect.php");
// Amenities
      $i = 0;
      $amenities_query = "SELECT id,amenities FROM amenities";
      $amenitiesresult = mysql_query($amenities_query) or die(mysql_error());
      while ($row = mysql_fetch_array($amenitiesresult))
      {
      $amenitiesid = $row['id'];
      $amenitiesname = $row['amenities'];
      $columns = '4';
      
      $arr_amenitiesid[$i]            = $amenitiesid;
      $arr_amenitiesname[$i]            = $amenitiesname;
      
      $i++;
      }
// smarty codes for amenities...
      $smarty->assign("amenitiesid"         , $arr_amenitiesid);
      $smarty->assign("amenitiesname"         ,$arr_amenitiesname);

      mysql_close($db_connect);
      
      $smarty->display('list_property_optional_details.tpl');      


?>

list_property_optional_details.tpl code
<table>
    <tr>
    {assign var="col" value="0"}
    {section name=element loop=$amenitiesname}
        {if $col == $columns}
            </tr><tr>{assign var="col" value="0"}
        {/if}
        <td><input type="checkbox" name="C9" value="{$amenitiesname[element]}">{$amenitiesname[element]}</td>
        {assign var="col" value="'$col+1'"}
    {/section}
    {assign var="remainder" value="'$columns-$col"}
    {section name=emptyElement loop=$remainder}
        <td>&nbsp;</td>
    {/section}
    </tr>
</table>

Could any of the smarty expert let me know how solve this problem...
In reply please explain with modified code..
A quick reply will be much appreciated
ASKER CERTIFIED SOLUTION
Avatar of Ovunc Tukenmez
Ovunc Tukenmez
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@jet-black: I see you, too, are a fan of LMGTFY

;-)

~Ray
@Ray_Paseur
I like it :)
Avatar of global_expert_advice

ASKER

thanks for post you did...
solving the problem is important rather giving url links..
searching i too can do... but what i asked is to solve the above error..
I rectified the problem self... and here is the solution

{* $cols is the number of columns you want *}
<table border=1>
  {section name=tr loop=$data step=$cols}
  <tr>
    {section name=td start=$smarty.section.tr.index
loop=$smarty.section.tr.index+$cols}
    <td>{$data[td]|default:"& nbsp;"}</td>      
    {/section}
  </tr>
  {/section}
</table>
@global_expert_advice: I do not really have a dog in this hunt, so I am not going to object, but I think jet-black did EXACTLY the right thing for you, which is point you to a learning resource.  Obviously you had not found those resources before jet-black pointed you to them.  

Many of us here are professional programmers, and we do not write other people's code for free - we get paid to do that.  The spirit of EE seems to work best when we teach a man to fish, rather than give him a dead fish.
@Ray_Paseur
I totally agree and thank you for common sense.

@global_expert_advice
This site helps programmers to find a solution. Most times giving a link exactly does that.

If you want someone to write code for you, here is the list of these type of popular websites:
elance.com
freelancer.com
scriptlance.com
rentacoder.com
unfair closing.
I have explained in detail in the message ID:26193361
Partly accepted answer
@DropBearMod
Thank you.