[nycphp-talk] A Bit Of Help
Brent Baisley
brent at landover.com
Thu Nov 11 12:14:24 EST 2004
I think the problem may be in the array_search function. Array search
returns the key of the item found or FALSE (or NULL in older versiosns)
if it is not found. So what happens when there is a match found at the
0 element? Since array_search returns 0, your if() statement probably
evaluates to true (!0), since 0 can also mean false. It's only first
item that is getting repeated, correct?
Using something stricter may resolve your problem, something like
testing for boolean:
if(is_bool(array_search($result['company_id'],$arr))) {
}
Or, drop the whole array search and use an associative array.
if( !is_set( $arr[$result['company_id']] ) {
...
$arr[$result['company_id']] = $result['company'];
}
Using an associative array may be quicker than array_search since you
are referencing the index of the array instead of searching the entire
array for values. The difference may not be noticeable with only a few
elements.
On Nov 11, 2004, at 11:42 AM, Joseph Crawford wrote:
> Guys,
>
> I have an sql query that uses one table to join another, the tables
> are ci2_bizopps and ci2_companies, each company could have many
> entries in the bizopps table and what i need to do is get each company
> name only once. However since we are searching the bizopps table it
> returns a result for each bizopp found, this makes for duplicate
> company names added to the array. The following code works if i
> remove the ORDER BY statement in the SQL query, however for some
> reason with it in there it breaks the code.
>
> $result = $db->query("
> SELECT
> b.id as bizopp_id, b.contract_name,
> c.id as company_id, c.name as company
> FROM ci2_bizopps b
> INNER JOIN ci2_companies c ON b.company_id=c.id
> WHERE b.contract_type=".$type."
> ORDER BY c.name ASC"
> );
> $results = $db->fetchAll($result);
>
> $data = "<table border=0 cellpadding=0 cellspacing=0 width=100%
> align='center'>\n";
> $arr = array();
> foreach($results as $result) {
> if(!array_search($result['company_id'], $arr)) {
> $data .= "<tr>\n";
> $data .= "<TD class='tabledata'><a
> href='company.php?
> id=".$result['company_id']."'>".$result['company']."</a></td>\n";
> $data .= "</tr>\n";
> $arr[] = $result['company_id'];
> }
> }
> $data .= "</table>";
> return $data;
>
> as you can see all i did was for each record found, store the company
> id into another array, and also use array_search to search the array
> to see if that company_id has already been used. If it has been used,
> do nothing otherwise display the data for that company.. I have
> printed out the value of company_id and $arr for each item and with
> the order by statement, it says the company id is 2 but 2 is also in
> the arr 3 times, so i am not sure why array_search is not finding it
> in the array and just skipping it. I guess one way of accomplishing
> this is to not use the ORDER BY statement but create yet another
> array, add the companies to that which will be displayed and then
> sorting the array and then displaying but i think the order by would
> be much easier to use if it can be done.
>
> Here are the results shown with the ORDER BY in place
> IPRO
> PRO NJ Test Data
> Romy's Widgets
> The British Medicine Group
> IPRO
> IPRO
>
> And here are the results without the ORDER BY in place
> The British Medicine Group
> Romy's Widgets
> The British Medicine Group
> IPRO
> PRO NJ Test Data
>
> This is really starting to get to me after working on this for a while
> the other day to finally get it working and today just to break it
> with an order by statement ;D
>
> Anyone that can help i would greatly appreciate it.
>
> --
> Joseph Crawford Jr.
> Codebowl Solutions
> codebowl at gmail.com
>
> For a GMail account
> contact me OFF-LIST
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
>
>
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
More information about the talk
mailing list