Variable won't repeat in repeat region

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cpb
Forum Newbie
Posts: 2
Joined: Wed Apr 01, 2009 8:10 am

Variable won't repeat in repeat region

Post by cpb »

Hello all,
I am a PHP beginner and having a problem getting a variable to assign itself to it's individual row in a repeat region:

Here's my code:

mysql_select_db($database_edr_con, $edr_con);
$query_rs_teams = "SELECT * FROM info ORDER BY win DESC";
$rs_teams = mysql_query($query_rs_teams, $edr_con) or die(mysql_error());
$row_rs_teams = mysql_fetch_assoc($rs_teams);
$totalRows_rs_teams = mysql_num_rows($rs_teams);
$pct = $row_rs_teams['win']/($row_rs_teams['win']+$row_rs_teams['loss']);

When I place $pct in my repeat region - it correctly does the math, but then just repeats the answer for the 1st row in the table, rather than giving a different answer for each row.

Here's my code as it is placed in the repeat region:

<td width="164" class="stand_row_left"><?php echo $row_rs_teams['team_name']; ?></td>
<td width="30" class="stand_row"><?php echo $row_rs_teams['win']; ?></td>
<td width="30" class="stand_row"><?php echo $row_rs_teams['loss']; ?></td>
<td width="60" class="stand_row"><?php echo $pct = number_format($pct,3); ?></td>

Can anyone tell me what I am doing wrong here in not getting my variable to identify with each individual row? All of the other values in my repeat region are working properly.

Any help is GREATLY appreciated!
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: Variable won't repeat in repeat region

Post by becky-atlanta »

This is something that I did.

Code: Select all

<!-- InstanceBeginEditable name="EditRegion7" -->
            
<?php
//make a MySQL connection
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$query = "SELECT * FROM entrees ORDER BY item";  
$result = mysql_query($query) or die(mysql_error());
 
while($row = mysql_fetch_array($result)){
echo "<tr>\n";
echo "<td width=\"350\" height=\"100\" class=\"subtitle-red\">".$row['Item']."<br>\n";
echo "<div class=\"bodyleft\">".$row['Description']."</div></td>\n";
echo "<td width=\"100\" height=\"100\" class=\"bodyright\">Calories<br>Carbohydrate<br>Fiber<br>Protein<br>Fat<br></td>\n";
echo "<td width=\"40\" height=\"100\" class=\"bodyright\">".$row['Calories']."<br>".$row['Carbohydrate']."<br>".$row['Fiber']."<br>".$row['Protein']."<br>".$row['Fat']."</td>\n";
echo "</tr>\n";
}
?>
<!-- InstanceEndEditable -->
The while function will process each record and output to the screen according to your format. I hope this is helpful.
cpb
Forum Newbie
Posts: 2
Joined: Wed Apr 01, 2009 8:10 am

Re: Variable won't repeat in repeat region

Post by cpb »

Thanks Becky - but I think I should clarify a little more:

- I'm not having any problem formatting the output - I'm using number_format to take care of that.
- My problem is with my variable "$pct" - I am using it to get a percentage based on 2 column values in each row of my table.
(i.e. - column 1 = wins, column 2 = losses...so $pct is adding "wins+losses" then dividing by "wins" to get a "win percentage"

So, when I place the variable "$pct" in a Repeat Region - I am expecting it to produce a "win percentage" for each row in my table, based on the values in each row's "win" and "loss" column.

What's happening is when I output my Repeat Region - I get the same "win percentage" for every row (it shows the win percentage that is correct for the 1st row over and over)

Here's how it looks:

Team W L pct
Team 1 20 1 0.952
Team 2 19 3 0.952
Team 3 17 5 0.952
Team 4 13 9 0.952

and so on....

Any other suggestions on why my variable $pct is not outputting properly?
Post Reply