Jump to content

Updating Tags: What would YOU do?


kingnutter

Recommended Posts

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

<?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
        }  
    }
} ?>

Link to comment
Share on other sites

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.