Jump to content

Recommended Posts

Hey,

 

I am trying to loop out an option box, but i'm hit a wall.

 

Here is the code:

 

    $select_tags_query = mysql_query('SELECT * FROM `tags`');
    
    $selected_tag_query = mysql_query('SELECT * FROM `store_tags`');
                
    while($tags = mysql_fetch_row($select_tags_query) && $selected = mysql_fetch_row($selected_tag_query)){
    
        echo '<option name="' . $tags[0] . '" value="' . $tags[0] . '"'; 
        
        if($tags[0] == $selected[2])
        {
            echo "selected";
        }else{}
        
        echo '>' . $tags[1] . '</option>';
    }

 

$select_tags_query is grabbing from a table and looping out tag values.

 

$selected_tag_query is grabbing from a list that determines what the user has previously selected, tag wise.

 

I think that this is the problem:

 

$tags = mysql_fetch_row($select_tags_query) && $selected = mysql_fetch_row($selected_tag_query)

 

This may be confusing, but if anyone can help that would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/
Share on other sites

the tags table looks like this:

 

tag_id | tag_name

--------------------

  1    |  shirt

  2    |  pants

  3    |  hat

 

the store_tags table looks like this:

 

store_id | tag_id | order_id

-------------------------------

    1      |    2    |    1

    1      |    3    |    2

    1      |    1    |    3

 

When the users goes to edit his tags, i want the tag that the user has chosen to be selected in the option box.

 

Either that, or i can't figure out a way to only update a changed tag.

What is it actually doing? Where does it not do what you want it to do?

 

From what I can tell from your code and tables, I would think that it would print the options with the id instead of the name. Then, in your if statement, you are comparing the tag_id to the order_id (in the other table), that doesn't seem like that is what you would want to do.

 

You really should do those SELECTs in one query. If one of those two tables ever has a different number of rows than the other, you will be losing data.

@ lemmin,

 

if you have

[pre]

tags      store_tags

------    ----------

  1        A

  2        B

  3        C

[/pre]

 

then

  
SELECT * FROM tags, store_tags

 

gives -->

1 A

1 B

1 C

2 A

2 B

2 C

3 A

3 B

3 C

 

@envexlabs,

 

So in the sample data you just gave, all three tags should be "selected" ? (assuming user belongs to store 1)

@berand

 

the html output that the user sees is:

 

Tag 1: option box (so the user has previously selected option 2, so option 2 should be selected)

Tag 2: option box (so the user has previously selected option 3, so option 3 should be selected)

Tag 3: option box (so the user has previously selected option 1, so option 1 should be selected)

 

using only one query, duplicates the option 3 times, so i dont think that will work.

 

I've been tinkering around, and my while($tags = mysql_fetch_row($select_tags_query) && $selected = mysql_fetch_row($selected_tag_query)) is working, but it's only fetching the rows of $selected and not $tags.

 

I think if i can get the while to grab both i will be golden :P

 

Thanks

 

 

Tag 1: option box (so the user has previously selected option 2, so option 2 should be selected)

Tag 2: option box (so the user has previously selected option 3, so option 3 should be selected)

Tag 3: option box (so the user has previously selected option 1, so option 1 should be selected)

Based on that, I assume you are trying to compare both tag_id's, but you aren't.

Change

if($tags[0] == $selected[2])

to:

if($tags[0] == $selected[1])

, Should do that.

As soon as i add another mysql_fetch_row to the while, all the options go blank.

 

it's not grabbing the first $tag fetchrow, which is why they are going blank.

 

I'm thinking of just using ajax, and updating each one when it is changed, instead of changing all at once.

It seems to me there is no check for a user or anything, like Barand said. It looks like the connection between the two tables is not a row, but the order of the rows, at least, that is how your code works. Whatever entry is at the top row of store_tags will correspond to the top row of the tags table. I assume you handle this with the order_id, to keep them in the right order. Assuming the purpose of order_id is what I stated, you should try the following code:

$query = mysql_query("SELECT tags.tag_id, tags.tag_name, store_tags.tag_id as st_tag_id FROM tags, store_tags WHERE tags.tag_id = store_tags.order_id");

Now you have one query with all the corresponding fields you want so you can use one condition for the loop.

    while($query = mysql_fetch_row($select_tags_query)){
    
        echo '<option name="' . $row['tag_id'] . '" value="' . $row['tag_id'] . '"'; 
        
        if($row['tag_id'] == $row['st_tag_id'])
        {
            echo "selected";
        }else{}
        
        echo '>' . $row['tag_name'] . '</option>';
    }

 

I don't have any idea what store_id is for and why it is has duplicate values. If this isn't how you want it to work then you will need to explain better how you want it to work.

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.