TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-27-2009, 02:51 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default How to search array

I want to code a buy one get one free option on my site. Initially, I wrote it so that half of the number of products were free, based on the lowest prices. So, if three products were $200 each and three were $100 each, the three $100 ones would be free. But my customers quickly pointed out they were not really getting one free. It should have been one $200 and two $100 for free. That's where I am at now. I can't figure out how to determine which ones should be free. Here's some pseudo code showing what I am doing.

PHP Code:
function IsFree($id) {
Array
(
    [
0] => Array
        (
            [
id] => 1961
            
[price] => 129.0000
            
[model] => 20
        
)

    [
1] => Array
        (
            [
id] => 1962
            
[price] => 179.0000
            
[model] => 24
        
)

    [
2] => Array
        (
            [
id] => 7047
            
[price] => 229.0000
            
[model] => 30
        
)

    [
3] => Array
        (
            [
id] => 19650
            
[price] => 299.0000
            
[model] => 30
        
)

    [
4] => Array
        (
            [
id] => 1989
            
[price] => 279.0000
            
[model] => 36
        
)

    [
5] => Array
        (
            [
id] => 23112
            
[price] => 339.0000
            
[model] => 36
        
)

    [
6] => Array
        (
            [
id] => 22399
            
[price] => 409.0000
            
[model] => 40
        
)
)
  return 
true or false;
}

for (
$i 0$i count($products); ++$i {
  if (
IsFree($products[$i]['id']))
   echo 
'Mark as free';

The IsFree function reads through the products and creates a multi array sorted on the model and then the price. I just showed the output of that array to, hopefully, make it clearer. If I wanted to just return the half of the products that have the lowest prices, I could just take that many from the beginning of the array. But I need to check whether there are duplicate model sizes listed. If there are, then the lowest price of that pair gets returned instead. I think part of the problem might be that the array is being created each time and the product being checked is a whole new search. In other words, there's no history of what has already been found. I thought of adding another array to track those but that made an already confusing situation worse.

Does anyone have any suggestions on how to tackle this problem?
benton is offline  
Reply With Quote
Old 03-27-2009, 08:27 AM   #2 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

You mean something like this:
PHP Code:
<?php

$data 
= array();
$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20);
$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24);
$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30);
$data[] = array('id'=>19650,'price'=>299.0000,'model'=>30);
$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36);
$data[] = array('id'=>23112,'price'=>339.0000,'model'=>36);
$data[] = array('id'=>22399,'price'=>409.0000,'model'=>40);



function 
isFree($input
{
    
$output = array();
    foreach(
$input as $row)
    {
        if(!
array_key_exists($row['model'],$output) || array_key_exists($row['model'],$output) && $output[$row['model']]['price'] < $row['price'] )
        {
            
$output[$row['model']] = $row;
        }
    }

    return 
$output;


echo 
"Current selected items: \n".print_r($data,true)."\n";
echo 
"New selected items:\n".print_r(isFree($data),true)."\n";
outputs:
Code:
Current selected items: 
Array
(
    [0] => Array
        (
            [id] => 1961
            [price] => 129
            [model] => 20
        )

    [1] => Array
        (
            [id] => 1962
            [price] => 179
            [model] => 24
        )

    [2] => Array
        (
            [id] => 7047
            [price] => 229
            [model] => 30
        )

    [3] => Array
        (
            [id] => 19650
            [price] => 299
            [model] => 30
        )

    [4] => Array
        (
            [id] => 1989
            [price] => 279
            [model] => 36
        )

    [5] => Array
        (
            [id] => 23112
            [price] => 339
            [model] => 36
        )

    [6] => Array
        (
            [id] => 22399
            [price] => 409
            [model] => 40
        )

)

New selected items:
Array
(
    [20] => Array
        (
            [id] => 1961
            [price] => 129
            [model] => 20
        )

    [24] => Array
        (
            [id] => 1962
            [price] => 179
            [model] => 24
        )

    [30] => Array
        (
            [id] => 19650
            [price] => 299
            [model] => 30
        )

    [36] => Array
        (
            [id] => 23112
            [price] => 339
            [model] => 36
        )

    [40] => Array
        (
            [id] => 22399
            [price] => 409
            [model] => 40
        )

)
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 03-28-2009, 12:09 AM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thank you for the code but that doesn't output the result I am looking for. I didn't think about it when I originally posted but I probably should have listed what the output should be, for this example, so I will do that now. Given the following array, there are seven entries, which means there should be three marked as free, since it's buy one get one.
PHP Code:
$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20); 
$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24); 
$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30); 
$data[] = array('id'=>19650,'price'=>299.0000,'model'=>30); 
$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36); 
$data[] = array('id'=>23112,'price'=>339.0000,'model'=>36); 
$data[] = array('id'=>22399,'price'=>409.0000,'model'=>40); 
The three would be:

This one since it is the lowest price and not part of a pair (where a pair refers to items with the same model numbers)
PHP Code:
$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20); 
This one since it is part of a pair and is the lowest of that pair
PHP Code:
$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30); 
This one since it is part of a pair and is the lowest of that pair
PHP Code:
$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36); 
So even though this one
PHP Code:
$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24); 
is lower in price than the last two above it, they take precedence since they are part of a pair.
benton is offline  
Reply With Quote
Old 03-29-2009, 12:31 AM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

I just wanted to say that I figured this out. I ended up creating a master array before looping through the products. If a product is found, I then delete it from the master array and count the number of free products found. I have to iterate through the array more times that is probably necessay but it works. Thanks for trying to help.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
How do I code this array benton General 3 10-22-2008 02:17 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
(array) or array()? Orc General 4 01-20-2008 07:52 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 05:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design