Craig van Tonder
Craig van Tonder

Reputation: 7687

Php/Mysql while loop - Div is NOT repeating for each possible result. Only the last result is shown? (have some code)

I am having issues with a while loop that draws data out of a mysql database. Essentially, a user searches a string, the string is then compared against of possible names in the db. If a string matches a result from the db, then the row is returned and the information relating to the result is dislayed in its' own div.

Here is the, somewhat faulty code relating to the while loop:

    (LOTS OF CODE RELATING TO EVERYTHING ELSE BEFORE THIS)
    $tempVar = 0;
while ($data = mysql_fetch_array($data)) {
    $lstId = $data['id'];
    $lstName = $data['name'];
    $lstDesc = $data['description'];
    $lstAddress = $data['address'];
    $lstArea = $data['area'];
    $lstProvince = $data['province'];
    $lstPostcode = $data['postcode'];
    $lstTel = $data['tel'];
    $lstCell = $data['cell'];
    $lstFax = $data['fax'];
    $lstEmail = $data['email'];

if ($tempVar == 0) {
    $pageContent .= '
        <div class="searchResultS">
    ';
    }
    $pageContent .= '
    <div class="searchResult">
    <h2>'.$lstName.'</h2>
        <div class="bizDesc">
            <p>Description: '.$lstDesc.'</p>
        </div>
        <div class="bizAddr">
            <p>Street: '.$lstAddress.'</p>
            <p>Town: '.$lstArea.'</p>
            <p>Province: '.$lstProvince.'</p>
            <p>Post Code: '.$lstPostcode.'</p>
        </div>
        <div class="bizCont">
            <p>Tel: '.$lstTel.'</p>
            <p>Cell: '.$lstCell.'</p>
            <p>Fax: '.$lstFax.'</p>
            <p>eMail: '.$lstEmail.'</p>
        </div>
    </div>
';
  $tempVar ++;
 }

if ($anymatches > 0 and count($tempVar) == count($anymatches)) {
        $pageContent .= '
    <!-- end .searchResultS --></div>
';
}

The problem that I am having, is that when a user searches a string with two or more possible results. It seems as though only the last result is displayed. And not all of the possible results, each in their own containing div. I know for a fact that there are more results, as there is a line generated that displayed the number of mysql rows returned.

Would anyone have a suggestion, or solution for this problem? I assume that something has been left out, but after many hours of trial and error testing, i have come up with no solid reason as to why this behavior currently exists.

Any help what so ever would be greatly appreciated, thank you!

Upvotes: 1

Views: 1154

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270775

Looks like you are overwriting the result resource $data to an array on the first loop iteration. After this iteration, $data can no longer be accessed as a result resource, so it will appear as though you've had only one result.

while ($data = mysql_fetch_array($data)) {

// Instead use a new variable:
while ($row = mysql_fetch_array($data)) {
//-----^^^^

// Then change all of these
$lstId = $row['id'];
$lstName = $row['name'];
$lstDesc = $row['description'];
...
...
$lstEmail = $row['email'];

You should turn on display_errors in development, as I would expect you to have received this warning after the first loop iteration:

Warning: mysql_fetch_array() expects parameter 1 to be resource...

Upvotes: 3

Related Questions