Sure, I am not using the above code anymore, but here is an example of something similar:

PHP code:

   $files= findFiles($dir,'php'); //returns an array
  asort($files);
  $smarty->assign('numFiles',count($files));
  $smarty->assign('files',$files);

Smarty code:

{if $numFiles > 0}
<strong>{$numFiles}</strong> PHP files found:
<ul>
{foreach item=file from=$files}
<li><a href="viewcode.php?file={$file}">{$file}</a></li>
{/foreach}
</ul>
{else}
<strong>No PHP files found.</strong>
{/if} 

Note in above, each time it loops it puts the value in the array in the variable $file (not $files).

If you are doing associative array, it is a little different, if you need help with that let me know. Good luck.

    
    $sql = "SELECT * FROM $table ORDER BY id DESC";
    $result = mysql_db_query($database, $sql);
    
    $row = mysql_fetch_array($result);
    
    

    this is my code for fetching some values from an "article" table. The fields are body, title, date etc. If you could just show me how to assign all of these into an array and displaying them in my template file.

    Thank you for being so helpful...much appreciated.

      So why do you want to assign these values? Do you want smarty to loop through and display like body here, title here, ect... duplicating for each entry you have?

      If so, here are a few tips. You are using mysql_fetch_array(). Using this returns your result in an array with numeric index. So if your database is setup so title is first, then body, you would access these by $result[0] and $result[1], respectively.

      However, you can also have your results returned in an associate array, using mysql_fetch_assoc(). Instead of accessing $result[0], you could do $result['title'] (provided title is the name of your column in your table). So basically, it really doesn't matter which one you use, as passing to smarty is the same.

      However, with your code, you are only returning one row. Why? Because you did:

      $row= mysql_fetch_array($result)

      However, mysql_fetch_array returns all of the rows, and I believe with your code you only get the last one. So modify that that last line to be:

       while ($row= mysql_fetch_assoc($result)) $data[]= $row;
      $smarty->assign('data',$data); //send this
      

      Then in your Smarty file, all you have to do is:

      {section name=show loop=$data}
      body: {$data[show].body}
      title: {$data[show].title}
      {/section}
      

      And that should do it. Let me know if you have any problems.

        brilliant. thank you very much for detailed and quick responses. (yeah it works :รพ )

          Hey no problem, glad to help.

            a month later

            i am having the same problem with my fulltext query..
            i am missing a row each time.
            could you please tell me what was wrong with your query, and how you solved it?
            Regards...

              wouldnt closing the db link before fetching the data kinda goof things up?
              ๐Ÿ˜•

                m3avrck is sharing misinformation.

                The documentation for mysql_fetch_array VERY clearly states that fetches ONE row from the result set, and the resulting array can be referenced as an associative array or a numeric array.

                If you're dropping the first returned line (and how would you know?) it is likely that you have an error in the flow of your program.

                  a month later

                  Hi,

                  I was also stumped by this for awhile, even after reading the responses here. I have figured it out for now...until the next array!

                  Anyways, I have posted my full solution on the Vancouver PHP site so please follow the link below to read what I did1

                  http://vancouver.php.net/forum/viewtopic.php?p=672#672

                  Partial solution:
                  1) created a multidimensional array using the following code...this was the hardest part to understand...the rest is a piece of cake.

                  while ($query_data = mysql_fetch_array($result)){
                  		$dbinfo[] = array(id=>$query_data['id_lin'], 
                  						projectlink=>$query_data['projectlink_lin'],
                  						linkname=>$query_data['linkname_lin']);
                  
                  }

                  2) assign values to Smarty template
                  $smarty->assign('projectlink',$dbinfo);

                  3) smarty html portion
                  <!--{section name=linkindex loop=$projectlink}-->
                  <!--{$smarty.section.linkindex.rownum}-->)
                  <a href="<!--{$projectlink[linkindex].projectlink}-->" target="_blank">
                  <!--{$projectlink[linkindex].linkname}-->
                  </a><br>

                  cheers,
                  Paul

                    that's exactly what i do. Later i ran into the problem of adding an array to the multidimensional array (say you want to add the user name into the "news" array). In case you might need this here is the code for this sort of operation.

                    $data2[] = $row + array("hofundur" => $sql_row['userName'], "userID" => $sql_row['userID']);
                    

                    Given that $row is a mysql_mysql_fetch_assoc this code adds two new arrays to the (already) multidimensional array. Another tip: print_r function prints out arrays for humans to read...very handy if you are not sure what your code is exactly doing.

                      cool! i am sure it will come in handy soon. I am really interested in using template systems and Smarty is the first one I have tried...hopefully the last one for PHP ๐Ÿ˜‰

                      i have found very few smarty tutorials on the net so if you know of any good ones with examples pls let me know!

                      cheers,
                      Paul

                        Write a Reply...