Here is an example of how to setup your MySQL queries so that they can easily be passed into and parsed by Smarty.
The result set will be a multidimensional array. The first dimension of the array is numeric and represents each row of your result set. The second dimension of the array is an associative array where the names of the indexes are the field names you specified in your query.
| Code: |
| //Init the array to hold the results $results = Array(); //Try to connect to the DB //Setup the SQL statement //Run the query //Get the results as an associative array and put each row into the numerically indexed array //Now we get set up the query to get the number of results that were returned //Run the query //Store the resulting row //Get the total rows //Assign the results to smarty //Display the Smarty template |
Your resultset should look something like this:
| Code: |
| Array (2) 0 => Array (5) name => John Doe address => 123 Anonymous Way city => Springfield state => IL zip => 62707 1 => Array (5) name => Jane Doe address => 777 Luxury Way city => Beverly Hills state => CA zip => 90210 |
And here is an example using {section} to parse the results
| Code: |
| Displaying {$total} records :<br /> {section name=nr loop=$results} {$results[nr].name} {$results[nr].address} {$results[nr].city} {$results[nr].state} {$results[nr].zip}<br /> {sectionelse} <h1>No results found!</h1> {/section} |









































