Connecting Tech Pros Worldwide Forums | Help | Site Map

Compare array value with string

Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#1: Jun 14 '09
I have an array containing the various products
Expand|Select|Wrap|Line Numbers
  1. $inventory = array();
  2. $inventory[0] = "whatever1";
etc
I have an array containing various values
Expand|Select|Wrap|Line Numbers
  1. $quantity = array(100,200,300);
I call in a value from a form $_POST[itemname]; say
I assign a variable
Expand|Select|Wrap|Line Numbers
  1. $var1="$_POST[itemname]";
I want to compare the input value with the array value e.g.
Expand|Select|Wrap|Line Numbers
  1. for($i = 0;$i<$inventory_total;$i++) {
  2.    if($inventory[$i] == $item) { code }
  3. }
However, this is always negative as I believe I cannot compare an array item with a string.
The only thing I've found so far is the "mplode" statement
But I want to compare individual array items not a string composed of all of them.
Can anyone suggest a simple conversion so that I can compare values?

Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#2: Jun 15 '09

re: Compare array value with string


Hi toadstool!

First of all, just pasting the code here without telling us, which language it is isn't a good idea. If there is a specific forum for the language you're using, please use that, otherwise please state which language it is.

So, if you'd tell us, which language you're using, I'm sure one of the mods here will change that for you.

The other criticism is, that you didn't use [code]...[/code] tags. Wrap them around your code and it will look much nicer and can be read much more easily.

Greetings,
Nepomuk
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#3: Jun 15 '09

re: Compare array value with string


I am not familiar with the code tags referred to in one message. Do Ijust add them as such
Expand|Select|Wrap|Line Numbers
  1. ...
  2. if (IN PHP) I call $cart->insert_Pcart($item1, $item2);
  3.  
  4. class Cart 
  5. {
  6. :
  7. function insert_Pcart($item_name,$p_qty)
  8.       {
  9. $insert = "INSERT INTO Cart (pname, qty) 
  10. VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
  11. Line69=>     if (mysql_query($insert, $conn)) { $cartID = mysql_insert_id($conn);
  12.  
  13. } else          { die ("Error inserting CART FORM data: " .mysql_error()); };
  14.  
  15. //$p_name=($_REQUEST["p_name"]);
  16. }
  17.  
I get an error with this
I get the error
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\phpdev\www\project_shopping_cart.php on line 69
Error inserting CART FORM data:

Can anyone explain what this error means, even better suggest a solution.
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
#4: Jun 15 '09

re: Compare array value with string


I added the tags - some improvement - thanks for the suggestion.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#5: Jun 16 '09

re: Compare array value with string


I could be wrong here but it looks like your missing an apostrophe (') in the VALUES portion of your SQL query.

I think you should change this:
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Cart (pname, qty) 
  2. VALUES ($_POST[$item_name]','$_POST[$p_qty]')"; 
To this:
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Cart (pname, qty) 
  2. VALUES ('$_POST[$item_name]','$_POST[$p_qty]')"; 
-Frinny
Reply