Jump to content

[SOLVED] array problem


Goose87

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 *  

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.