Check whether array contains selected value in a mysql query with PHP

Asked

Viewed 100 times

0

I wonder if you could help me?
I need to check if array contains a specific value in a mysql query.

In a function I get the day value of the week:

$wekc = 4

In table tb_turg, I own the weekg column that holds the values like this:

idturg weekg
1      1,3,5
2      1,2,4
3      0,4
4      3,6
5      4,6
6      1,2
7      1,4,6
8      0,1,2,3,
9      2,4
10     3,5

Then I created the array like this:

$sqlturg = "SELECT weekg,abng,classg FROM tb_turg WHERE abng != '$nwdatefer' AND classg = '1' ORDER BY idturg ASC";
$querya = $conn->query($sqlturg);
while($rowtg = $querya->fetch_assoc()) {
$weekg = $rowtg["weekg"];

@$nwekga .= $weekg." - ";
}

Then I separated this array:

$nwekg = rtrim($nwekga, ' - ');
$aw = explode(' - ', $nwekg);
foreach($aw as $bw) {
$cw = $bw;

$sqlga = mysqli_query($conn, "SELECT idturg,weekg FROM tb_turg WHERE weekg = '$cw' AND classg = '1' ORDER BY idmodg ASC");
$rowtga = $sqlga->fetch_assoc();
$idturg = $rowtga["idturg"];
$ag = $rowtga["weekg"];

$fw = explode(',', $ag);
foreach($fw as $gw) {
$hw = $gw;
}

if (in_array($wekc, $fw)) { 

$sqlgb = mysqli_query($conn, "SELECT idturg,codturg FROM tb_turg WHERE idturg = '$idturg' AND classg = '1'");
$rowtgb = $sqlgb->fetch_assoc();
$idturg = $rowtgb["idturg"];
$codturg = $rowtgb["codturg"];

echo $idturg . "<br>";
} else {
echo "";
}

}

But it is not working because it is repeating results of the $sqlgb query
CONCLUSION: He would have to list the idturg only of the lines that have the day 4 (Thursday), without repeating.
Thank you very much!

1 answer

0


RESOLVED:
Just do it like this: FIND_IN_SET($wekc,weekg)

$sqlturg = "SELECT idturg FROM tb_turg WHERE FIND_IN_SET($wekc,weekg) AND classg = '1'"

And I would only need one consultation... Aff
I’m sorry, it’s just that I’m still learning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.