1

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>");
}
2
  • 1
    You should stop using Mysql_*, it's been depreciated. Check out MySqli or PDO. Commented Jan 10, 2014 at 15:36
  • In query result , records repeated. You have to check your query Commented Jan 10, 2014 at 15:38

3 Answers 3

2

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>");
Sign up to request clarification or add additional context in comments.

Comments

0

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

10 Comments

I tried to move the while loop, but now I get an error repeated 3 times:Notice: Undefined variable: row in on line 10..11..12. Do I need another while loop somewhere?
I need the results to display like this: <br> 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 $2100 25 $2000 20 Money Market Accounts Minimum Daily Balance to Earn APY Annual Percentage Yield % $3000
and those edits, sorry for the bugs... hopefully I got them all this time
Sorry, guess I'm not explaining myself very well. I'm been trying to edit my answers but the site keeps cutting me off.
For my test, the results should display NOW Checking Accounts with one row under it. The Super Now should show 3 rows. The Money Market should show one row.
|
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.