Jump to content

How to use one array to display items from a second array


huntr77

Recommended Posts

Hi,

 

I'm hoping someone can help me with a simply array problem.

 

I have a table with two columns: "ID" and "Name" and 100 Names.

 

I also have an array called $myIDList containing 10 IDs. I want to display 10 Names from the table that correspond with the IDs from my array.

 

Originally, I tried to implode the array, adding commas and inserted into my MySQL query using:

 

$myQuery = "SELECT ID, Name FROM namesTable WHERE ID IN ($myImplodedIDList)";

 

The problem was that duplicated names (i.e. the same Name, but assiged to different IDs) would only be displayed once.

 

So now I'm trying to use the original array to loop through a second array and display Names where the ID matches an ID from my first array. I have succesffuly listed the table like this:

 

$myQuery = "SELECT ID, Name FROM myTable ORDER BY ID";

 

$myResult = mysql_query($myQuery) or die(mysql_error());

 

    while($row=mysql_fetch_array($myResult)) {

echo $row['ID'];

echo $row['Name'];

    }

 

But I want to do something like:

 

    while($row=mysql_fetch_array($myResult)) {

while $row['ID'] = $myIDList {

echo $row['Name']

}

    }

 

Can someone shed some light onto this for me please?

 

Thank you!

Bryan

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.