mrtwidget
mrtwidget

Reputation: 83

PHP switch inside a while loop

I am getting two of the same entry from my mysql databaseusing a PHP switch. I have isolated the problem to here, perhaps someone can indulge me? $mySQL->Fetch() just gets the next row in a query.

Here's what I'm working with:

while($row = $mySQL->Fetch())
{
switch($filter)
{
    case 1:
    case 2:
        $data[] = array(150, $row["lastName"]);
        $data[] = array(125, $row["firstName"]);
        $data[] = array(100, $row["city"]);
        $data[] = array(75, $row["state"]);
        $data[] = array(100, FormatPhone($row["phone"]));
        $data[] = array(125, $row["registrationDate"]);
        $data[] = array(125, $row["expirationDate"]);
        $id = $row["id"];

        echo "<a href=\"?dp=profile&filter=member&id=$id\"><ul class=\"search\">";
        foreach($data as $key => $value)
        {
             echo "<li style=\"width:$value[0]px\">$value[1]</li>";
        }
        echo "</ul></a>";
        break;
}
}

For some reason the list keep building. I must have overlooked something obvious but I just can't track it down.

Here is what the HTML is outputting:

<ul class="search">
    <li style="width:150px">Jones</li>
    <li style="width:125px">Bob</li>
    <li style="width:100px">City</li>
    <li style="width:75px">NH</li>
    <li style="width:100px">(123) 456-7890</li>
    <li style="width:125px">2013-01-20</li>
    <li style="width:125px">2014-07-31</li>
    <li style="width:150px">Doe</li>
    <li style="width:125px">Jane</li>
    <li style="width:100px">Little Town</li>
    <li style="width:75px">NH</li>
    <li style="width:100px">(123) 456-7890</li>
    <li style="width:125px">2013-01-20</li>
    <li style="width:125px">2014-07-31</li>
</ul>

Instead of having one it combines two. But I made sure to close my unordered list tag.. Not to mention, each entry after this continues to grow in size, combining all the entries so far. Do I need to purge the query each time?? Thanks

Upvotes: 1

Views: 3244

Answers (3)

strager
strager

Reputation: 90052

Replace:

$data[] = array(150, $row["lastName"]);
$data[] = array(125, $row["firstName"]);
$data[] = array(100, $row["city"]);
$data[] = array(75, $row["state"]);
$data[] = array(100, FormatPhone($row["phone"]));
$data[] = array(125, $row["registrationDate"]);
$data[] = array(125, $row["expirationDate"]);

with:

$data = array(
    array(150, $row["lastName"]);
    array(125, $row["firstName"]),
    array(100, $row["city"]),
    array(75, $row["state"]),
    array(100, FormatPhone($row["phone"])),
    array(125, $row["registrationDate"]),
    array(125, $row["expirationDate"]),
);  

You are mutating the single $data array every iteration, instead of creating a new array every iteration.

Upvotes: 0

Kyle
Kyle

Reputation: 341

I believe your answer lies in the dynamic nature of PHP variable scoping. Notice how you didn't have to declare $data before using it? PHP assumes you wanted a variable called data, and inferred that you wanted an array because you appended to it using []. Upon each iteration of the while loop, you append the row results to $data as you should, but you never told PHP that you wanted a 'new' $data variable. I suggest declaring $data explicitly as below:

while ($row = $mySQL->Fetch()) {
    $data = array();
    switch ($filter) {
        // case statements

This will re-initialize $data to an empty array at the start of each iteration, assuring that you're only creating list items for the current row.

Upvotes: 1

Sujit Singh
Sujit Singh

Reputation: 921

What happening is after adding 1st time in first iteration, for next iteration, it is again putting next elements in $data and as you are using like $data[]= $next_element, previous data is not getting removed from $data array, so after every iteration, unset the array. This should work.

Upvotes: 1

Related Questions