Jump to content


[SOLVED] Loops and template files


1 reply to this topic

#1 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 10 February 2007 - 02:11 PM

SOLVED

I'm currently developing a CMS. I've incorporated a template system into the CMS. It is my own, not Smarty.

I was wondering how I would go about displaying the result of a multiple row resultset by using a template file. My system does load in values correctly. But I don't know how I would go about loops.

config.php
$content = array(
		'{T_ADMIN_HOME}' => $text[$config['language']]['AdminHome'],
		'{L_ADMIN_HOME}' => 'index.php?page=admin/index',
		'{KEY_HEADER}' => $text[$config['language']]['KeyHeader'],
		'{VALUE_HEADER}' => $text[$config['language']]['ValueHeader'],
	);
	
	$grabConfig = "SELECT * FROM config ORDER BY config_key ASC";
	$grabConfig = mysql_query($grabConfig);
	
	$content['{KEY}'] = array();
	$content['{KEY_VALUE}'] = array();
	$i = 0;
		
	While ($c = MySQL_fetch_array($grabConfig)){
		$i++;
		$content['{KEY}'][$i] = $c['config_key'];
		$content['{KEY_VALUE}'][$i] = $config['config_value'];
	}

config.tpl
<div id="adminContainer">
	<a href="{L_ADMIN_HOME}">{T_ADMIN_HOME}</a>
	<div id="adminBody">
		<div id="adminText">
			<table width="100%" cellspacing="1" cellpadding="1" border="0">
				<tr>
					<th>{KEY_HEADER}</th><th>{VALUE_HEADER}</th>
				</tr>
				<!-- CONFIG KEY LOOP -->
				<tr>
					<td>{KEY}</td><td>{KEY_VALUE}</td>
				</tr>
				<!-- END CONFIG KEY LOOP -->
			</table>
		</div>
	</div>	
</div>

How the values are loaded in. Where $body['Body'] is equal to the TPL file
foreach($content as $v => $c){
		$body['Body'] = str_replace($v, $c, $body['Body']);
	}

Above I make the items to be looped as an array. The only problem I'm having is how I would repeat the appropriate lines in the TPL file.

Thanks,

Edited by Lang, 10 February 2007 - 11:48 PM.


#2 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 10 February 2007 - 03:17 PM

As I mentioned in my original post, I'm looking for a way to display all rows in a resultset by using a TPL file.

In a TPL file you can't know how many results there is going to be in a query, so in the TPL file you lay out how one row should look like and the code should copy and paste that design for each row.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users