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
https://forums.phpfreaks.com/topic/172483-updating-tags-what-would-you-do/
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);

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.