Foreach in smaty - combined with info from the database

Discussion in 'PHP' started by PET, Mar 11, 2007.

  1. #1
    Hello,

    normaly I would do this without problems in PHP, the thing is that now I use Smarty and I have to do the {foreach} in the template not in the PHP.

    This is the stuff:

    I have a query and I extract everything from a table.

    $row = mysql_fetch_assoc($result)

    This is how I place the info into the $row variabile.

    Normaly I ould do a for and simply multiply the information and make it look as I want. Now I have to do the for in the template.

    
    <table border="1">
      <tr class="table_header">
        <td>#</td>
        <td>User</td>
        <td>Nume</td>
        <td>Prenume</td>
        <td>Email</td>
        <td>Mobil</td>
        <td>Opt.</td>
      </tr>
    {foreach}
      <tr>
        <td>&nbsp;</td>
        <td>{$user}&nbsp;</td>
        <td>{$nume}&nbsp;</td>
        <td>{$prenume}&nbsp;</td>
        <td>{$email}&nbsp;</td>
        <td>{$mobil}&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    {/foreach}
      <tr class="table_header">
        <td>#</td>
        <td>User</td>
        <td>Nume</td>
        <td>Prenume</td>
        <td>Email</td>
        <td>Mobil</td>
        <td>Opt.</td>
      </tr>
    </table>
    
    HTML:
    This is the table, and as you see, the seccond row it will be mutyplied for each row I have in the database. Now should I build the foreach from smarty?

    P.S. In the table I have about 30 fields, but I want to show only 5 of them (user, nume, prenume, email, mobil).

    In PHP I would simply place $row['nume'] under the Nume coumn and so on...
     
    PET, Mar 11, 2007 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    Something like this:

    {foreach from=$row item=r name=row}
    <tr>
    <td>{$r.user}&nbsp;</td>
    <td>{$r.nume}&nbsp;</td>
    ...
    </tr>
    {/foreach}
    
    Code (markup):
     
    wmtips, Mar 11, 2007 IP
  3. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Thanks. I will try it tomorow.
     
    PET, Mar 11, 2007 IP
  4. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #4
    you can also do this

    {php}
    code here
    {/php}

    if you can do it in php easily ;)
     
    SedNaX, Mar 12, 2007 IP
  5. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #5
    This is the template:
    {foreach from=$row item=r name=row}
      <tr>
        <td>&nbsp;</td>
        <td>{$r.user}&nbsp;</td>
        <td>{$r.nume}&nbsp;</td>
        <td>{$r.prenume}&nbsp;</td>
        <td>{$r.email}&nbsp;</td>
        <td>{$r.mobil}&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    {/foreach}
    HTML:
    This is the PHP
    
                           $query = "SELECT * FROM `users`";
                            $result = mysql_query($query) or die(mysql_error());
                            $row = mysql_fetch_assoc($result);
                      
                            $smarty->assign('row',$row);
                            
                            $smarty->assign('title','Rheal - Admin CP - Show Users');    // PAGE TITLE
                            $smarty->display('showusers.tpl');
    
    PHP:
    I'm getting something like in the screenshot. Any sugestions? I'm a little tired to think this part.

    THanks

    P.S. I have about 7 entryes in the table.
     

    Attached Files:

    PET, Mar 12, 2007 IP
  6. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #6
    {php}
    $query = "SELECT * FROM `users`";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    echo "<tr>";
    echo "<td>" . $row['nume'] . "</td>";
    //etc...
    echo "</tr>";
    {/php}
     
    SedNaX, Mar 12, 2007 IP
  7. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #7
    yeah...PHP I know how to do it, but I want do to it Smarty style :p
     
    PET, Mar 12, 2007 IP
  8. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Yes, look at the code

    the {php} and {/php} can be used in smarty. Those are smarty tags, and everything in between is php code.
     
    SedNaX, Mar 12, 2007 IP
  9. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #9
    I understand that, and I used it, I jsut wanted to use smarty tags not php style.
     
    PET, Mar 12, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  11. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #11
    I have already read it and I still can't figure it out.
     
    PET, Mar 12, 2007 IP
  12. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #12
    You have only one single row after mysql_fetch_assoc execution. You need to iterate through all results (code not tested, just an example):

    $rows = array();
    while ($row = mysql_fetch_assoc($result)) 
     $rows[] = $row;
    
    $smarty->assign_by_ref('row',$rows);
    PHP:
    Note that assign_by_ref is more suitable in that case, it consumes less memory.
     
    wmtips, Mar 13, 2007 IP