Jump to content

Make loop in template system?


CrazeD

Recommended Posts

I made a simple template system. I'm in the process of making a forum-style script, that has categories within categories. I would like to call my static template file that will display all of the data. I can do this easily enough for one query, but I can't get it ordered with the category-within-category system.

 

That was probably jibberish, let me try to articulate:

 

My query is set up in this fashion:

 

$sql = mysql_query ("SELECT * FROM categories");
while ($row = mysql_fetch_array ($sql)) {
echo $row['title'].'<br />';

$sql2 = mysql_query ("SELECT * FROM categories2 WHERE cat_id='".$row['id']."'");
while ($row2 = mysql_fetch_array ($sql2)) {
	echo $row2['title'].'<br />';
}
}

 

Excuse the sloppy, that's just for an example. Now, that is how I want it to work. It displays the first category, then the sub-categories beneath, then the next main category...etc. Well, if I assign variables to these arrays and just pass them to my template, it displays, obviously, the main templates all in a list then all the sub categories under.

 

That still doesn't make any sense reading it, hopefully somebody knows what I want to do. I just need to retain the order in the above code through a static template file. Maybe by using variables to start/end loops, but I don't know how to do that.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/121080-make-loop-in-template-system/
Share on other sites

It would help if you had given more information about your template file.

 

Here is a quick example of how you could set it up...

 

Category Template:

<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td><#category_name#></td>
</tr>
<tr>
<td><b>Sub-categories:</b></td>
</tr>
<#sub_categories#>
</table>

 

Sub-category Template:

<tr>
<td><#category_name#></td>
</tr>

 

Now the PHP Code:

$sql = mysql_query ("SELECT * FROM categories");
$output = "";
while ($row = mysql_fetch_array ($sql)) {

$output .= categoryTemplate(); //a function that will have the category template file called

$output = str_replace("<#category_name#>", $row['title'], $output); //replace the <#category_name#> in the main category template file with the actual name.

$sql2 = mysql_query ("SELECT * FROM categories2 WHERE cat_id='".$row['id']."'");
$sub_categories = ""; //we start this as nothing

while ($row2 = mysql_fetch_array ($sql2)){
$sub_categories .= subCategoryTemplate(); //the subcategory template call
$sub_categories = str_replace("<#category_name#>", $row2['title'], $sub_categories);
}

if($sub_categories == ""){
$output = str_replace("<#sub_categories#>", "", $output);
}else{
$output = str_replace("<#sub_categories#>", $sub_categories, $output);
}
}

 

Just an example, hope that makes some sense.

Try native PHP.

 

template.php

<?php foreach($cats as $main_category => $sub_cats): ?>
    <h1><?php echo $main_category ?></h1>
    <ul>
    <?php foreach($sub_cats as $category): ?>
        <li><?php echo $category?></li>
    <?php endforeach; ?>
    </ul>
<?php endforeach; ?>

 

Then you just supply template.php with the data arrays, something like,

 

script.php

<?php
$cats = array();
$sql = mysql_query("SELECT * FROM categories"); 
while ($row = mysql_fetch_assoc($sql)) {
    $sql2 = mysql_query("SELECT * FROM categories2 WHERE cat_id='" . $row['id'] . "'");
    $subs = mysql_fetch_assoc($sql2);
    $cats[$row['name']] = $subs;
}

give_array_data_to_template($cats, $template_file);
?>

 

Do note that your queries can be greatly improved by using JOIN on cat_id for the second table. It will reduce the script to just one database query and organize the resulting array in a much more accesible form. I suggest that you take a look at that (JOINs) first.

 

If you do not want to use native PHP in your templates, you will need to code some sort of parsing engine for loops and control structures, which I personally don't recommend. PHP has everything you need for templates already.

 

Cheers.

I know I can parse individual template files, but is that the only way?

 

This is the static template snippet that I want to use:

 

<div id="centerCol">
<div class="blockBar">
	<div class="blockBarText">Forums</div>		
</div>
<div class="block" id="3">
	<div class="blockContent">
	  {CATEGORIES}
	</div>
</div>
<div class="blockCenterFooter">
	<div class="blockCenterFooterText"></div>
	<div class="blockCenterFooterText" style="float:right;"></div>
</div>
<div class="blockSpacer"></div>
</div>

 

That puts a center block and then puts contents in it. If I did the loop to call two template files (one for categories and one for sub categories) then I would need 4 template files. A header, the categorry & sub category, and then a footer. I want to avoid that, if possible.

 

Aeglos, the JOIN sounds promising. I'm not real great with JOIN, can you show me a small example?

 

Thanks.

I know, but Smarty is too bulky and over-complicated for what I need.

 

I'm just trying to eliminate HTML in the PHP files, so that templates can be edited from a central location rather than picking through bits and pieces.

 

EDIT: Okay, I think I'm close.

 

I didn't want to have a header, index, and footer files but it will work for now.

 

My PHP looks like this:

 

$tpl->display('header.tpl');

$sql = mysql_query ("SELECT * FROM categories");
while ($row = mysql_fetch_array ($sql)) {
$var['CATEGORIES'] = $row['cat_title'];

$sql2 = mysql_query ("SELECT * FROM subcategories WHERE cat_id='".$row['cat_id']."'");
while ($row2 = mysql_fetch_array ($sql2)) {
	$var['SUBCATEGORIES'] = $row2['subcat_title'];			
}

$tpl->display('index.tpl',$var);

}

$tpl->display('footer.tpl');

 

There are two categories, and three sub categories. It only displays two categories and two sub categories.

 

This is my template:

 

<b>{CATEGORIES}</b><br />{SUBCATEGORIES}<br />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.