0

Hello here is my code :

<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>';

<?
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";   
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){  
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
echo '      <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
}
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>

the output of the code will be like .

<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<tr>
<td>1</td>
<td>test1@gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>test2@gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3@gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>test4@gmail.com</td>
<td>1</td>
</tr>
</table>

(1 = normal user , and 2 = admin for user level) but what I want is something .like that

<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<div id="users">
<tr>
<td>1</td>
<td>test1@gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>test4@gmail.com</td>
<td>1</td>
</tr>
</div>
<div id="admins">
<tr>
<td>2</td>
<td>test2@gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3@gmail.com</td>
<td>2</td>
</tr>
</div>
</table>

I want to add a div id="users" that will contain all users from database that have user_level = 1 and another div id="admins" for user_level = 2.

  • Order by `user_level` first, then all your admins will be in the later result. You can just echo `
    ` when you encounter the first admin.
    – chris85 Apr 01 '17 at 18:59
  • please take a quick look at [why you should immediately stop using mysql_*](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – hassan Apr 01 '17 at 19:04
  • Using DIV tags as immediate children of table tag is not a valid HTML syntax. – manian Apr 01 '17 at 19:11
  • @ManivannanSadasivam what should I use then please? – chris david Apr 01 '17 at 19:34
  • @chrisdavid, Use tbody to group table rows. I am on mobile. I will post the answer in 8 hours time if no one has posted until then – manian Apr 01 '17 at 19:41
  • ok sir I'm waiting for your answer because it seems no one has managed to help me – chris david Apr 01 '17 at 19:52
  • My comment should give you the answer. @chrisdavid If not then show what you tried and how it failed. – chris85 Apr 01 '17 at 19:57

2 Answers2

0

try the following code, you need to create div's based on conditions like if the iteration number 1 then insert div(open) before content and after content(close)...and the if iteration number is 3 then again insert the div(open) before content an after content(close)

   <table class="table table-hover table-bordered">
    <tr>
    <th class="well" style="text-align:center">ID</th>
    <th class="well" style="text-align:center">Email</th>
    <th class="well" style="text-align:center">User level</th>
    </tr>

    <?php
    $code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";   
    $code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
    $sql_rows = mysql_num_rows($code_query);
    if($sql_rows > 0){
        $i=0;  
    while($rows = mysql_fetch_object($code_query)){
      $user_id = intval($rows->user_id);
      $user_level= intval($rows->user_level);
      $user_email = htmlspecialchars($rows->user_email);
      if($i==0){?><div id='users'><?php }elseif($i==2){?><div id='admins'><?php }
      echo '      <tr>
      <td>'.$user_id.'</td>
      <td>'.$user_email.'</td>
      <td>'.$user_level.'</td>
      </tr>';
      if($i==0){?></div><?php }elseif($i==2){?></div><?php }
       $i++;
      }
      mysql_free_result($code_query);
      }else{
    echo '<tr>
    <td>
    <font color="red">no data found</font>
    </td>
    </tr>';
    }
    echo '</table>';
    ?>
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
0

Try the below code with tbody to group table rows,

<?php
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_level ASC, user_id ASC ";   
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows =        mysql_num_rows($code_query);
if($sql_rows > 0){  
  $newgroup = false;
  $groupid = array(1=>'users', 2=>'admins');
  while($rows = mysql_fetch_object($code_query)){
    $user_id = intval($rows->user_id);
    $user_level= intval($rows->user_level);
    $user_email = htmlspecialchars($rows->user_email);
    if($newgroup != $user_level) {
      if($newgroup != false) echo '</tbody>';
      echo '<tbody id="'.$groupid[$user_level].'">';
      $newgroup = $user_level;
    }
  echo '      <tr>
  <td>'.$user_id.'</td>
  <td>'.$user_email.'</td>
  <td>'.$user_level.'</td>
  </tr>';
}
echo '</tbody>';
mysql_free_result($code_query);
}else{
 echo '<tr>
  <td>
    <font color="red">no data found</font>
  </td>
</tr>';
}
echo '</table>';
?>

Note: It would work if you just want to group table rows. But if you want to add additional features like animation then you might need to add more css/jQuery.

Additional suggestions: 1. You should start using mysqli or PDO 2. You should start using css to replace font tags.

manian
  • 1,418
  • 2
  • 16
  • 32