简体   繁体   中英

Display row in database with same data in particular div

Lets say I have data in my database with some rows having the same data.

id|value|message
 1|001.  |one
 2|001.  |five
 3|002.  |four
 4|001.  |hello
 5|001.  |sup
 6|002.  |sure?

Is it possible i echo all message data with 001 together in one <div class="display"></div> and those with 002 with the same <div class="display></div> There by having two <div class="display"></div> display the count of the two different values.

I have tried with PHP using

$sql =<<<EOF
SELECT value, COUNT(value) AS NumOccurrences FROM table WHERE id != '$log_id' GROUP BY value, message ORDER BY id DESC; 
EOF;
$ret = $db->query($sql);

while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
    $value = $row['value'];

    echo "<div class='display'>$value</div>";
}    

But the above code echo six <div class="display"></div> . That is each div for each value

<div class="display">001</div>
<div class="display">001</div>
<div class="display">002</div>
<div class="display">001</div>
<div class="display">001</div>
<div class="display">002</div>

But i want something like this

<div class="display">001001001001</div>
<div class="display">002002</div>

Please I am new to PHP and searched hard for an answer. Is this possible?

Without changing query you can do something like that:

  $by_val = array();
  while ($row = $ret->fetchArray(SQLITE3_ASSOC))
  {
      $value = $row['value'];
      if(!isSet($by_val[$value])) $by_val[$value] = '';
      $by_val[$value] .= $value;
  }

  foreach($by_val as $key => $str) {
    echo "<div class='display'>".$str."</div>";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM