Jump to content

loop to view multiple results from one column?


Recommended Posts

Confusing I know.  Here's what I've got.  I used the Dreamweaver Developer Toolbox in a form that has multiple checkbox results entered into one db column.  Id's are entered into this column that join with another table to connect the text to these id's.

 

So the column looks like this: 1,5,7,8.

The corresponding text is red, blue, green, yellow.

 

I'm using the str_replace to change the comma to a <BR> so the results read in separate rows.  Where I'm drawing a blank is how to show all these results as text.  So far I've only been successful showing this:

 

1

5

7

8

 

And only the first result as text (red).

I'd like the results to look like this:

 

red

blue

green

yellow

 

I'll take any help pointing me in a correct direction.

Thanks!

Hi

 

Depends how flexible you want it to be.

 

You could use:-

 

$colours = array(1=>'red',5=>'blue',7=>'green',8=>'yellow');
$Fields = explode(',',$row['id']);
foreach($Fields as $Field)
{
  echo $colours[$Field]."<br />";
}

 

Trouble is that this requires hard coded lists of colours.

 

It would be better if there was a separate table containing a separate row for each ID for each row from the current table. Then you could just use a JOIN against a table of colours to get them.

 

All the best

 

Keith

Thanks!

There is a separate table that the values do correspond to.  And that's definitely way I'd like to go.

 

This is the column I'm pulling the values from $row_rsKSA['knowledge_ksa']

 

Here's the equivalent of what I've got:

Table 1  = id, color

Table 2 = id, color_id

 

The color_id has the multiple values (1,5,7)

 

 

 

 

 

Hi

 

You really need to split up the field that has multiple values onto a seperate table with one row per value.

 

Eg, while you have something like:-

 

Table1

Id - Name - ColourId

1 - Fred - 1,2,3,4,5

2 - Burt - 2,4,6

 

What you want is something like:-

 

Table1

Id - Name

1 - Fred

2 - Burt

 

Table2

Id - Table1Id - ColourId

1 - 1 - 1

2 - 1 - 2

3 - 1 - 3

4 - 1 - 4

5 - 1 - 5

6 - 2 - 2

7 - 2 - 4

8 - 2 - 6

 

While it is possible to do a join against the various values from the comma seperated column, the code to do it is going to be dog slow and isn't particularly friendly for anyone who has to modify it in the future.

 

All the best

 

Keith

I gotcha.  Makes sense and does seem less painful in the long run.

I think I was just trying to combine too many processes into one.  I liked the use of the comma-separated checkbox and wanted to run with it. 

 

Thanks for the help Keith!

 

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.