开发者

php and mysql function snowballing results

Im using the following function.

function GetSubmissions($coach){
     $result = mysql_query("SELECT * FROM `ptable` WHERE coach = '$_SESSION[username]'") or trigger_error(mysql_error()); 
     while($row = mysql_fetch_array($result)){ 
     foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
      $id = $row['id'];
      $teampre = $row['team'];
      $eventpre = $row['event'];
      $statuspre = $row['status'];
      $eventarray = DecodeEvent($eventpre);
       $event = $eventarray[0];
       $cat = $eventarray[1];
       $subcat = $eventarray[2];
       $division = $eventarray[3];
       $type = $eventarray[4];
      $teamarray = DecodeTeam($teampre);
        foreach ($teamarray AS $key => $value){
         $teamgo .= $value[1]." ".$value[2]."<br/>";
       } 
      $push .= "<div id=submission><div id=event>$event<开发者_JS百科/div><div id=status>$statuspre</div><div id=subinfo>$cat $subcat $division $type</div><div id=team>$teamgo</div></div>";
     }   

  return $push;
 }

What is happening, is that the $teampre contains a series of numbers representing the team members, which i pass to the DecodeTeam function to return the names of the given members in an array. The problem is. That say there are 3 results for the main query. The first is fine. The second result starts off with the team members from the first result. The third result starts off with the team members from the first and second queries, like a snowball effect.

I figure the problem is in the way im handling the $teamgo variable, but im not sure how to get the results to stop snowballing like this.


Looks like you just need to reset the $teamgo string each time you go through the loop:

$teamarray = DecodeTeam($teampre);
$teamgo = ""; // Reset so it doesn't contain the results from the last row.
foreach ($teamarray AS $key => $value){
   $teamgo .= $value[1]." ".$value[2]."<br/>";
} 


I was beat to the bunch but I beautified your code =)

function GetSubmissions($coach) {

  $result = mysql_query("SELECT * FROM `ptable` WHERE coach = '$_SESSION[username]'");

  while ($row = mysql_fetch_array($result)) { 

    // stripslashes
    foreach($row AS $key => $value) { 
      $row[$key] = stripslashes($value); 
    } 

    $id         = $row['id'];
    $teampre    = $row['team'];
    $eventpre   = $row['event'];
    $statuspre  = $row['status'];

    $eventarray = DecodeEvent($eventpre);
    $event      = $eventarray[0];
    $cat        = $eventarray[1];
    $subcat     = $eventarray[2];
    $division   = $eventarray[3];
    $type       = $eventarray[4];

    $teamarray  = DecodeTeam($teampre);
    $teamgo     = '';
    foreach ($teamarray AS $key => $value) {
      $teamgo .= $value[1]." ".$value[2]."<br/>";
    } 

    $push .= "blah blah blah";

  }   

  return $push;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜