Jump to content

[SOLVED] in_array unexpected problem


simon551

Recommended Posts

$contractId="37";

if  (in_array($contractId, $contractAdminArray)) { $admin='yes'; } else { $admin='no';}


echo $admin;

$contractAdminArrayString= implode(',',$contractAdminArray);

echo $contractAdminArrayString;

 

result

no72,21,62,16,37,6,86,104,49,29,100,104

 

clearly 37 is in the array but the script says it's not. what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/
Share on other sites

Only thing that I can think of is maybe because they're off different data types, but even that shouldn't cause a problem.

 

do this, right before the in_array() and post what it outputs:

 

print_r($contractAdminArray);

Oh, then it's clear why it isn't working.. in_array() checks all the elements of the array. You only have one element that contains a string..

 

You should explode the string in that element by ',' then check the array.

 

Or use stripos() to check the string.

oh. maybe there's a problem in how I'm creating the array.

This is the code:

$empid = $_COOKIE['empid'.$companyName];
$div = $_COOKIE['divisionId'];

//contract manager
mysql_select_db($database_conn_org, $conn_org);
$qManager = "SELECT DISTINCT contract_teamdets.contractId as x FROM contract_teamdets  WHERE ((contract_teamdets.teamRoleID = 1 OR contract_teamdets.teamRoleId=4) AND contract_teamdets.empID = $empid) ";
$rsManager = mysql_query($qManager, $conn_org) or die(mysql_error());
$total_contracts_admin=mysql_num_rows($rsManager);
////start array my contracts//////////////////////////////////////////////////////
$myContractsArray=array();
while ($row_rsManager = mysql_fetch_assoc($rsManager))
{
array_push ($myContractsArray, $row_rsManager['x']);
}
//ownership items
$qOwner = "SELECT DISTINCT contracts.contractId as x FROM  contracts Inner Join tblclients ON contracts.clientId = tblclients.clientId  WHERE  tblclients.empID_relationshipHolder=$empid ";
$rsOwner = mysql_query($qOwner, $conn_org) or die(mysql_error());
$total_contracts_admin=mysql_num_rows($rsOwner);
///continue array
while ($row_rsOwner = mysql_fetch_assoc($rsOwner))
{
array_push ($myContractsArray, $row_rsOwner['x']);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
$myContractsArrayString= implode(',',$myContractsArray);


$contractAdminArray=array();

//add any personal contracts to the new array before building up further.
array_push ($contractAdminArray, $myContractsArrayString);


if 	($_SESSION['divisionAdmin']=='yes'){
$query_rsDivisionContracts = "SELECT contracts.contractId as x, contracts.divisionId FROM contracts WHERE contracts.divisionId = $div ";
$rsDivisionContracts = mysql_query($query_rsDivisionContracts, $conn_org) or die(mysql_error());
//continue building array
while ($row_rsDivisionContracts = mysql_fetch_assoc($rsDivisionContracts))
{
array_push ($contractAdminArray, $row_rsDivisionContracts['x']);
}
}
//continue building array
if 	(($_SESSION['globalAdmin']=='yes')||($_SESSION['HRAdmin']=='yes')){ 
$query_rsGlobalContracts = "SELECT DISTINCT contracts.contractId as x FROM contracts";
$rsGlobalContracts = mysql_query($query_rsGlobalContracts, $conn_org) or die(mysql_error());

while 
($row_rsGlobalContracts = mysql_fetch_assoc($rsGlobalContracts))
{
array_push ($contractAdminArray, $row_rsGlobalContracts['x']);
}
}

I have build one array that has a specific purpose and then I want to use that array in another array. I should find a better way to do that.

Can you please post your full source? It's hard to figure out the full problem when I'm partially blind-sighted.

 

This doesn't look right though:

 

$myContractsArrayString= implode(',',$myContractsArray);


$contractAdminArray=array();

//add any personal contracts to the new array before building up further.
array_push ($contractAdminArray, $myContractsArrayString);

Yeah. I was creating a string and then dumping it in the array. that was creating an array with a string in it rather than adding each string element to the array which I wanted to do. I fixed it.

Thanks for the help!

<?php session_start();
require_once('../Connections/conn_org.php'); 
require_once('../security/adminTest.php'); 
$empid = $_COOKIE['empid'.$companyName];
$div = $_COOKIE['divisionId'];

//contract manager
mysql_select_db($database_conn_org, $conn_org);
$qManager = "SELECT DISTINCT contract_teamdets.contractId as x FROM contract_teamdets  WHERE ((contract_teamdets.teamRoleID = 1 OR contract_teamdets.teamRoleId=4) AND contract_teamdets.empID = $empid) ";
$rsManager = mysql_query($qManager, $conn_org) or die(mysql_error());
$total_contracts_admin=mysql_num_rows($rsManager);
////start array my contracts//////////////////////////////////////////////////////
$myContractsArray=array();
$contractAdminArray=array();
while ($row_rsManager = mysql_fetch_assoc($rsManager))
{
array_push ($myContractsArray, $row_rsManager['x']);
array_push ($contractAdminArray, $row_rsManager['x']);
}
//ownership items
$qOwner = "SELECT DISTINCT contracts.contractId as x FROM  contracts Inner Join tblclients ON contracts.clientId = tblclients.clientId  WHERE  tblclients.empID_relationshipHolder=$empid ";
$rsOwner = mysql_query($qOwner, $conn_org) or die(mysql_error());
$total_contracts_admin=mysql_num_rows($rsOwner);
///continue array
while ($row_rsOwner = mysql_fetch_assoc($rsOwner))
{
array_push ($myContractsArray, $row_rsOwner['x']);
array_push ($contractAdminArray, $row_rsOwner['x']);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
$myContractsArrayString= implode(',',$myContractsArray);



//add any personal contracts to the new array before building up further.
//array_push ($contractAdminArray, $myContractsArrayString);


if 	($_SESSION['divisionAdmin']=='yes'){
$query_rsDivisionContracts = "SELECT contracts.contractId as x, contracts.divisionId FROM contracts WHERE contracts.divisionId = $div ";
$rsDivisionContracts = mysql_query($query_rsDivisionContracts, $conn_org) or die(mysql_error());
//continue building array
while ($row_rsDivisionContracts = mysql_fetch_assoc($rsDivisionContracts))
{
array_push ($contractAdminArray, $row_rsDivisionContracts['x']);
}
}
//continue building array
if 	(($_SESSION['globalAdmin']=='yes')||($_SESSION['HRAdmin']=='yes')){ 
$query_rsGlobalContracts = "SELECT DISTINCT contracts.contractId as x FROM contracts";
$rsGlobalContracts = mysql_query($query_rsGlobalContracts, $conn_org) or die(mysql_error());

while 
($row_rsGlobalContracts = mysql_fetch_assoc($rsGlobalContracts))
{
array_push ($contractAdminArray, $row_rsGlobalContracts['x']);
}
}
//array is complete
///turn it into a string to be used in a query
$contractAdminArrayString= implode(',',$contractAdminArray);
//$result=count($contractAdminArray);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.