Oke Tega Oke Tega -4 years ago 116
PHP Question

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?

Answer Source

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>";
  }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download