Steve Angelis Posted February 25, 2008 Share Posted February 25, 2008 I have created a template system for my website, and I am trying to entrgrate a database loop into it but I am not sure exactly how to pull it off. I have three files for the template. The file called template.php which puts it all together, this is all fine and dandy there, the .tpl file with is just stricktly the html code, and the .php file which processes everything. Here are the last two files: The PHP file <?PHP require('inc/config.php'); $linkid = @mysql_connect("$db_host", "$db_uname", "$db_pass"); mysql_select_db("$db_name", $linkid); $dpt_id=$_GET['id']; $queryd = "SELECT * FROM departments where id='$dpt_id'"; $resultd = mysql_query($queryd) or die("$query does not make any sence;<br>" . mysql_error()); $contentd=mysql_fetch_array($resultd); // include the template $currenttemplate='neraxian'; // instantiate a new template Parser object $tp=&new templateParser('templates/'.$currenttemplate.'/dpt.tpl'); // define parameters for the class $tags=array('dpt_leader'=>$contentd['dpt_leader'], 'dpt_desc'=>$contentd['dpt_desc'], 'dpt_mbr2'=>$dpt_mbr2, 'dpt_members'=>$contentd['dpt_members']); // parse template file $tp->parseTemplate($tags); // display generated page echo $tp->display(); $resultiddm = mysql_query("select * from departments_mbr where dptm_dptid='$dpt_id'", $linkid); if (mysql_numrows($resultiddm)>0) { for($n=0;$n<mysql_numrows($resultiddm);$n++) { $contentdm = mysql_fetch_array($resultiddm); $mbr_id2 = $contentdm['dptm_userid']; $resultiddm2 = mysql_query("select id, mbr_name from mbr where id='$mbr_id2'", $linkid); $contentdm2 = mysql_fetch_array($resultiddm2); $dpt_mbr2 = echo "<a href=\"index2.php?page=member&mid=".$contentdm2['id']."\">".$contentdm2['mbr_name']."</a><br>"; } } ?> This is the loop and I do not know where to stick it in so it will work: $resultiddm = mysql_query("select * from departments_mbr where dptm_dptid='$dpt_id'", $linkid); if (mysql_numrows($resultiddm)>0) { for($n=0;$n<mysql_numrows($resultiddm);$n++) { $contentdm = mysql_fetch_array($resultiddm); $mbr_id2 = $contentdm['dptm_userid']; $resultiddm2 = mysql_query("select id, mbr_name from mbr where id='$mbr_id2'", $linkid); $contentdm2 = mysql_fetch_array($resultiddm2); $dpt_mbr2 = echo "<a href=\"index2.php?page=member&mid=".$contentdm2['id']."\">".$contentdm2['mbr_name']."</a><br>"; } } Here is the tpl file: <table width="100%" border="0"> <tr> <td width="200" align="left" valign="top"><u><strong><?PHP echo $contentd['dpt_name'];?></strong></u></td> </tr> <tr> <td align="left" valign="top"><table width="100%" border="0"> <tr> <td width="200" align="left" valign="top"><u><span class="title1">Department Leader</span></u></td> <td align="left" valign="top"><span class="body">{dpt_leader}</span></td> </tr> <tr> <td align="left" valign="top"> </td> <td align="left" valign="top"><span class="body">{dpt_desc}</span></td> </tr> <tr> <td align="left" valign="top"><u><span class="title2">Department Members</span></u></td> <td align="left" valign="top"><span class="body">{dpt_mbr2}</span></td> </tr> </table></td> </tr> </table> Now I know the code on its own each part and all works, so getting info or what not all works, it's just putting it together. Some how the loop must go where it is defined as dpt_mbr2 but I am not sure how to do it exactly. Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/ Share on other sites More sharing options...
Bauer418 Posted February 25, 2008 Share Posted February 25, 2008 Enable a foreach/looping feature in your template or build a string in your loop. For each iteration, you could add whatever HTML data you need to a string variable, and then assign a template variable to that value. Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/#findComment-475527 Share on other sites More sharing options...
Steve Angelis Posted February 25, 2008 Author Share Posted February 25, 2008 Well that is what I have, I have the loop writen already, it is the second one. I should probably specify it more. The loop is the second bit of code. The variables from the tpl file are listed here in the first file: tags=array('dpt_leader'=>$contentd['dpt_leader'], 'dpt_desc'=>$contentd['dpt_desc'], 'dpt_mbr2'=>$dpt_mbr2, 'dpt_members'=>$contentd['dpt_members']); Some how I need to toss the loop into there somehow... or get $dpt_mbr2 to work with the loop... but I am not sure how to get it to work right. I tried get the $dtp_mbr2 to simply echo the loop code that shows it all, but it did not work. Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/#findComment-475528 Share on other sites More sharing options...
Steve Angelis Posted February 25, 2008 Author Share Posted February 25, 2008 I have tried this but it only shows one record: <?PHP require('inc/config.php'); $linkid = @mysql_connect("$db_host", "$db_uname", "$db_pass"); mysql_select_db("$db_name", $linkid); //get the template theme $query = "SELECT * FROM general"; $resultxx = mysql_query($query) or die("$query does not make any sence;<br>" . mysql_error()); $contentxx=mysql_fetch_array($resultxx); $currenttemplate=$contentxx['gen_theme']; //end template them $dpt_id=$_GET['id']; $queryd = "SELECT * FROM departments where id='$dpt_id'"; $resultd = mysql_query($queryd) or die("$query does not make any sence;<br>" . mysql_error()); $contentd=mysql_fetch_array($resultd); // instantiate a new template Parser object $tp=&new templateParser('templates/'.$currenttemplate.'/dpt.tpl'); $resultiddm = mysql_query("select * from departments_mbr where dptm_dptid='$dpt_id'", $linkid); if (mysql_numrows($resultiddm)>0) { for($n=0;$n<mysql_numrows($resultiddm);$n++) { $contentdm = mysql_fetch_array($resultiddm); $mbr_id2 = $contentdm['dptm_userid']; $resultiddm2 = mysql_query("select id, mbr_name from mbr where id='$mbr_id2'", $linkid); $contentdm2 = mysql_fetch_array($resultiddm2); $dpt_mbr2 = "<a href=\"index2.php?page=member&mid=".$contentdm2['id']."\">".$contentdm2['mbr_name']."</a><br>"; } } // define parameters for the class $tags=array('dpt_leader'=>$contentd['dpt_leader'], 'dpt_desc'=>$contentd['dpt_desc'], 'dpt_mbr2'=>$dpt_mbr2, 'dpt_members'=>$contentd['dpt_members']); // parse template file $tp->parseTemplate($tags); // display generated page echo $tp->display(); ?> Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/#findComment-475584 Share on other sites More sharing options...
Bauer418 Posted February 25, 2008 Share Posted February 25, 2008 Change this section: if (mysql_numrows($resultiddm)>0) { for($n=0;$n<mysql_numrows($resultiddm);$n++) { $contentdm = mysql_fetch_array($resultiddm); $mbr_id2 = $contentdm['dptm_userid']; $resultiddm2 = mysql_query("select id, mbr_name from mbr where id='$mbr_id2'", $linkid); $contentdm2 = mysql_fetch_array($resultiddm2); $dpt_mbr2 = "<a href=\"index2.php?page=member&mid=".$contentdm2['id']."\">".$contentdm2['mbr_name']."</a><br>"; } } To this: $dpt_mbr2 = ''; while ($contentdm = mysql_fetch_assoc($resultiddm)) { $mbr_id2 = $contentdm['dptm_userid']; $resultiddm2 = mysql_query("select id, mbr_name from mbr where id='$mbr_id2'", $linkid); $contentdm2 = mysql_fetch_array($resultiddm2); $dpt_mbr2 .= "<a href=\"index2.php?page=member&mid=".$contentdm2['id']."\">".$contentdm2['mbr_name']."</a><br>"; } You were not looping your SQL results correctly, or concatenating strings properly. Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/#findComment-475839 Share on other sites More sharing options...
Steve Angelis Posted February 26, 2008 Author Share Posted February 26, 2008 That works thanx Link to comment https://forums.phpfreaks.com/topic/92827-templates-and-loops/#findComment-476555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.