JackQaZ Posted January 3, 2011 Share Posted January 3, 2011 Hello I hope someone can help me. I am using JOOMLA AND VIRTUEMART. I am trying to add some form data from the database to surten products in the checkout process. So I am trying to read the SKU number from the shopping-cart basket. This I can do. And here is the problem. I need to search the shopping-cart basket for the right sku number(s). Then write it to the database, if the right number(s) is found. If the right SKU is not found A defauld valeu must be writen to the database. Then I search the database for the right sku number and echo the form data in the right places in the checkout process. I have made a attempt to solve this puzzle but I don't know PHP scripting. I have found this for my problem but it does not work totaly. I have an array which have another array inside and Id like to searh a value from that array My array: Code: print_r($product_rows);gives:Array ( [0] => Array ( [row_color] => sectiontableentry1 [product_name] => Lattaaa pew[product_attributes] => [product_sku] => xxxx [product_price] => 45.00 € ) Now I have modifer: Code: $db_sku = xxxx Now I'd like to search if that xxxx value is in that array How that if sentence should be written? Atm my code looks like this but its not working Code: if (in_array(array($product_sku, $db_sku ),$product_rows) ) { echo found; } else { echo not_found; } Any hints? print_r($product_rows); gives: The array looks like this: ----------------------------------------------------------------------- Array ( [0] => Array ( [row_color] => sectiontableentry1 [product_name] => Standaard Roll-up banner - 85 x 205 cm Standaard: Roll-up banner (= €117,81) [product_attributes] => [product_sku] => 00001 [product_availability] => [product_price] => €117,81 [quantity] => 1 [subtotal] => €117,81 [update_form] => [delete_form] => ) [1] => Array ( [row_color] => sectiontableentry2 [product_name] => Test2 Losse: spandoek_UV-Print [product_attributes] => [product_sku] => 10001 [product_availability] => [product_price] => €258,83 [quantity] => 1 [subtotal] => €258,83 [update_form] => [delete_form] => ) [2] => Array ( [row_color] => sectiontableentry1 [product_name] => Luxe Roll-up banner - 85 x 205 cm Luxe: Roll-up banner (= €213,01) [product_attributes] => [product_sku] => 00002 [product_availability] => [product_price] => €213,01 [quantity] => 1 [subtotal] => €213,01 [update_form] => [delete_form] => ) ) €117,81 1 €117,81 Test2 Losse: spandoek_UV-Print 10001 Array ( [0] => Array ( [row_color] => sectiontableentry1 [product_name] => Standaard Roll-up banner - 85 x 205 cm Standaard: Roll-up banner (= €117,81) [product_attributes] => [product_sku] => 00001 [product_availability] => [product_price] => €117,81 [quantity] => 1 [subtotal] => €117,81 [update_form] => [delete_form] => ) [1] => Array ( [row_color] => sectiontableentry2 [product_name] => Test2 Losse: spandoek_UV-Print [product_attributes] => [product_sku] => 10001 [product_availability] => [product_price] => €258,83 [quantity] => 1 [subtotal] => €258,83 [update_form] => [delete_form] => ) [2] => Array ( [row_color] => sectiontableentry1 [product_name] => Luxe Roll-up banner - 85 x 205 cm Luxe: Roll-up banner (= €213,01) [product_attributes] => [product_sku] => 00002 [product_availability] => [product_price] => €213,01 [quantity] => 1 [subtotal] => €213,01 [update_form] => [delete_form] => ) ) €258,83 1 €258,83 Luxe Roll-up banner - 85 x 205 cm Luxe: Roll-up banner (= €213,01) 00002 Array ( [0] => Array ( [row_color] => sectiontableentry1 [product_name] => Standaard Roll-up banner - 85 x 205 cm Standaard: Roll-up banner (= €117,81) [product_attributes] => [product_sku] => 00001 [product_availability] => [product_price] => €117,81 [quantity] => 1 [subtotal] => €117,81 [update_form] => [delete_form] => ) [1] => Array ( [row_color] => sectiontableentry2 [product_name] => Test2 Losse: spandoek_UV-Print [product_attributes] => [product_sku] => 10001 [product_availability] => [product_price] => €258,83 [quantity] => 1 [subtotal] => €258,83 [update_form] => [delete_form] => ) [2] => Array ( [row_color] => sectiontableentry1 [product_name] => Luxe Roll-up banner - 85 x 205 cm Luxe: Roll-up banner (= €213,01) [product_attributes] => [product_sku] => 00002 [product_availability] => [product_price] => €213,01 [quantity] => 1 [subtotal] => €213,01 [update_form] => [delete_form] => ---------------------------------------------------------------------------------- I have tryed the following code: But the code only see's the first one in the array. if (in_array('xxxx', $product_rows[0])) { echo "xxx found";} OR; if (in_array(array($product_sku, $db_sku ),$product_rows) ) { echo found; } else { echo not_found; } I have found a other peace of code. voor bigger array's but it is incompleet can some one take a look at this code? Problem comes when there is bigger array like [1],[2], etc though. The SKU numbers I seek are; 10001 and 00001 and 000066. <?php I dont know what to do with these variables? $array = '?'; $array_var = '?'; $string = '?'; function recur_in_array($array,$string){ for($i=0;$i<count($array);$i++){ if(is_array($array[$i])){ $found = recur_in_array($array[$i],$string); if($found === true){ return $found; } }else{ if (in_array($string, $product_rows[0])) { return true; } } }} if(recur_in_array($array_var) === true){ echo("xxx found");} ?> There is a error in this code anyone know where? Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/ Share on other sites More sharing options...
JackQaZ Posted January 3, 2011 Author Share Posted January 3, 2011 This seems to by a bit better then the one above. But still there is a error in it it does see the 10001 number but my page is not displaying right. can some one help me please.? <?php $string = '10001'; function recur_in_array($product_rows,$string){ for($i=0;$i<count($product_rows);$i++){ if(in_array($array[$i])){ $found = recur_in_array($product_rows[$i],$string); if($found == true){ return $found; } }else{ if (in_array($string, $product_rows[0])) { return true; } } }} if (recur_in_array($product_rows) == true){ echo '10001 found';} ?> Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154377 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 You'd probably have better luck getting help if your code was properly formatted with new lines, and indented, and enclosed in the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154382 Share on other sites More sharing options...
JackQaZ Posted January 3, 2011 Author Share Posted January 3, 2011 Oke I will try to do this aldo I dont know what it all meens. Ik have some luck with these codes: But there is a problem the result differents when the order is different of the products. I get two times found and one time not found. by the echo. He should only give one found. Here is the code that works a bit. <?php for($i=0;$i<count($product_rows);$i++){ if (in_array('10001', $product_rows[$i])) { echo "10001 found"; $found = '10001'; mysql_query("update `jos_vm_product` set `product_skur` = '$found' WHERE product_id ='1381'") or die(mysql_error()); } else { echo "10001 not found"; $notfound = '00000'; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound' WHERE product_id ='1381'") or die(mysql_error()); }} ?> <?php for($i=0;$i<count($product_rows);$i++){ if (in_array('00006', $product_rows[$i])) { echo "00006 found"; $found2 = '00006'; mysql_query("update `jos_vm_product` set `product_skur` = '$found2' WHERE product_id ='1242'") or die(mysql_error()); } else { echo "00006 not found"; $notfound2 = '00000'; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound2' WHERE product_id ='1242'") or die(mysql_error()); }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154390 Share on other sites More sharing options...
jcbones Posted January 4, 2011 Share Posted January 4, 2011 I don't follow the logic, 2 loops through the same data? Then updating a static row? By your logic, you would update product_id 1242 if 00006 was found in any of the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154459 Share on other sites More sharing options...
sasa Posted January 4, 2011 Share Posted January 4, 2011 try <?php $test = Array ( 0 => Array ( 'row_color' => 'sectiontableentry1', 'product_name' => 'Standaard Roll-up banner - 85 x 205 cm Standaard: Roll-up banner (= €117,81)', 'product_attributes' => '', 'product_sku' => '00001', 'product_availability' => '', 'product_price' => '€117,81', 'quantity' => 1, 'subtotal' => '€117,81', 'update_form' => '', 'delete_form' => ''), 1 => Array ( 'row_color' => 'sectiontableentry2', 'product_name' => 'Test2 Losse: spandoek_UV-Print', 'product_attributes' =>'', 'product_sku' => '10001', 'product_availability' =>'', 'product_price' => '€258,83', 'quantity' => 1, 'subtotal' => '€258,83', 'update_form' =>'', 'delete_form' =>''), 2 => Array ( 'row_color' => 'sectiontableentry1', 'product_name' => 'Luxe Roll-up banner - 85 x 205 cm Luxe: Roll-up banner (= €213,01)', 'product_attributes' =>'', 'product_sku' => '00002', 'product_availability' =>'', 'product_price' => '€213,01', 'quantity' => 1, 'subtotal' => '€213,01', 'update_form' => '', 'delete_form' => '')); function my_find_in_sku($array, $srch){ foreach ($array as $v) if($v['product_sku'] == $srch) return true; return false; } if(my_find_in_sku($test, '10001')){ echo 'find it'; } else { echo 'not'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154535 Share on other sites More sharing options...
JackQaZ Posted January 4, 2011 Author Share Posted January 4, 2011 Hello Thanks for your reply. I have got it working but I don't understand PHP at all. So My code is ferry ugly. This those the trick. <?php for($i=0;$i<count($product_rows);$i++){ if (in_array('10001', $product_rows[$i])) { echo "10001 not found"; $notfound = '00000'; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound' WHERE product_id ='1381'") or die(mysql_error()); } else { echo "10001 not found"; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound' WHERE product_id ='1381'") or die(mysql_error()); }} for($i=0;$i<count($product_rows);$i++){ if (in_array('10001', $product_rows[$i])) { echo "10001 found"; $found = '10001'; mysql_query("update `jos_vm_product` set `product_skur` = '$found' WHERE product_id ='1381'") or die(mysql_error()); } } for($i=0;$i<count($product_rows);$i++){ if (in_array('00006', $product_rows[$i])) { echo "00006 not found"; $notfound = '00000'; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound' WHERE product_id ='1242'") or die(mysql_error()); } else { echo "00006 not found"; mysql_query("update `jos_vm_product` set `product_skur` = '$notfound' WHERE product_id ='1242'") or die(mysql_error()); }} for($i=0;$i<count($product_rows);$i++){ if (in_array('00006', $product_rows[$i])) { echo "00006 found"; $found = '00006'; mysql_query("update `jos_vm_product` set `product_skur` = '$found' WHERE product_id ='1242'") or die(mysql_error()); } } ?> I am Working with a shopping basket. So there can by any number of products in the basket at any order. The problem was that the result of my code different because in the array the object reacurt let say 6 times not found and one time found. So the order made the result different each time. I want to write a value to the database null or the SKU number thats found. I also have to write and search more than one SKU number. If anyone can write a nice code for my I would by ferry glad. When I write a function the website is not displayed the right way. Quote Link to comment https://forums.phpfreaks.com/topic/223287-search-problem-in-array/#findComment-1154618 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.