Brian Powell
Brian Powell

Reputation: 3411

Add multiple rows to a single php while loop variable

I currently have this code that's working just fine:

while ($row = mysqli_fetch_array($popupResult)) {
  $popupQuote     = $row['quoteNumber'];
  $popupExp       = $row['quoteCustomer'];
  $popupCost      = $row['quoteExpirationDate'];
  }

My only issue is that the data in my table that contains what I want in $popupQuote COULD have quoteNumber populated, or it could have debitNumber populated - but it will never have both. I need this basically:

  $popupQuote     = $row['quoteNumber'] or $row['debitNumber']

If I use $popupQuote = $row['quoteNumber'];, I get 6 results. If I use $popupQuote = $row['debitNumber']; I can 4 results. I want the output to display all 10 results in a single variable.

Upvotes: 0

Views: 109

Answers (3)

Marc B
Marc B

Reputation: 360732

If you guarantee that one of the two fields will always be populated, and never will there be a record with both or neither, then:

$poupQuote = !empty($row['quoteNumber']) ? $row['quoteNumber'] : $row['debitNumber'];

e.g. check if quotenumber has a value. if it does, use that value, otherwise use the debitnumber. Of course, you'll probably want some better checking, since empty() will bite you in the rump if quotenumber can be 0 - empty(0) is true.

Upvotes: 3

Demodave
Demodave

Reputation: 6642

What you need is a Ternary operator

And test if there is a value

$popupQuote = ($row['quoteNumber'] != "") ? $row['quoteNumber'] : $row['debitNumber'];

Upvotes: 0

Barmar
Barmar

Reputation: 781310

Use a conditional expression:

$popupQuote = $row['quoteNumber'] ? $row['quoteNumber'] : $row['debitNumber'];

Upvotes: 0

Related Questions