1

Hope I explain this correctly.

I have in my code

$eng[] = "All";
$eng[] = "engine_Sigma";
$eng[] = "engine_K-Series";
$eng[] = "engine_Duratec";
$eng[] = "engine_Suzuki";
$eng[] = "engine_Vauxhall";
$eng[] = "engine_Crossflow";

// query your db
$itemsq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-items` LIMIT 0,100");
$items = mysqli_fetch_assoc($itemsq);
// do/while? should prob just use a while loop
do {
    $enginetype = explode(":", $items['engineid']);

    foreach ($enginetype as $key) {
        echo "$items[wsiid] - $items[code] - $items[incvat] - $eng[$key] <br>
";
    }

} while ($items = mysqli_fetch_assoc($itemsq)); 

However in the DB the engineid's are stored as 1:3:6 for example or even 1:2:3:4:5:6 now what I want to do is if there are multiple values and they have been split up, instead of it just echoing the first item and then echoing the data again for the next id (like this WSIID - CODE INCVAT engine_K-Series ) and so on I want to do display ( WSIID - CODE - INCVAT - engine_Sigma engine_K-Series ) if $eng is 1 & 3 ( 1:3)

Hope you can understand what I mean

Thanks guys

1 Answer 1

1

You should echo the first part of the data outside of your foreach loop, and echo only the engine type inside the foreach :

<?php
$eng[] = "All";
$eng[] = "engine_Sigma";
$eng[] = "engine_K-Series";
$eng[] = "engine_Duratec";
$eng[] = "engine_Suzuki";
$eng[] = "engine_Vauxhall";
$eng[] = "engine_Crossflow";

// query your db
$itemsq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-items` LIMIT 0,100");
//$items = mysqli_fetch_assoc($itemsq); # you should delete this, see my explanation below
// do/while? should prob just use a while loop
do {
    $enginetype = explode(":", $items['engineid']);

    echo "$items[wsiid] - $items[code] - $items[incvat] - ";
    foreach ($enginetype as $key) {
       echo "$eng[$key] ";
    }
    echo "<br />";

} while ($items = mysqli_fetch_assoc($itemsq)); 

You should delete your first execution of $items = mysqli_fetch_assoc($itemsq);, or your do...while loop will start at the 2nd iteration.

Sign up to request clarification or add additional context in comments.

3 Comments

Try a while instead of a do... while, the former executes the loop after checking the condition, whereas the latter executes the loop, then checks the condition. Hence, if there's no result from your query, do...while will still execute the loop once.
just changed to while ($items = mysqli_fetch_assoc($itemsq)) { $enginetype = explode(":", $items['engineid']); echo "$items[wsiid] - $items[code] - $items[incvat] - "; foreach ($enginetype as $key) { echo "$eng[$key] "; } echo "<br />"; } ; Works great now ! Thanks roberto !
how can i assign all relevant values from $eng[$key] to a variable eg $test so that all values will be displayed when I echo $test ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.