Goose87 Posted December 1, 2007 Share Posted December 1, 2007 Ok, well I have a list of items, and they are all displayed down the page in a table... The problem I'm having is knowing which one they have pressed. When they press one of the buttons, it changes the url to ....?id=4 for example. I then take the number 4, and I'm trying to get that to know what type of thing that is in the database. At the moment i have $array1=array(1, 4, 8, 12); $array2=array(2, 5, 9, 13); $array3=array(3,6,10,14); $array4=array(4,7,11,15); I then have: if(isset($_GET['id'])) { * which works * and then I have.. if(isset($array1[$_GET['id']])) { * do something * } elseif(isset($array2[$_GET['id'])) { * do something... ETC. ETC.. The first part of getting the "id" from the url works fine, but the array part just isnt working. Any advice would be grately appreciated. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted December 1, 2007 Share Posted December 1, 2007 i assume you want this array_key_exists($_GET['id'], $array1); Quote Link to comment Share on other sites More sharing options...
Goose87 Posted December 1, 2007 Author Share Posted December 1, 2007 erm, i think so.. basically if the ID is 4 for example, i want that to do the code that is within the if(isset($array1) part, and if the ID is like 6, it'll only implement the code that is related to the $array that is related to that. Do you get what i mean? I didn't want to post all of my code becuase there is a lot of extra parts included in it. Quote Link to comment Share on other sites More sharing options...
Fehnris Posted December 1, 2007 Share Posted December 1, 2007 Im not sure if its going to make a difference but you are trying to compare an integer in your array to the $_GET['id'] value which is more than likely a string. You could try adding intval() to your $_GET['id'] value to convert the GET value from string to integer before the comparison takes place like:- if(isset($array1[intval($_GET['id'])])) { * do something * Quote Link to comment Share on other sites More sharing options...
mlin Posted December 1, 2007 Share Posted December 1, 2007 or you could typecast like this: $id = (int)$_GET['id']; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 1, 2007 Share Posted December 1, 2007 If you can change the array to be two dimensional, you can do something like this: <?php $large_array=array(1=>array(1, 4, 8, 12), 2=>array(2, 5, 9, 13), 3=>array(3,6,10,14), 4=>array(4,7,11,15)); $id = $_GET['id']; $found = false; $found_index = 0; for ($i=1; $i<=count($large_array) && !$found;$i++) if (in_array($id,$large_array[$i])) { $found_index = $i; $found = true; } echo $id . ' was found in array ' . $found_index . "<br>"; print_r($large_array[$found_index]); ?> Ken Quote Link to comment Share on other sites More sharing options...
Goose87 Posted December 2, 2007 Author Share Posted December 2, 2007 brilliant help ken, thanks a lot for that! i REALLY appreciate it. Goose. Quote Link to comment 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.