简体   繁体   English

PHP循环无法正确显示

[英]PHP loop not displaying properly

I need some help with a looping problem. 我需要一些有关循环问题的帮助。 For my example code below, I have two Mysql tables: 对于下面的示例代码,我有两个Mysql表:

tblDepRatesCats:

ID 
header 
left_text 
center_text 
right_text 
header_order

------

tblRates_balance:

id 
depratecat 
MinBalance 
InterestRate 
APY 
suborder

The tblDepRatesCats.ID = tblRatesBalance.depratecat . tblDepRatesCats.ID = tblRatesBalance.depratecat For each row in tblDepRatesCats , there may be 0 or 1 or more rows in tblRates_balance . 对于每一行tblDepRatesCats ,有可能在0或1或更多行tblRates_balance

I'm trying to display the results of querying these tables so that it shows each tblDepRatesCats data with the corresponding tblRates_balance data, but instead it is showing tblDepRatesCats so that if tblDepRatesCats row has 3 tblDepRatesCats rows associted with it, the tblDepRatesCats row is repeated 3 times with one row of tblDepRatesCats ' 我正在尝试显示查询这些表的结果,以使其显示每个tblDepRatesCats数据以及相应的tblRates_balance数据,但相反,它显示的是tblDepRatesCats以便如果tblDepRatesCats行具有tblDepRatesCats 3个tblDepRatesCats行,则tblDepRatesCats行被重复一行tblDepRatesCats '

As you can see below "Super Now Checking Account" is displayed 3 times, but what I want is for it to display just once with the 3 results for minimum balance and apy listed under the one header. 如您所见,“超级现在的支票帐户”显示3次,但我希望它仅显示一次,并在一个标题下列出3个最小余额和apy结果。

NOW Checking Accounts Minimum Daily Balance to Earn APY Annual Percentage Yield $1000 10 现在支票账户的最低每日余额以赚取APY年利率1000美元10

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2222 2 Super NOW支票账户每天赚取APY的最低日余额百分比,%2222美元2

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2100 25 Super NOW Checking Account获得APY的最低每日余额的年百分比收益率%$ 2100 25

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2000 20 Super NOW Checking Account获得APY的每日最低最低年利率百分比$ 2000 20

Money Market Accounts Minimum Daily Balance to Earn APY Annual Percentage Yield % $3000 30 货币市场账户赚取APY的最低每日余额年利率百分比$ 3000 30

Below is my test code. 下面是我的测试代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

$result = mysql_query('SELECT tblDepRatesCats.*, tblRates_balance.* FROM tblDepRatesCats JOIN tblRates_balance ON tblDepRatesCats.ID = tblRates_balance.depratecat ORDER BY tblDepRatesCats.header_order, tblRates_balance.suborder;');

while ($row = mysql_fetch_assoc($result)) 
{
 echo ("<table width=\"98%\" border=\"0\" bgcolor:\"#ffffff\"><tr><td>");
 echo ("" . $row["header"] . " <br>");
 echo ("" . $row["left_text"] . "&nbsp;&nbsp;");
 echo ("" . $row["right_text"] . "");
 echo ("</tr></td>");
 // &nbsp temporarily added
 echo ("<tr><td>");
 echo ("" . $row["MinBalance"] . "");
 echo ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
 echo ("" . $row["InterestRate"] . " <br><br>");
 echo ("</tr></td>");
}

Just move the output you don't want to be repeated each time to outside of the while loop... 只需将您不想每次重复的输出移到while循环之外...

echo ("<table width=\"98%\" border=\"0\" bgcolor:\"#ffffff\"><tr><td>");
echo ("" . $row["header"] . " <br>");
echo ("" . $row["left_text"] . "&nbsp;&nbsp;");
echo ("" . $row["right_text"] . "");
echo ("</tr></td>");
echo ("<tr><td>");
while ($row = mysql_fetch_assoc($result)) 
{
    //&nbsp temporarily added
    echo ("" . $row["MinBalance"] . "");
    echo ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    echo ("" . $row["InterestRate"] . " <br><br>");
}
echo ("</tr></td>");

If I am understanding you right, then all you need to do is shift around your code: 如果我理解正确的话,那么您要做的就是转移代码:

$result = mysql_query('SELECT tblDepRatesCats.*, tblRates_balance.* FROM tblDepRatesCats JOIN tblRates_balance ON tblDepRatesCats.ID = tblRates_balance.depratecat ORDER BY tblDepRatesCats.header_order, tblRates_balance.suborder;');
$new_result = array();
while ($row = mysql_fetch_assoc($result)) 
{
  $new_result[$row["header"]][] = $row;
}

foreach($new_result as $new_row){
  echo ("<table width=\"98%\" border=\"0\" bgcolor:\"#ffffff\"><tr><td>");
  echo ("" . $new_row[0]["header"] . " <br>");
  echo ("" . $new_row[0]["left_text"] . "&nbsp;&nbsp;");
  echo ("" . $new_row[0]["right_text"] . "");
  echo ("</tr></td>");

  foreach($new_row as $row){
   // &nbsp temporarily added
   echo ("<tr><td>");
   echo ("" . $row["MinBalance"] . "");
   echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
   echo ("" . $row["InterestRate"] . " <br><br>");
   echo ("</tr></td>");
  }

  //also, dont forget to close the table:
  echo "</table>";
}

The way your code was was starting a new table everytime you went through your loop 每次循环时,代码创建新表的方式

edit to avoid warning. 编辑以避免警告。 Though it might make more sense to find a way to not need to have to call $result[0]["xxxxx"] at all. 尽管找到一种根本不必调用$result[0]["xxxxx"]的方法可能更有意义。 Maybe a static variable would be easier, though a second query to get just those values might be what you are looking for. 也许静态变量会更容易,尽管您可能正在寻找第二个查询来获取这些值。 It just depends 这取决于

Use left join if there can be 0 links between tables. 如果表之间的链接可以为0,请使用左连接。 Using join only will result rows that have no data in balance table to be dropped out from results. 仅使用联接将导致余额表中没有数据的行从结果中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM