通过php

时间:2017-07-20 18:22:58

标签: php mysql

先前的问题排序了,我发现我浪费了很多时间,因为我没有正确地使用while循环,但我正在努力解决它们。我在这里有这个代码,我为type_id = 1,2,3,4运行了4次,然后将结果输出到$ vehicle1,$ vehicles2,ect。有没有办法运行它来检查有多少type_id,然后使用相同的输出结果运行它们?

$result = mysqli_query($con,"SELECT * FROM vehicles WHERE type_id=1 AND verified='$yes' ORDER BY description");
while($row = mysqli_fetch_array($result)) {
$description = $row['description'];
$description = strtoupper($description);

$id = $row['id'];
$count++;
$vehicles1 .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description'   , 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
}
$vehicles1 .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
$vehicles1 .="<div style='margin-bottom: 5px'></div>";

3 个答案:

答案 0 :(得分:1)

考虑以下事项;

<?

    $vehicles_array = array();

    $types = array(1,2,3,4);
    foreach($types as $value) {
        $result = mysqli_query($con, "SELECT * FROM vehicles WHERE `type_id`='{$value}' AND `verified`='{$yes}' ORDER BY description");
        while ($row = mysqli_fetch_array($result)) {
            $description = strtoupper($row['description']);
            $id          = $row['id'];
            $vehicles1 .= <<<STRING
                <a href="" onclick="onClickAction('{$id}', '{$description}', '{$type_id}')" class="btn new2 dodgerbluemenu">{$description}</a>
STRING;
        }
        $vehicles1 .= " <div>
                            <button id='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button>
                        </div>";
        $vehicles1 .= "<div style='margin-bottom: 5px'></div>";
        array_push($vehicles_array, $vehicles1);
    }
?>

<script>
    function onClickAction(id, description, type_id) {
        $('#entered-details').show(); 
        $('#vehicle_id').val(id);
        $('#vehicle_list').val($('#vehicle_list').val()+id+":");
        vehicle_selected.innerHTML = description;
        elt.tagsinput('add', { 'value': id , 'text': description, 'type': type_id, 'type_id': '1'});
        $('#show_new').show();
    }
</script>

我在这里做的主要是添加一个foreach循环,您可以在其中指定您想要检查的值。

另外,如果可能的话,你想要避免使用内联javascript,所以我拿出你的内联javascript并把它放在它自己的函数中,我也改变你的javascript从javascript / jquery随机选择到只使用javascript

您可以通过$vehicles_array或1,2,3等访问$vehicles_array[0];数组的每个车辆链接 - 或者使用foreach循环仅使用几行代码输出每一行(可能是3行)

我可能在这里遗漏了一些东西,因为很难说出你真正需要的东西,而且这可能比Code Review更多地属于StackOverflow

答案 1 :(得分:0)

也许这样的事情对你有用

        $my_types = [1,2,3,4];
        foreach($my_types as $type){
            $result = mysqli_query($con,"SELECT * FROM vehicles WHERE type_id=$type AND verified='$yes' ORDER BY description");
            while($row = mysqli_fetch_array($result)) {
                $description = $row['description'];
                $description = strtoupper($description);

                $id = $row['id'];
                $count++;
                $vehicles1 .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description'   , 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
            }
            $vehicles1 .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
            $vehicles1 .="<div style='margin-bottom: 5px'></div>";
        }

您也可以使用此查询从数据库中获取类型:

SELECT id FROM types

然后将结果放入数组并循环遍历它。

答案 2 :(得分:0)

一个循环和一个查询!优于避免循环是为了避免查询。

$result = mysqli_query( $con, "SELECT * FROM vehicles WHERE verified='$yes' ORDER BY type_id ASC, description;"); // remove type_id here and add order

if( mysqli_num_rows( $result ) > 0 ) // Evaluate always, the notified too amount the execution time, display_errror=On in DEVEPOLMENT ENVIRONMENT
{
    $str_vehicles = "";
    $current_type_id = -1;
    $count = 0;

    while( $row = mysqli_fetch_array( $result ) )
    {
        if( $current_type_id != $row['type_id'] )
        {
            if( $current_type_id > -1 )
            {
                $str_vehicles .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
                $str_vehicles .="<div style='margin-bottom: 5px'></div>";
            }
            $current_type_id = $row['type_id'];
        }
        $id = $row['id'];
        $description = strtoupper( $row['description'] );
        $count++;
        $str_vehicles .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description', 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
    }
}