I have an array like below and that array refers to another data on my db,
Array
(
[0] => 4
[1] => 1
[2] => 15
[3] => 1
[4] => 1
)
how do I want to select value no 1, and get the most top value from table that has value=1
?
I use in_array(1,$array)
but it does not output anything. Below are my coding;
$sql = " Select * FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' ORDER BY table_name DESC ";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
$leave_id= array();
while($row_permohonan=mysqli_fetch_assoc($result_permohonan))
{
$leave_available = $row_permohonan['leave'];
$leave_id[] = $row_permohonan['leave_id'];
} mysqli_free_result($result_permohonan);
//echo '<pre>'; print_r($leave_id); echo '</pre>';
if(in_array(1,$leave_id)){
echo $leave_available .'/'.$row['total_leave'];
}
I think you have missed $result
.
$sql = " Select * FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' ORDER BY table_name DESC ";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
$leave_id= array();
while($row_permohonan=mysqli_fetch_assoc($result)) //Here
{
$leave_available = $row_permohonan['leave'];
$leave_id[] = $row_permohonan['leave_id'];
} mysqli_free_result($result); //And here
First, your code is not clear. You are using:
echo $leave_available.'/'.$row['total_leave'];
Where "$row" is not set anywhere into your code. If you have put on top of your code:
ini_set("display_errors", "On"); error_reporting(E_ALL);
PHP would break with a fatal error if you are executing the code exactly as you gave it. But I assume that you set it somewhere else into your code.
2nd: You should return the data you want into your MySQL query. If you just want to know if there is at least 1 row into your database having leave_id=1 and staff_id=$_SESSION['staff_id'], then add this condition in your SQL query:
$sql = "Select COUNT(*) AS nbrows FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' AND leave_id=1 ORDER BY column_name DESC ";
And then: you have to use "ORDER BY" statement with a column name and not the table name.
Using "in_array": as you are matching "1", I would recommend you to add "true" as 3rd parameter of this function (force using "strict comparison" in case sensitive and type matching):
in_array(1, $array, true)
Then, you'll also have to cast your $leave_id result:
$leave_id[] = (int)...
(int) cast the string value to integer (as PHP results from database are almost strings)