passing data arrays to multiple columns using smarty php and mysql

Posted on 2010-01-06
Last Modified: 2013-12-12
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
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:


      $smarty = new smarty;
      $smarty->allow_php_templates= true;
      $smarty->compile_check = true;
      $smarty->caching = true;
      $smarty->cache_lifetime = 100;

// 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;
// smarty codes for amenities...
      $smarty->assign("amenitiesid"         , $arr_amenitiesid);
      $smarty->assign("amenitiesname"         ,$arr_amenitiesname);



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

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
Question by:global_expert_advice
    LVL 12

    Accepted Solution

    LVL 107

    Expert Comment

    by:Ray Paseur
    @jet-black: I see you, too, are a fan of LMGTFY


    LVL 12

    Expert Comment

    I like it :)
    LVL 1

    Author Comment

    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}
        {section name=td start=$
        <td>{$data[td]|default:"& nbsp;"}</td>      
    LVL 107

    Expert Comment

    by:Ray Paseur
    @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.
    LVL 12

    Expert Comment

    I totally agree and thank you for common sense.

    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:
    LVL 12

    Expert Comment

    unfair closing.
    I have explained in detail in the message ID:26193361
    LVL 1

    Author Closing Comment

    Partly accepted answer
    LVL 12

    Expert Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now