Paul Butler diff 算法致命错误且结果未显示

发布于 2025-01-02 23:56:17 字数 8312 浏览 1 评论 0原文

哇我今天有很多问题… 所以我设法稍微修复了用于 Paul Butler 的 diff 算法的代码。然而,虽然代码本身似乎没有问题,但它并没有比较我希望它从数据库中比较的两个文本值。我想知道这是否有什么具体原因?或者也许这与所有内容都在表格中有关?不确定,但无论如何,那里只是一个空白...... 另外,当为 $old 和 $new 放置不同值的代码时,它现在显示:

致命错误:无法在 /home/equinox/public_html/admins/profile-tool_elizabeth.php 第 169 行重新声明 diff() (之前在 /home/equinox/public_html/admins/profile-tool_elizabeth.php:125 中声明)

致命错误:无法 不知道为什么……第 125 行是第一个“函数差异”,第 169 行是我想要的接下来两个值的“函数差异”进行比较。不确定那里发生了什么......

这基本上是整个代码:

<?php

  session_start();

   while ($nSELECT = mysql_fetch_object($SELECT)) {

        echo "

          <tr bgcolor=\"#000000\">

           <td><a href=\"$PHP_SELF?id=" . $nSELECT->userid . "\">" . getName($nSELECT->userid) . "</a></td>


          </tr>

        ";

      }

    }

    echo "</table><p>";

  } elseif (empty($_GET['s']) && !empty($_GET['id'])) {

    $SELECT = mysql_query("SELECT * FROM profile_edit WHERE userid = '$id'") or die(mysql_error()); 
    $SELECT = mysql_fetch_object($SELECT);

    $USERINFO = mysql_query("SELECT * FROM members WHERE id = '$id'") or die(mysql_error());
    $USERINFO = mysql_fetch_object($USERINFO);

    $oldhistory = mysql_query("SELECT history FROM members WHERE id = '$id'") or die(mysql_error());
    $oldhistory = mysql_fetch_object($oldhistory);

    $newhistory = mysql_query("SELECT history FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $newhistory = mysql_fetch_object($newhistory);

    $OLDPERSONALITY = mysql_query("SELECT personality FROM members WHERE id = '$id'") or die(mysql_error());
    $OLDPERSONALITY = mysql_fetch_object($OLDPERSONALITY);

    $NEWPERSONALITY = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $NEWPERSONALITY = mysql_fetch_object($NEWPERSONALITY);

    $OLDAPPEARANCE = mysql_query("SELECT description FROM members WHERE id = '$id'") or die(mysql_error());
    $OLDAPPEARANCE = mysql_fetch_object($OLDAPPEARANCE);

    $NEWAPPEARANCE = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());    
    $NEWAPPEARANCE = mysql_fetch_object($NEWAPPEARANCE);




    echo "

      <form action=\"$PHP_SELF?id=$id&s=a\" method=\"post\">

      <table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" align=\"center\" bgcolor=\"#234904\">

        <tr bgcolor=\"#000000\">

         <td>Username :: </td>

         <td><a href=\"/profile.php?id=".$SELECT->userid."\" target=\"_blank\">" . getName($SELECT->userid) ."</a> (Click to see current profile in new window)</td>

        <tr bgcolor=\"#000000\">

         <td>Species :: </td>

         <td>" . $USERINFO->species2 ." -> ". $USERINFO->subspecies ." (". $USERINFO->clan .")</td>

        <tr bgcolor=\"#000000\">

         <td>History :: </td>
         <td> 
";

function diff($oldhistory, $newhistory){
    foreach($oldhistory as $oindex => $ovalue){
        $nkeys = array_keys($newhistory, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldhistory, 'i'=>$newhistory));
    return array_merge(
        diff(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)),
        array_slice($newhistory, $nmax, $maxlen),
        diff(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen)));
}

function htmlDiff($oldhistory, $newhistory){
    $diff = diff(explode(' ', $oldhistory), explode(' ', $newhistory));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}



echo "</td>

        </tr>


        <tr bgcolor=\"#000000\">

         <td>Description :: </td>

         <td>";

function diff($oldappearance, $newappearance){
    foreach($oldappearance as $oindex => $ovalue){
        $nkeys = array_keys($newappearance, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldappearance, 'i'=>$newappearance));
    return array_merge(
        diff(array_slice($oldappearance, 0, $omax), array_slice($newappearance, 0, $nmax)),
        array_slice($newappearance, $nmax, $maxlen),
        diff(array_slice($oldappearance, $omax + $maxlen), array_slice($newappearance, $nmax + $maxlen)));
}

function htmlDiff($oldappearance, $newappearance){
    $diff = diff(explode(' ', $oldappearance), explode(' ', $newappearance));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}


echo "</td>


        </tr>


        <tr bgcolor=\"#000000\">

         <td>Description :: </td>

         <td>";

function diff($oldappearance, $newappearance){
    foreach($oldappearance as $oindex => $ovalue){
        $nkeys = array_keys($newappearance, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldappearance, 'i'=>$newappearance));
    return array_merge(
        diff(array_slice($oldappearance, 0, $omax), array_slice($newappearance, 0, $nmax)),
        array_slice($newappearance, $nmax, $maxlen),
        diff(array_slice($oldappearance, $omax + $maxlen), array_slice($newappearance, $nmax + $maxlen)));
}

function htmlDiff($oldappearance, $newappearance){
    $diff = diff(explode(' ', $oldappearance), explode(' ', $newappearance));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}


echo "</td>

        </tr>

        <tr bgcolor=\"#000000\">

         <td colspan=\"2\" style=\"text-align: center\"><input type=\"submit\" value=\"Accept\"></td>

        </tr>

      </table>

      </form>

      <p><br>

      <form action=\"$PHP_SELF?id=$id&s=d\" method=\"post\">

    <table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" align=\"center\" bgcolor=\"#234904\">

      <tr bgcolor=\"#000000\">

         <td>Reason :: </td>

         <td><textarea name=\"reason\" rows=\"6\" cols=\"30\"></textarea></td>

        </tr>

        <tr bgcolor=\"#000000\">

         <td colspan=\"2\" style=\"text-align: center\"><input type=\"submit\" value=\"Deny\"></td>

        </tr>

      </table>

      </form>

    ";

  } 


  incFooter();

?>

Wow I've got a lot of questions today...
So I managed to fix the code I used for Paul Butler's diff algorithm a bit. However, while there doesn't seem to be a problem with the code itself, it's not comparing the two text values I want it to compare from the database. I was wondering if there was a specific reason for that? Or maybe it has to do with the fact that it's all in a table? Not sure, but either way, there's just a blank space there...
Also, when putting the code with different values for $old and $new, it now says:

Fatal error: Cannot redeclare diff() (previously declared in /home/equinox/public_html/admins/profile-tool_elizabeth.php:125) in /home/equinox/public_html/admins/profile-tool_elizabeth.php on line 169

I don't know why that is... line 125 is the first "function diff" and 169 is "function diff" for the next two values I want to compare. Not sure what's happening there...

here's basically the whole code:

<?php

  session_start();

   while ($nSELECT = mysql_fetch_object($SELECT)) {

        echo "

          <tr bgcolor=\"#000000\">

           <td><a href=\"$PHP_SELF?id=" . $nSELECT->userid . "\">" . getName($nSELECT->userid) . "</a></td>


          </tr>

        ";

      }

    }

    echo "</table><p>";

  } elseif (empty($_GET['s']) && !empty($_GET['id'])) {

    $SELECT = mysql_query("SELECT * FROM profile_edit WHERE userid = '$id'") or die(mysql_error()); 
    $SELECT = mysql_fetch_object($SELECT);

    $USERINFO = mysql_query("SELECT * FROM members WHERE id = '$id'") or die(mysql_error());
    $USERINFO = mysql_fetch_object($USERINFO);

    $oldhistory = mysql_query("SELECT history FROM members WHERE id = '$id'") or die(mysql_error());
    $oldhistory = mysql_fetch_object($oldhistory);

    $newhistory = mysql_query("SELECT history FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $newhistory = mysql_fetch_object($newhistory);

    $OLDPERSONALITY = mysql_query("SELECT personality FROM members WHERE id = '$id'") or die(mysql_error());
    $OLDPERSONALITY = mysql_fetch_object($OLDPERSONALITY);

    $NEWPERSONALITY = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $NEWPERSONALITY = mysql_fetch_object($NEWPERSONALITY);

    $OLDAPPEARANCE = mysql_query("SELECT description FROM members WHERE id = '$id'") or die(mysql_error());
    $OLDAPPEARANCE = mysql_fetch_object($OLDAPPEARANCE);

    $NEWAPPEARANCE = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());    
    $NEWAPPEARANCE = mysql_fetch_object($NEWAPPEARANCE);




    echo "

      <form action=\"$PHP_SELF?id=$id&s=a\" method=\"post\">

      <table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" align=\"center\" bgcolor=\"#234904\">

        <tr bgcolor=\"#000000\">

         <td>Username :: </td>

         <td><a href=\"/profile.php?id=".$SELECT->userid."\" target=\"_blank\">" . getName($SELECT->userid) ."</a> (Click to see current profile in new window)</td>

        <tr bgcolor=\"#000000\">

         <td>Species :: </td>

         <td>" . $USERINFO->species2 ." -> ". $USERINFO->subspecies ." (". $USERINFO->clan .")</td>

        <tr bgcolor=\"#000000\">

         <td>History :: </td>
         <td> 
";

function diff($oldhistory, $newhistory){
    foreach($oldhistory as $oindex => $ovalue){
        $nkeys = array_keys($newhistory, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldhistory, 'i'=>$newhistory));
    return array_merge(
        diff(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)),
        array_slice($newhistory, $nmax, $maxlen),
        diff(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen)));
}

function htmlDiff($oldhistory, $newhistory){
    $diff = diff(explode(' ', $oldhistory), explode(' ', $newhistory));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}



echo "</td>

        </tr>


        <tr bgcolor=\"#000000\">

         <td>Description :: </td>

         <td>";

function diff($oldappearance, $newappearance){
    foreach($oldappearance as $oindex => $ovalue){
        $nkeys = array_keys($newappearance, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldappearance, 'i'=>$newappearance));
    return array_merge(
        diff(array_slice($oldappearance, 0, $omax), array_slice($newappearance, 0, $nmax)),
        array_slice($newappearance, $nmax, $maxlen),
        diff(array_slice($oldappearance, $omax + $maxlen), array_slice($newappearance, $nmax + $maxlen)));
}

function htmlDiff($oldappearance, $newappearance){
    $diff = diff(explode(' ', $oldappearance), explode(' ', $newappearance));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}


echo "</td>


        </tr>


        <tr bgcolor=\"#000000\">

         <td>Description :: </td>

         <td>";

function diff($oldappearance, $newappearance){
    foreach($oldappearance as $oindex => $ovalue){
        $nkeys = array_keys($newappearance, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldappearance, 'i'=>$newappearance));
    return array_merge(
        diff(array_slice($oldappearance, 0, $omax), array_slice($newappearance, 0, $nmax)),
        array_slice($newappearance, $nmax, $maxlen),
        diff(array_slice($oldappearance, $omax + $maxlen), array_slice($newappearance, $nmax + $maxlen)));
}

function htmlDiff($oldappearance, $newappearance){
    $diff = diff(explode(' ', $oldappearance), explode(' ', $newappearance));
    foreach($diff as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
                (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}


echo "</td>

        </tr>

        <tr bgcolor=\"#000000\">

         <td colspan=\"2\" style=\"text-align: center\"><input type=\"submit\" value=\"Accept\"></td>

        </tr>

      </table>

      </form>

      <p><br>

      <form action=\"$PHP_SELF?id=$id&s=d\" method=\"post\">

    <table cellpadding=\"5\" cellspacing=\"1\" border=\"0\" align=\"center\" bgcolor=\"#234904\">

      <tr bgcolor=\"#000000\">

         <td>Reason :: </td>

         <td><textarea name=\"reason\" rows=\"6\" cols=\"30\"></textarea></td>

        </tr>

        <tr bgcolor=\"#000000\">

         <td colspan=\"2\" style=\"text-align: center\"><input type=\"submit\" value=\"Deny\"></td>

        </tr>

      </table>

      </form>

    ";

  } 


  incFooter();

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-01-09 23:56:17

将您的函数从 diff 重命名为其他名称。如果您尝试在同一作用域中声明具有相同名称的两个函数,则会出现该错误消息。

Rename your function from diff to something else. That error message happens if you attempt to declare the two functions with the same name in the same scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文