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
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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.