Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print_r() function returns result as simple "Array" for diff algorithm

I'm using a diff algorithm (the Paul Butler one), which I now got working somewhat. I've discovered that I needed mysql_fetch_array() rather than mysql_fetch_object(), which solved warnings and error messages, but now when I try print_r() to display the result, the result is simply "Array" instead of the field from the database I need it to display and compare... when I call a different function, it seems to show the fields, but doesn't compare them rather than listing them twice and with Array ( [0] => Array ( [d] => Array ( [0] => before it.

Any idea why? And yes I've googled this and looked it up and spent about two days trying to work it out.

Here's the parts of the script:

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

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

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

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

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

    $newappearance = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());    
    $newappearance = mysql_fetch_array($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>History :: </td>
         <td> 
";

function diffhistory($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(
        diffhistory(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)),
        array_slice($newhistory, $nmax, $maxlen),
        diffhistory(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen)));
}

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

}
print_r (diffhistory($oldhistory, $newhistory));
like image 778
Elizabeth Avatar asked Nov 20 '25 18:11

Elizabeth


1 Answers

It may be that you simply need to use mysql_fetch_assoc() (or possibly mysql_fetch_row()) to read the data from the database as associative OR indexed and not both, but I suspect that your approach is quite flawed - for one thing you are making 6 queries where one will do. You are also passing a single value array to a function that I suspect is expecting two arrays of multiple values to compare.

Try this instead (EDITED based on comments below):

<?php

  function diff($old, $new){
    $maxlen = 0;
    foreach ($old as $oindex => $ovalue) {
      $nkeys = array_keys($new, $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;
        }
      } 
    }
    return ($maxlen == 0)
      ? array(array('d'=>$old, 'i'=>$new))
      : array_merge(
          diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
          array_slice($new, $nmax, $maxlen),
          diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
  }

  function htmlDiff($old, $new){
    $ret = '';
    $diff = diff(preg_split('/\s+/', $old), preg_split('/\s+/', $new));
    foreach ($diff as $k) {
      if (is_array($k)) {
          $ret .= (!empty($k['d']) ? "<del style='color:red'>".implode(' ',$k['d'])."</del> " : '')
                . (!empty($k['i']) ? "<ins style='color:green'>".implode(' ',$k['i'])."</ins> " : '');
      } else {
        $ret .= $k . ' ';
      }
    }
    return $ret;
  }

  $query = "
    SELECT
      `m`.`history`, `p`.`history` AS 'p_history',
      `m`.`personality`, `p`.`personality` AS 'p_personality',
      `m`.`description`, `p`.`description` AS 'p_description'
    FROM `members` `m`
    JOIN `profile_edit` `p` ON `p`.`userid` = `m`.`id`
    WHERE `m`.`id` = '".mysql_real_escape_string($id)."'
    LIMIT 1
  ";

  $result = mysql_query($query) or die(mysql_error());
  $data = mysql_fetch_assoc($result);

  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>History :: </td>
          <td>".htmlDiff(htmlspecialchars($data['history']), htmlspecialchars($data['p_history']))."</td>
        </tr>
        <tr bgcolor=\"#000000\">
          <td>Personality :: </td>
          <td>".htmlDiff(htmlspecialchars($data['personality']), htmlspecialchars($data['p_personality']))."</td>
        </tr>
        <tr bgcolor=\"#000000\">
          <td>Description :: </td>
          <td>".htmlDiff(htmlspecialchars($data['description']), htmlspecialchars($data['p_description']))."</td>
        </tr>
      </table>
    </form>";
like image 136
DaveRandom Avatar answered Nov 23 '25 08:11

DaveRandom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!