/    Sign up×
Community /Pin to ProfileBookmark

problem with while loop

hi,
I am having this little problem…
I have a template and i fill it with the results i get from some sql queries. the problem is that I have some nested while loops and sql queries and when I parse the needed values I get as a result all the previous values plus the new ones. In my case I am trying to extract all the committees a user is member of and at the same time all the announcements and the members that are linked to each committee.
so here is the code….

[CODE]
<?php
session_start();
if(!session_is_registered(“authenticated”)){
header(“location:login.php”);
}

include(“include/config.inc.php”);
include(“include/functions.php”);
include(“Classes/class.FastTemplate.php3”);
$tpl=new FastTemplate(“templates”);

$tpl->define(array(“index”=>”main_tpl.tpl”,
“ver_nav_dep”=>”vertical_nav_dep.tpl”,
“ver_nav_pd”=>”vertical_nav_pd.tpl”,
“ver_nav_head”=>”vertical_nav_head_secr.tpl”,
“table_pub”=>”table_pub.tpl”,
“row_title_pub”=>”row_title_pub.tpl”,
“row_pub”=>”row_pub.tpl”,
“table_auth”=>”table_auth.tpl”,
“row_title_auth”=>”row_title_auth.tpl”,
“row_auth”=>”row_auth.tpl”,
“table_preview_announce”=>”table_preview_announce.tpl”,
“table_committee_pub”=>”table_committee_pub.tpl”,
“row_committee_pub”=>”row_committee_pub.tpl”,
“row_committee_member_pub”=>”row_committee_member_pub.tpl”,
“row_committee_announce_pub”=>”row_committee_announce_pub.tpl”,
“table_committee”=>”table_committee.tpl”,
“rows_committee”=>”rows_committe.tpl”,
“rows_committee_member”=>”rows_committe_memeber.tpl”,
“rows_committee_announce”=>”rows_committee_announce.tpl”));

$con=mysql_connect($dbServer, $dbUser, $dbPass)
or exit(“<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html;charset=windows-1253″>
<link rel=”stylesheet” type=”text/css” href=”../../styles/lh.css”>
</head>
<body>
<h2 align=center>&#931;&#966;&#940;&#955;&#956;&#945; &#954;&#945;&#964;&#940; &#964;&#951; &#963;&#973;&#957;&#948;&#949;&#963;&#951; &#956;&#949; &#964;&#951; &#914;&#940;&#963;&#951; &#916;&#949;&#948;&#959;&#956;&#941;&#957;&#969;&#957;</h2>
</body>
</html>”);

//valid assignment for insert content
$author_id=$_SESSION[“id”];

if($_SESSION[“type”]==”admin” || $_SESSION[“type”]==”pub”){
$active=1;
}
else{
$active=0;
}
if(isset($_REQUEST[“load”])){
$head_id=$_SESSION[“id”];
$tpl->assign(“{USER}”,$_SESSION[“username”]);

$page_name=”&#917;&#960;&#953;&#964;&#961;&#959;&#960;&#941;&#962;”;
$tpl->assign(“{PAGE_NAME}”,$page_name);

switch($_SESSION[“group”]){
case “Melos DEP”:
$tpl->assign(“{H_NAVIGATION}”,”);
$tpl->assign(“{TREE}”,”treemenu1″);
$tpl->parse(“{V_NAVIGATION}”,”.ver_nav_dep”);
break;
case “PD 407/80”:
$tpl->assign(“{H_NAVIGATION}”,”);
$tpl->assign(“{TREE}”,”treemenu1″);
$tpl->parse(“{V_NAVIGATION}”,”.ver_nav_pd”);
break;
}//end switch

//show only committees where professor is head or member
$sql=”SELECT DISTINCT c.id as id,
c.title_gr as title,
c.active as active
FROM committee c,
committee_members cm
WHERE c.head_id=’$head_id’
OR (c.id=cm.committee_id AND cm.member_id=’$head_id’)”;
$res=mysql_db_query($dbDatabase,$sql);

if(mysql_num_rows($res)==’0′){
$tpl->assign(“{CONTENT}”,”<center><h3>&#916;&#949;&#957; &#965;&#960;&#940;&#961;&#967;&#959;&#965;&#957; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#941;&#962;.</center></h3>”);
}

if ($res && mysql_num_rows($res)){

while($result=mysql_fetch_array($res)){
//$comm_array[]=$result[“id”];
//find committee members
$sql1=”SELECT DISTINCT p.inter_name_gr as name, c.active as active, c.memid as memid
FROM personnel p, (SELECT member_id as memid, active as active
FROM committee_members
WHERE committee_id='”.$result[“id”].”‘) c
WHERE p.id=memid”;

$res1=mysql_db_query($dbDatabase,$sql1);
if($res1 && mysql_num_rows($res1)){

while($result1=mysql_fetch_array($res1)){
$name=stripslashes($result1[“name”]);
$memid=$result1[“memid”];

$tpl->assign(“{NAME_MEMBER}”,$name);

$tpl->assign(“{DEL_MEM}”,”<a href=”committee.php?delete=member&id=”.$memid.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#948;&#953;&#945;&#947;&#961;&#945;&#966;&#964;&#949;&#943; &#964;&#959; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#959; &#956;&#941;&#955;&#959;&#962;;’,this)”><img width=”15″ height=”16″ src=”images/del2.png” alt=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” title=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” border=”0″/></a>”);
if($result1[“active”]==”0″){
$tpl->assign(“{ACTIVE_&#924;&#917;&#924;}”,”<a href=”committee.php?active=member&act=yes&id=”.$memid.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#942;&#963;&#949;&#964;&#949; &#964;o &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#959; &#956;&#941;&#955;&#959;&#962;;’,this)”><img width=”15″ height=”16″ src=”images/stop2.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}
if($result1[“active”]==”1″){
$tpl->assign(“{ACTIVE_MEM}”,”<a href=”committee.php?active=member&act=no&id=”.$memid.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#942;&#963;&#949;&#964;&#949; &#964;&#959; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#959; &#956;&#941;&#955;&#959;&#962;;’,this)”><img width=”15″ height=”16″ src=”images/apply2.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}
$tpl->parse(“{COMMITTEE_MEMBER_ROWS}”,”.rows_committee_member”);
}//end while result1
}//end if res1 true

$sql2=”SELECT DISTINCT ca.id as id, ca.title_gr as title, ca.active as active
FROM committee_announcement ca
WHERE ca.committee_id='”.$result[“id”].”‘”;

$res2=mysql_db_query($dbDatabase,$sql2);
$result2=””;
if($res2 && mysql_num_rows($res2)){

while($result2=mysql_fetch_array($res2)){
$title=stripslashes($result2[“title”]);
$id=$result2[“id”];
$active=$result2[“active”];
$tpl->assign(“{TITLE_ANN}”,$title);

$tpl->assign(“{MOD_ANN}”,”<a href=”committee.php?edit=announce&id=”.$id.””><img width=”15″ height=”16″ src=”images/edit2.png” alt=”&#913;&#955;&#955;&#945;&#947;&#942;” title=”&#913;&#955;&#955;&#945;&#947;&#942;” border=”0″/></a>”);

$tpl->assign(“{DEL_ANN}”,”<a href=”committee.php?delete=announce&id=”.$id.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#948;&#953;&#945;&#947;&#961;&#945;&#966;&#964;&#949;&#943; &#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#945;&#957;&#945;&#954;&#959;&#943;&#957;&#969;&#963;&#951;;’,this)”><img width=”15″ height=”16″ src=”images/del2.png” alt=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” title=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” border=”0″/></a>”);

if($active==”0″){
$tpl->assign(“{ACTIVE_ANN}”,”<a href=”committee.php?active=announce&act=yes&id=”.$id.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#942;&#963;&#949;&#964;&#949; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#945;&#957;&#945;&#954;&#959;&#943;&#957;&#969;&#963;&#951;;’,this)”><img width=”15″ height=”16″ src=”images/stop2.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}

if($active==”1″){
$tpl->assign(“{ACTIVE_ANN}”,”<a href=”committee.php?active=announce&act=no&id=”.$id.”” onclick=”return confirmlink(‘&#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#942;&#963;&#949;&#964;&#949; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#945;&#957;&#945;&#954;&#959;&#943;&#957;&#969;&#963;&#951;;’,this)”><img width=”15″ height=”16″ src=”images/apply2.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}
$tpl->parse(“{COMMITTEE_ANNOUNCE_ROWS}”,”.rows_committee_announce”);
}//end while result2
}//end if res2 true
else if(mysql_num_rows($res2)==’0′){
$tpl->assign(“{COMMITTEE_ANNOUNCE_ROWS}”,”<tr> <td class=”boxContentCell” colspan=”4″>&#916;&#949;&#957; &#941;&#967;&#959;&#965;&#957; &#945;&#957;&#945;&#961;&#964;&#951;&#952;&#949;&#943; &#945;&#957;&#945;&#954;&#959;&#953;&#957;&#974;&#963;&#949;&#953;&#962; &#947;&#953;&#945; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#953;&#956;&#941;&#957;&#951; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942;.</td></tr>”);
}//end else if

$tpl->assign(“{ID}”,$result[“id”]);
$tpl->assign(“{COMMITTEE_NAME}”,stripslashes($result[“title”]));

$tpl->assign(“{EDIT_COMMITTEE}”,”<a href=”committee.php?edit=committee&id=”.$result[“id”].””><img width=”15″ height=”16″ src=”images/edit3.png” alt=”&#913;&#955;&#955;&#945;&#947;&#942;” title=”&#913;&#955;&#955;&#945;&#947;&#942;” border=”0″/></a>”);

$tpl->assign(“{DELETE_COMMITTEE_FULL}”,”<a href=”committee.php?delete=committee&id=”.$result[“id”].”” onclick=”return confirmlink(‘&#916;&#953;&#945;&#947;&#961;&#940;&#966;&#959;&#957;&#964;&#945;&#962; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942; &#952;&#945; &#948;&#953;&#945;&#947;&#961;&#945;&#966;&#959;&#973;&#957; &#972;&#955;&#949;&#962; &#959;&#953; &#945;&#957;&#945;&#954;&#959;&#953;&#974;&#963;&#949;&#953;&#962; &#954;&#945;&#953; &#972;&#955;&#945; &#964;&#945; &#956;&#941;&#955;&#951; &#964;&#951;&#962; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942;&#962; &#945;&#965;&#964;&#942;&#962;. &#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#963;&#965;&#957;&#949;&#967;&#943;&#963;&#949;&#964;&#949;;’,this)”><img width=”15″ height=”16″ src=”images/del3.png” alt=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” title=”&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;” border=”0″/></a>”);

if($result[“active”]==”0″){
$tpl->assign(“{ACTION_COMMITTEE_FULL}”,”<a href=”committee.php?active=committee&act=yes&id=”.$result[“id”].”” onclick=”return confirmlink(‘&#917;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#974;&#957;&#964;&#945;&#962; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942; &#952;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#951;&#952;&#959;&#973;&#957; &#972;&#955;&#949;&#962; &#959;&#953; &#945;&#957;&#945;&#954;&#959;&#953;&#974;&#963;&#949;&#953;&#962; &#954;&#945;&#953; &#972;&#955;&#945; &#964;&#945; &#956;&#941;&#955;&#951; &#964;&#951;&#962; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942;&#962; &#945;&#965;&#964;&#942;&#962;. &#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#963;&#965;&#957;&#949;&#967;&#943;&#963;&#949;&#964;&#949;;’,this)”><img width=”15″ height=”16″ src=”images/stop3.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}
if($result[“active”]==”1″){
$tpl->assign(“{ACTION_COMMITTEE_FULL}”,”<a href=”committee.php?active=committee&act=no&id=”.$result[“id”].”” onclick=”return confirmlink(‘&#913;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#974;&#957;&#964;&#945;&#962; &#964;&#951; &#963;&#965;&#947;&#954;&#949;&#954;&#961;&#953;&#956;&#941;&#957;&#951; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942; &#952;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#953;&#951;&#952;&#959;&#973;&#957; &#972;&#955;&#949;&#962; &#959;&#953; &#945;&#957;&#945;&#954;&#959;&#953;&#974;&#963;&#949;&#953;&#962; &#954;&#945;&#953; &#972;&#955;&#945; &#964;&#945; &#956;&#941;&#955;&#951; &#964;&#951;&#962; &#949;&#960;&#953;&#964;&#961;&#959;&#960;&#942;&#962; &#945;&#965;&#964;&#942;&#962;. &#920;&#941;&#955;&#949;&#964;&#949; &#957;&#945; &#963;&#965;&#957;&#949;&#967;&#943;&#963;&#949;&#964;&#949;;’,this)”><img width=”15″ height=”16″ src=”images/apply3.png” alt=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” title=”&#922;&#940;&#957;&#964;&#949; &#954;&#955;&#953;&#954; &#947;&#953;&#945; &#945;&#960;&#949;&#957;&#949;&#961;&#947;&#959;&#960;&#959;&#943;&#951;&#963;&#951;” border=”0″/></a>”);
}
$tpl->parse(“{ROWS}”,”.rows_committee”);
}//end while $result
$tpl->parse(“{CONTENT}”,”table_committee”);
}//end if res true

$tpl->parse(“MAIN”,”index”);
$tpl->FastPrint();
}
?>
[/CODE]

what happens for example for the announcements is I get the announcements for the first committee and they appear correct, but when it comes to the announcements of the second committee i get as a result the announcements of the first plus the announcements of the second. the announcement of third committee are all announcements from both 1 and 2 plus her own…. Any suggestion what to do to fix this …
Thanx in advance
Yona

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @yona_t spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...