kingnutter Posted August 30, 2009 Share Posted August 30, 2009 I am recalling user defined tags into an update form field for using the code below: $query="SELECT moj_genre FROM genres INNER JOIN genrelinkcd ON genres.genre_id=genrelinkcd.genre_id WHERE genrelinkcd.moj_id='$id'"; $result=mysql_query($query); $num_rows=mysql_num_rows($result); $count=1; while ($row = mysql_fetch_row($result)) { echo "$row[0]"; if ($count < $num_rows) { echo ', '; } $count++; } I have a separate table linking the tags to objects. My question is, how would you manipulate the code to update the relationships? Should I delete the original records and create new ones each time? This seems wasteful, but how would you retain the original relationships in order to only amend changes? Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/ Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 Instead of: $num_rows=mysql_num_rows($result); $count=1; while ($row = mysql_fetch_row($result)) { echo "$row[0]"; if ($count < $num_rows) { echo ', '; } $count++; } You could also do: $parts = array(); while ($row = mysql_fetch_row($result)) { $parts[] = $row[0]; } print implode(', ', $parts); Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/#findComment-909330 Share on other sites More sharing options...
The Eagle Posted August 30, 2009 Share Posted August 30, 2009 As posted above, $parts = array(); while ($row = mysql_fetch_row($result)) { $parts[] = $row[0]; } print implode(', ', $parts); This should definitely work. Try this. Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/#findComment-909338 Share on other sites More sharing options...
kingnutter Posted August 30, 2009 Author Share Posted August 30, 2009 Very neat. But how does this help me recall / remove / reinsert the amended tags where applicable when the update is submitted? For instance if the original array "Rock, Pop, Soul" is changed by the user to "Rock, Jazz, Classical"? Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/#findComment-909349 Share on other sites More sharing options...
ignace Posted August 31, 2009 Share Posted August 31, 2009 <?php $genres = array(); $query="SELECT moj_id, moj_genre FROM genres INNER JOIN genrelinkcd ON genres.genre_id=genrelinkcd.genre_id WHERE genrelinkcd.moj_id='$id'"; //.. while ($row = mysql_fetch_row($result)) { $genres[$row[0]] = $row[1]; //.. ?> <input type="text" name="tag[<?php print $row[0]; ?>]" value="<?php print $row[1]; ?>"><!-- Tag:Rock --> <input type="text" name="tag[<?php print $row[0]; ?>]" value="<?php print $row[1]; ?>"><!-- Tag:Pop --> <input type="text" name="tag[<?php print $row[0]; ?>]" value="<?php print $row[1]; ?>"><!-- Tag:Soul --> <?php } if (isset($_POST['tag']) && is_array($_POST['tag'])) { $ids = array_keys($genres); foreach ($_POST['tag'] as $id => $value) { if (!in_array($id, $ids)) { //newly added tag Rock, Pop, Soul, [Jazz] } else if ($value !== $genres[$id]) { //modified a tag } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/#findComment-909567 Share on other sites More sharing options...
kingnutter Posted August 31, 2009 Author Share Posted August 31, 2009 Thanks so much for your detailed reply. To be very honest, I'm not completely sure what's going on here, or where you're put the dot dots but I shall have a tinker and let you know how it goes. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/#findComment-909857 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.