用php查看mysql的结果 [英] Looking though results from mysql with php

查看:35
本文介绍了用php查看mysql的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$result = mysqli_query($con,"SELECT * FROM Vehicle 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'>添加执行车辆</button></div>";$vehicles1 .="<div style='margin-bottom: 5px'></div>";

解决方案

考虑以下;

<代码>
<button id='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>添加执行车辆</button>

";$vehicles1 .= "<div style='margin-bottom: 5px'></div>";array_push($vehicles_array, $vehicles1);}?><脚本>函数 onClickAction(id, description, type_id) {$('#entered-details').show();$('#vehicle_id').val(id);$('#vehicle_list').val($('#vehicle_list').val()+id+":");Vehicle_selected.innerHTML = 描述;elt.tagsinput('add', { 'value': id , 'text': description, 'type': type_id, 'type_id': '1'});$('#show_new').show();}

我在这里做的主要事情是添加一个 foreach 循环,您可以在其中将要检查的值指定为数组.

另外,如果可能的话,您想避免使用内联 javascript,所以我取出您的内联 javascript 并将其放入它自己的函数中,我还将您的 javascript 从随机选择使用 javascript/jquery 的内容更改为仅使用 javascript.

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

我可能在这里遗漏了一些东西,因为很难说出您真正需要什么,这可能更多地属于代码审查更多比 StackOverflow

having a previous issue sorted, it has occurred to me that I am wasting a lot of time effort because I am not using while loops properly, but I am struggling with them. I have this code here which I run 4 times for type_id=1,2,3,4 and then output the results into $vehicle1, $vehicles2, ect. Is there a way to run it to check how many type_id there is and then run through them all with the same output results as below?

$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>";

解决方案

Consider the following;

<?

    $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>

The main thing I did here was add a foreach loop where you can specify as an array the values you'd like to check.

Also, you want to avoid inline javascript if possible, so I took out your inline javascript and put it in it's own function, I also changed your javascript from selecting things with javascript/jquery randomly to just using javascript.

You can access each vehicle link with the $vehicles_array array, by doing $vehicles_array[0]; or 1, 2, 3, etc - or use a foreach loop to output each line with just a few lines of code (like 3 lines probably)

I may have missed something here because it's hard to tell what you actually need really, and this probably belongs more on Code Review more than StackOverflow

这篇关于用php查看mysql的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆