Connecting Tech Pros Worldwide Help | Site Map

Compare array value with string

  #1  
Old June 14th, 2009, 05:15 AM
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16
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?

Last edited by Frinavale; June 16th, 2009 at 05:41 PM. Reason: Added code tags. Please post code in [code] [/code] tags. Moved thread to PHP Answers from Misc.
  #2  
Old June 15th, 2009, 02:31 PM
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,468

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
  #3  
Old June 15th, 2009, 08:52 PM
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16

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.
  #4  
Old June 15th, 2009, 08:54 PM
Newbie
 
Join Date: Jun 2009
Location: Australia
Posts: 16

re: Compare array value with string


I added the tags - some improvement - thanks for the suggestion.
  #5  
Old June 16th, 2009, 05:39 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare array value with string toadstool answers 17 June 16th, 2009 01:40 PM
Return value of FN procedure dim'd as Variant cannot be assigned an Array value MLH answers 11 November 21st, 2006 06:35 PM
Help with string.StartsWith() please CCLeasing answers 4 November 7th, 2006 03:35 PM
stacks and array JC answers 3 July 19th, 2005 08:04 PM