Maihes Posted February 8, 2020 Share Posted February 8, 2020 <?php $val1 = "10"; $val2 = "20"; $val3 = "30"; $prod = "2"; $array1=array("1" => $val1, "2"=> $val2, "3" => $val3); if (in_array($prod,$array1)) { foreach ($array1 as $result) { $bla = $result; echo $bla; } } else { $bla = 0; echo $bla; } ?> ok so i have this kind of array or at least i need something like this but this returns 0 so if i could get help with this i would really appreciate and i hope i posted this in the right place. Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/ Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 in_array() checks if a value exists in an array. You are looking for a key. if (isset($array1[$prod]) ) Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574092 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 well if i change in_array part to what you gave me i get as result back : 102030 which is obviously not what i need, but thanks for reply anyway . Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574100 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 Your code was if $prod is in the array loop through the array and output each value endif As your values are 10, 20 and 30 then the output of "102030" is doing exactly what you asked it to do. Why don't you tell us what you need - we aren't psychics. Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574103 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 ok sorry about that .. i wrote a new code which is working fine with a small issue <?php $array = array( $TSprice => 1, $ESSprice => 2, $ASBBprice => 3, $MSBBprice => 4, $KAprice => 5, $CFSprice => 6, $BKprice => 7, $ODPprice => 8, $GTDPprice => 9, $GSDPprice => 10, $SLGHprice => 11, $RCBprice => 12, $RCCBprice => 13, $CCBprice => 14, $STORprice => 15, $CPCprice => 16, $PPPCprice => 17, $PPCprice => 18, $APCprice => 19, $MDFCprice => 20, $SDFCprice => 21, $MTDFCprice => 22, $DSFCprice => 23 ); while ($testvalue = current($array)) { if ($testvalue == $prodval) { $proprice = key($array); } next($array); } ?> ok so this works fine the only problem is that if some of the prices are the same then my result won't show up for all prices just for one. for example as you can see the variables for prices .. each correspond to 1 product which this array is giving me once i select a product from the drop down list. but if for example $TSprice = 10 and $ESSprice = 10 .. when i select TS from drop down $proprice gives me the right value (10) but then if i select ESS it doesnt give me any value only if i change the price for ESS so it doesnt match any of the other prices. So i need to make sure that it gives me the price even if other product has the same price Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574117 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 Array keys need to be unique. Build your array the other way round $array = [ 1 => $TSPrice, 2 => $ESSPrice, etc ]; the it doesn't matter if two prices are the same. Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574119 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 and how do i do that i mean: while ($testvalue = current($array)) { if ($testvalue == $prodval) { $proprice = key($array); } next($array); } this part ... because i'm kinda noob with arrays coz if i simply just change the other way around its not working since i think $prodval looks in the value and not key Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574121 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 We still don't know what you are trying to do. All you've told us is how you are attempting to do something. Where are the prices stored Why do need to hard code them into an array What does the dropdown look like - you mentioned selecting a product. (Does that give you the 1, 2, or 3 or the price) Are you trying to find the price for a product or trying to find all products that have a specific price. Give us a clue and we can help. Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574122 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 So i have a dropdown with a list of products $prodval is the variable that once i select a product from the list returns the products ID(which is populated from DB) prices are stored in db too but each have an assigned variable as you could see in the array, so im trying to make the array that once i select a product from the list i get a variable which is = to the selected products price because based on the product's price i need to display certain info on the pace once the product is selected so i need the price for some of information there Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574123 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 (edited) 20 minutes ago, Maihes said: prices are stored in db too but each have an assigned variable That is not what you should be doing. You should either create a prices array direct from the DB where the keys are the product ids and values are the prices, or When user selects product id, query the DB to get the price for the product If your data looks like +---------------+----------+ | product_id | price | +---------------+----------+ | 1 | 10.00 | | 2 | 12.50 | | 3 | 10.00 | +---------------+----------+ get the data and store in aray $res = $db->query("SELECT product_id, price FROM product"); foreach ($res as $rec) { $prices[ $rec['product_id'] ] = $rec['price']; } then the array will be $prices = [ 1 => 10.00, 2 => 12.50, 3 => 10.00 ]; and to get the price $selected_id = 2; $price = $prices[$selected_id]; //--> 12.50 Edited February 8, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574124 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 well the problem is that my prices DB looks like this: ID,prod1,prod2,prod3,etc, 1 ,10 ,20 ,30 ,etc, and is stored in a table , but then my products list which is stored in a different table looks like: ID,Name 1,prod1 2,prod2 3,prod3 etc so it's a bit difficult to make it the way you said(i would have to change my db and parts of the codes i have in the project ... the array that i posted earlier works fine i just need to invert it like you said and look for the key and display the value instead of looking at value and display key Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574125 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 (edited) Even with your terrible (polite description) table design it can still be done relatively easily without all those variables I set up a couple of tables to replicate your DB TABLE: product TABLE: price +----+-------+ +----+-------+-------+-------+ | id | name | | id | prod1 | prod2 | prod3 | +----+-------+ +----+-------+-------+-------+ | 1 | prod1 | | 1 | 10.00 | 20.00 | 30.00 | | 2 | prod2 | +----+-------+-------+-------+ | 3 | prod3 | +----+-------+ code const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = '????'; function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } $db = pdoConnect(); // create prices array with prod names as the keys $res = $db->query("SELECT * FROM price"); $prices = $res->fetch(PDO::FETCH_ASSOC); // create an array of prod names (key = prod id) $res = $db->query("SELECT id, name FROM product"); foreach ($res as $r) { $prods[ $r['id'] ] = $r['name']; } $selected_id = 2; $price = $prices[ $prods[$selected_id] ]; echo $price; // 20.00 * * * In future, all you would need to do is add a "price" column to your product table and put the prices in. This will do it for you (use same connection code first) <?php // CONNECTION CODE HERE // $db->exec("ALTER TABLE `product` ADD COLUMN `price` DECIMAL(10,2) NULL AFTER `name`"); $res = $db->query("SELECT * FROM price"); $prices = $res->fetch(PDO::FETCH_ASSOC); $stmt = $db->prepare("UPDATE product SET price = ? WHERE name = ? "); foreach ($prices as $name => $price) { $stmt->execute( [ $price, $name ] ); } // JOB DONE - check the results... $res = $db->query("SELECT id , name , price FROM product "); echo '<pre>'; foreach ($res as $r) { vprintf("%3d | %-20s | %10.2f\n", $r); } Edited February 8, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574128 Share on other sites More sharing options...
Maihes Posted February 8, 2020 Author Share Posted February 8, 2020 (edited) wow that is a lot of code an i appreciate so much that you took your time to write it and help out,the thing is please check this out: my project <== this is the page with what i'm trying to do, it may give you a bit more insight ... the thing is that my price table has more items than the product one so not sure how is that a solution to integrate the prices into that table.. i thought about it too but since the prices table has more items .. wasn't sure how to proceed so i just made a table for it(ik i could've make the table like: id,product,price .. but well at the time i just did it like that and stuck with it) .. well tbh atm for this project .. im using 4 different tables for that page (1 is for the prices on the left, 2 are for the drop down lists(1 for each) and 1 more which gets me: SS Req.; MS Req.:; SO Req.:; EO Req.:; Craft Cost:.) .. what i meant about the variables for the prices is that they are required anyway for them to be displayed so that's how i got them .. the thing is that i agree with you and not just my table .. i do feel that even my coding feels messy .. i think i have like 10-15 different php files(well not big files but like different functions for different parts of the page) for that one page .. haha Anyway, if you could be so kind as to check THIS out(its a .rar file containing all the files for that page) and let me know how messy it is and help me out fixing it i would really appreciate. (ofc when you have time to check it) any tips .. suggestions are so much appreciated Edited February 8, 2020 by Maihes Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574136 Share on other sites More sharing options...
ginerjm Posted February 8, 2020 Share Posted February 8, 2020 Do you think you could write your posts using some punctuation to make it easier to follow what you are saying? Proper sentences would help tremendously instead of a run-on stream of text with no easy way to make sense of what you are trying to say. A period here, a period there and some caps to start would be the thing to do. Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574137 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 The solution should work for those products in the product table whose name matches a column name in the price table. Basically you just need +----+-------+-------+ | id | name | price | +----+-------+-------+ | 1 | prod1 | 10.00 | | 2 | prod2 | 20.00 | | 3 | prod3 | 30.00 | +----+-------+-------+ You can Use the id/name columns to get your product dropdown. Use id/price columns to create your prices array. I don't know what SS Req, MS Req etc are, or how they relate to products, so I can't comment on those 33 minutes ago, Maihes said: what i meant about the variables for the prices is that they are required anyway for them to be displayed No, they are not required. The array elements are your variables If you want to display the price for product 2 then you don't need $ESSPrice, just echo $prices[ $prods[2] ] Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574138 Share on other sites More sharing options...
Barand Posted February 8, 2020 Share Posted February 8, 2020 I've just looked at the data in your sql dump files. Did I say the design was terrible? I apologise for the gross understatement and for referring to it as a database As I hope I demonstrated in my earlier posts, if you get the data right the processing becomes a whole lot easier. Nothing in the other tables matches those column names in your prices table, so all of my solutions go out of the window. There might have been some hope if the first column in prices mapped to id #1 and the 2nd column to id #2 but even that isn't the case. I'll check if dropping "Price" suffix from the column names can save the day. I'll have a look at your code now, but first I need a stiff drink, or several - a large gin and tonic should help. 2 Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574139 Share on other sites More sharing options...
Maihes Posted February 9, 2020 Author Share Posted February 9, 2020 (edited) Ok, so based on what you said i changed a bit the db and merged product with prices and the file looks like this now: CREATE TABLE `pricelist` ( `Id` int(11) NOT NULL, `Product` varchar(13) DEFAULT NULL, `Price` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `pricelist` (`Id`, `Product`, `Price`) VALUES (1, 'TS', '17'), (2, 'ESS', '200'), (3, 'ASBB', '70'), (4, 'MSBB', '300'), (5, 'KA', '30'), (6, 'CFS', '80'), (7, 'BK', '30'), (8, 'ODP', '300'), (9, 'GTDP', '1000'), (10, 'GSDP', '1500'), (11, 'SLGH', '25'), (12, 'RCB', '10'), (13, 'RCCB', '10'), (14, 'CCB', '450'), (15, 'STOR', '500'), (16, 'CPC', '10'), (17, 'PPPC', '10'), (18, 'PPC', '10'), (19, 'APC', '10'), (20, 'MDFC', '10'), (21, 'SDFC', '10'), (22, 'MTDFC', '10'), (23, 'DSFC', '10'), (24, 'SS', '1'), (25, 'MS', '8'), (26, 'SO', '3'), (27, 'EO', '7'); ALTER TABLE `pricelist` ADD PRIMARY KEY (`Id`); ALTER TABLE `pricelist` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=28; COMMIT; so than this can be used for both the prices and for the product dropdown as long as the last 4 rows are not used for the dropdown list then a new `bundle` table for the second dropdown which its the same as the `books` one i sent you(just column names are Id,Pid,Bname) and then lastly the `dopdown` table which remains the same. Edited February 9, 2020 by Maihes Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574140 Share on other sites More sharing options...
Barand Posted February 9, 2020 Share Posted February 9, 2020 Don't rely on table position (last four). Add a "dropdown" flag (1 = used in dropdown, 0 = not) Quote Link to comment https://forums.phpfreaks.com/topic/309994-php-array/#findComment-1574141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.