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>";
Maybe something like that could be useful to you
$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>";
}
You could also make it so you get your types from your database, with this query :
SELECT id FROM types
Then just put the result in an array and loop through it.
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
One loop and One query! Better than avoid loops is to avoid queries.
$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>";
}
}