Jump to content


Photo

while loop table, read discount


  • Please log in to reply
3 replies to this topic

#1 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 September 2006 - 06:11 PM

I have a table with items that i want to match up with the price discount.
Currently i have the discounts stored in a separate table, but as the amount of users grows i cannot add one table with discount for each customer. Thats why i must find another solution.

illustrating the items table:
--------------------------------
item_id | discount_code | etc....
--------------------------------
1        | 5600              |
--------------------------------

and the discount goes like
-------------------------
discount_code | discount
-------------------------
5600              | 20
-------------------------

I am thinking about writing discount codes and discount values to a discount file (.txt or whatever) for each customer and read this to match each item upon request. I am however concerned about how to do this the right way so its still very fast to find/read while looping the items table and showing results.

Any ideas? Just making a file like:

5600;20
5601;15

etc...

or is it possible to save a file as an two dimentional array like

array(5600 => 20, 5601 => 15 ......);

...havent tested to see if it works though...


WHATS FASTEST to retrieve datas ???



#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 06 September 2006 - 06:17 PM

SELECT i.*,d.* FROM items AS i LEFT JOIN discounts AS d ON i.discount_code=d.discount_code;

This will select the items with the discount. Then use mysql_fetch_assoc() in a while loop to get run through all the items.

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 September 2006 - 06:45 PM

thanx, but i'm seeking the fastest way to retrieve a match from a file - not a table, already have that  :)

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 September 2006 - 08:08 PM

ok - i've tested and found this so far:

<?php

$dir_path = "/www/www.what.net/dir/dir/dir";
$file = $dir_path . "/disc.txt";
$dfile = file_get_contents($file);

$disklines = explode("\n", $dfile);
$arr = array();

foreach ($disklines as $line)
{
$line = explode(";", $line);
$disc_code = $line[0];
$disc = $line[1];
$arr[$disc_code] = $disc;

//echo "<p r e>";
//print_r($arr);
//echo "</ p r e>";
}

// $query = mysql_query(" blah... ");
// while($row = mysql_fetch_array($query)
{
$discount_code = $row["discount_code"];

if (array_key_exists($discount_code, $arr))
{
$disc = $arr[$disc_code];
}
}

?>


seems fast and ok... ?
any improvements out there ?? (...always seem to be...)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users