envexlabs Posted August 31, 2007 Share Posted August 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/ Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 What do you want it to do and what is it doing wrong? Why are you using two different queries? "SELECT * FROM `tags`, `store_tags`" would be the exact same. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338635 Share on other sites More sharing options...
envexlabs Posted August 31, 2007 Author Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338650 Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338656 Share on other sites More sharing options...
Barand Posted August 31, 2007 Share Posted August 31, 2007 @ 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) Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338660 Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 I suggested it without knowing any fields in the table. I assumed he knew how to select something other that the entirety of a table. I still think he should use one query for it. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338665 Share on other sites More sharing options...
envexlabs Posted August 31, 2007 Author Share Posted August 31, 2007 @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 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338673 Share on other sites More sharing options...
Barand Posted August 31, 2007 Share Posted August 31, 2007 So how do you know what the user selected before? The is no user_id so I guess it has to be either store_id or order_id that indicated the user's previous choice. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338679 Share on other sites More sharing options...
envexlabs Posted August 31, 2007 Author Share Posted August 31, 2007 im an idiot, i didn't even think to just grab the store_id's from the table. i will try this! thanks Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338684 Share on other sites More sharing options...
envexlabs Posted August 31, 2007 Author Share Posted August 31, 2007 it's still ignoring my first mysql_fetch_row. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338689 Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338696 Share on other sites More sharing options...
envexlabs Posted August 31, 2007 Author Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338704 Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67454-looping-out-a-option-box/#findComment-338715 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.