2017-10-06 73 views
1

在數據庫表中我已ENGINEID 引擎ID是0行 - 第6和如果存儲多個引擎它們被存儲爲1:3:5等爆炸分貝數據和呼應結果瓦爾

我有下面的代碼,我試圖查詢數據庫,並讓它拉結果。那麼我想它來查詢ENGINEID列,在爆炸吧:然後每個值賦給另一個變量(見下面的代碼)

$itemsq = mysqli_query($con,"SELECT * FROM `crm`.`service-items` WHERE `itemgroup` = 'Service' LIMIT 0,10"); 
$items = mysqli_fetch_assoc($itemsq); 
do { 
$enginetype = explode(":",$items); 
$eng0 = "engine_Sigma engine_K-Series engine_Duratec engine_Suzuki engine_Vauxhall engine_Crossflow"; 
$eng1 = "engine_Sigma"; 
$eng2 = "engine_K-Series"; 
$eng3 = "engine_Duratec"; 
$eng4 = "engine_Suzuki"; 
$eng5 = "engine_Vauxhall"; 
$eng6 = "engine_Crossflow"; 

$enginetype[0] = $eng0; 
$enginetype[1] = $eng1; 
$enginetype[2] = $eng2; 
$enginetype[3] = $eng3; 
$enginetype[4] = $eng4; 
$enginetype[5] = $eng5; 
$enginetype[6] = $eng6; 

echo "$items[code] - ".$enginetype; 

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

希望是有道理的給你們。 請不要問我爲什麼使用:以單獨的數據,它不是我的數據庫,是這樣的,當我開始在這裏

由於工作提前

+1

能不能請你告訴我們你的數據庫表的一個例子嗎? –

回答

0

定義你的引擎在一個數組,而不是單個變量,然後從結果中挑出引擎中的引擎。

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

$items = '1:3:5'; 
$enginetype = explode(":", $items); 

/* 
engine_Sigma 
engine_Duratec 
engine_Vauxhall 
*/ 
foreach ($enginetype as $key) { 
    echo $eng[$key].PHP_EOL; 
} 

https://3v4l.org/utBNG

你的代碼是這樣:

<?php 
// define engines - add a numeric key if you need it 
$eng[] = "engine_Sigma engine_K-Series engine_Duratec engine_Suzuki engine_Vauxhall engine_Crossflow"; 
$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`.`service-items` WHERE `itemgroup` = 'Service' LIMIT 0,10"); 

// do/while? should prob just use a while loop 
do { 
    $enginetype = explode(":", $items); 

    foreach ($enginetype as $key) { 
     echo "\$eng[$key] - ".$eng[$key].PHP_EOL; 
    } 

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

只是測試答案很快就會回覆,非常感謝 –

+0

也不會在循環中定義'$ eng',因爲它不起作用。 –

+0

正是我想要的!非常感謝:) –