开发者

Dropdown list strangely shown out of the table

I'm having some issues right now with some weird behaviour of my code and can't find the why.

I have a table of extensions (phone) which are assigned to a qeue. I get the data by SQL query and then show it in the screen. One of the data shown is Priority which I want to be changed with jquery+ajax via choosing a new one of a dropdown list.

So, I managed this code:

echo '<table cellpadding="5" cellspacing="0" width="30%" id="deletion" bgcolor="#f6f6f6" style="border:1px solid #cccccc;">
        <tr>
            <th><strong>Extension</strong></th>
            <th><strong>Priority</strong></th>
            <th>&nbsp;</th>
        </tr>';
        while ($datos_extension = mysql_fetch_array($result)){
                echo '<tr id="'.$extension_data['id'].'"><td>'.$extension_data['extension'].'</td><td>'.show_priority($datos_extension['priority']).'</td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr>';
            }
        echo "</table>";

Now, the function:

function show_priority ($priority) {
    $max_priority = 3;
    $cont = 1;

    echo '<select name="priority" class="priority">';

    while ($cont <= $max_priority) {
        echo '<option';
        if ($cont == $priority)
            echo " selected";
        echo ' value="'.$cont.'">'.$cont.'</option>';
        $cont ++;
    }
    echo "</select>";
}

But it shows the Dropdowns out of the table... Here is the HTML output:

<tr>
            <th><strong>Extension</strong></th>
            <th><strong>Priority</strong></th>

            <th>&nbsp;</th>
        </tr><select name=开发者_StackOverflow"priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="2"><td>1002</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="3"><td>1003</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="4"><td>1004</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="5"><td>1005</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="6"><td>1006</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="7"><td>1007</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="8"><td>1008</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="9"><td>1009</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option value="1">1</option><option selected value="2">2</option><option value="3">3</option></select><tr id="10"><td>9003</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr></table>

Do someone knows where it could be the problem?


I think it's because of nested echo statements. So before PHP caches first echo it displays second one, because it's inside. I think that solution is to build string inside of show_priority function and just return it, like:


function show_priority ($priority) {
    $max_priority = 3;
    $cont = 1;

    $return = '<select name="priority" class="priority">';

    while ($cont <= $max_priority) {
        $return .= '<option';
        if ($cont == $priority)
            $return .= " selected=selected"; //this should be done like this in proper HTML
        $return .= ' value="'.$cont.'">'.$cont.'</option>';
        $cont ++;
    }
    $return .= "</select>";

    return $return;
}

I haven't tested it, but it I believe it'll work.


It's because all code for echo gets executed before outputting the final sting. The function echoes the list before the parent echo populates the table row. You need to change your function like this to be able to insert it into other strings:

function show_priority ($priority) {
    $max_priority = 3;
    $cont = 1;
    $output = '';

    $output .= '<select name="priority" class="priority">';

    while ($cont <= $max_priority) {
        $output .= '<option';
        if ($cont == $priority)
            $output .= " selected";
        $output .= ' value="'.$cont.'">'.$cont.'</option>';
        $cont ++;
    }
    $output .= "</select>";
    return $output;
}

But after all it's always better to return the list as an array or an object, and then populate it separately after you generated all contents; separate database logic from design.

Also if for some reason you can't alter the function you could use output buffering:

    while ($datos_extension = mysql_fetch_array($result)){
            ob_start();
            show_priority($datos_extension['priority']);
            echo '<tr id="'.$extension_data['id'].'"><td>'.$extension_data['extension'].'</td><td>'.ob_get_clean().'</td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr>';
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜