简体   繁体   中英

PHP loop not displaying properly

I need some help with a looping problem. For my example code below, I have two Mysql tables:

tblDepRatesCats:

ID 
header 
left_text 
center_text 
right_text 
header_order

------

tblRates_balance:

id 
depratecat 
MinBalance 
InterestRate 
APY 
suborder

The tblDepRatesCats.ID = tblRatesBalance.depratecat . For each row in tblDepRatesCats , there may be 0 or 1 or more rows in 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 '

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.

NOW Checking Accounts Minimum Daily Balance to Earn APY Annual Percentage Yield $1000 10

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2222 2

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2100 25

Super NOW Checking Account Minimum Daily Balance to Earn APY Annual Percentage Yield % $2000 20

Money Market Accounts Minimum Daily Balance to Earn APY Annual Percentage Yield % $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...

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. 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. Using join only will result rows that have no data in balance table to be dropped out from results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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