2017-07-20 12 views
0

で整理してみましたが、whileループを正しく使用していないため、多くの時間を浪費していますが、私は苦労しています。私はここでtype_id = 1,2,3,4に対して4回実行し、結果を$ vehicle1、$ veh2、ectに出力します。いくつかのtype_idが存在するかどうかを確認して実行する方法はありますか?mysqlの結果をPHPの

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

このコードは現在の状態で機能しますか? – GrumpyCrouton

+0

はいはいうまく動作しますが、前に行ったことから、私はループについて少し学ぶ必要があると思います。 – zak

+0

ここでは既にループを使用していますが、コードを見直して改善できるかどうか確認しています。 – GrumpyCrouton

答えて

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 

結果を配列に入れ、ループします。

+0

これは良いスタートです!ループ内で出力をエコーし​​ない場合は、 'vehicles'の配列を使いたいでしょう。 '$ vehicles1'の代わりに' $ vehicles [$ i] 'のように – BizzyBob

+0

ありがとう、試みますが、$ vehicles1はループするときに$ vehicles2,3,4などに変更する必要がありますか? – zak

+0

笑、私はそれを送ったように私はその応答を得た!私はそれを試していただきありがとうございます – zak

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を使用してランダムジャバスクリプト/ jQueryを使って物事を選択するからあなたのjavascriptを変更し、JavaScriptとそれ自身の機能でそれを置くあなたのインラインを取り出しました。

あなたは$vehicles_array[0];または1、2、3、などを行うことによって、$vehicles_array配列と各車両のリンクにアクセスすることができます - (おそらく3行と同じように)

をわずか数行のコードで、各ラインのか、出力にforeachループを使用します

それはあなたが実際に本当に必要とし、これはおそらくStackOverflowの

0

つのループとワンクエリよりCode Reviewより多くの詳細を属するものを言うのは難しいので、私はここで何かを見逃している可能性があります!ループを避けるよりもクエリを避けるほうがよい。

$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>"; 
    } 
} 
+0

ありがとうございますが、何も出力しません! – zak

+0

Ein ... echo $ str_vehiclesを使用します。終了コード – MikeSouto

+0

で私はすでに何もしなかった! – zak