Database (book table)
serialID price
0001 10.00
0001 30.00
0002 15.00
0004(A) 9.00
0004(B) 5.00
0005 3.00
(Noted: 0003 no record)Code
$serialID = array("0001","0002","0003","0004","0005"); //DB Connection for($i = 0; $i < count($serialID); $i++) { $q = "select * from book where serial like \"$serialID[$i]%\" limit 1"; $r = mysqli_query($dbc,$q); while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $serial[$i] = $row['serial']; $price[$i] = $row['price']; echo $serial[$i].' '.$price[$i]; } } //pass db value into array for($j = 0; $j < count($serialID); $j++) { $data[$j] = array($serialID[$j],$price[$j]); }This time my questions is how to skip the serialID 0003 value?
My expected output: (echo $serial[$i].' '.$price[$i])
0001 10.00
0002 15.00
0003
0004(A) 9.00
0005 3.00
-
3Just remove it from your array of codes to search forHanky Panky– Hanky Panky2015-01-13 07:19:23 +00:00Commented Jan 13, 2015 at 7:19
-
4is this an IQ test ?Raptor– Raptor2015-01-13 07:19:48 +00:00Commented Jan 13, 2015 at 7:19
-
@Raptor Did anybody pass?u_mulder– u_mulder2015-01-13 07:20:40 +00:00Commented Jan 13, 2015 at 7:20
-
compare with the array and your table recordsVamshi .goli– Vamshi .goli2015-01-13 07:21:11 +00:00Commented Jan 13, 2015 at 7:21
-
possible duplicate of PHP array mysql retrieve each recorduser1864610– user18646102015-01-13 07:40:02 +00:00Commented Jan 13, 2015 at 7:40
Add a comment
|
4 Answers
Modify last loop to
for($j = 0; $j < count($serialID); $j++)
{
if(null != $price[$j]){
$data[$j] = array($serialID[$j],$price[$j]);
}
}
But my question is, it's not sucide to query something in loop? Maybe you should use "IN" statement? Or "OR"? For e.g:
foreach($serialId as $id){
$string.= ' %'.$serialId. ' % OR '; //of course you should check if this is first or last cell in array. So you don’t add unnecessary OR or space
}
select * from book where serial like \"$string\" limit 1
1 Comment
Michael Kuan
I have already try in statement. But not exactly get the value that I want.
Just check it in the for loop. Use the code below
$serialID = array("0001","0002","0003","0004","0005");
//DB Connection
for($i = 0; $i < count($serialID); $i++)
{
$q = "select * from book where serial like \"$serialID[$i]%\" limit 1";
$r = mysqli_query($dbc,$q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$serial[$i] = $row['serial'];
if($serialID=="0003"){
$price[$i] = $row['price'];
}
echo $serial[$i].' '.$price[$i];
}
}
//pass db value into array
for($j = 0; $j < count($serialID); $j++)
{
$data[$j] = array($serialID[$j],$price[$j]);
}
Hope this helps you
2 Comments
Shaiful Islam
I don't think this will help OP
Utkarsh Dixit
@Shaiful Islam Updated my answer
I think this code should work better for your needs:
$serialID = array("0001", "0002", "0003", "0004", "0005");
//DB Connection
foreach ($serialID as $serialItem) {
$q = "select * from book where serial like \"$serialItem%\" limit 1";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if (count($row) > 0) {
echo $row['serial'] . ' ' . $row['price'];
//pass db value into array
$data[] = array(
'serial' => $row['serial'],
'price' => $row['price']
);
}
}
}
// debug output
print_r($data);
Comments
$serialID = array("0001","0002","0003", "0004","0005");
$preparing = [];
foreach($serialID as $value) {
$preparing[] = "'" . $value . "'";
}
$sql = "select * from book where serial IN (" . implode(',', $preparing) . ")";
$query = mysqli_query($sql);
$data = mysqli_fetch_all($query, MYSQLI_ASSOC);