Hi
i have a mysql table :
each user have 4 emails
Please How to sort the row result horizentaly
like that
r937
April 18, 2018, 4:12pm
2
SELECT user_Id
, GROUP_CONCAT(email) AS emails
FROM tbl
GROUP
BY user_id
2 Likes
Hi, thank you verry much for your help, it work, but i have an other problem, the displayed resul in html table:
user_Id
email1
email2
email3
email4
11
emailx@email.com,emailc@email.com,emaild@email.com,emailv@email.com
12
emailr@email.com,emailb@email.com,emailW@email.com,emailT@email.com
13
emailJ@email.com,emailI@email.com,emailL@email.com,emailQ@email.com
how can i fix that
<table border="1" width="784">
<tr>
<td width="112">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">user_Id </span></code></pre>
</td>
<td width="478">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email1</span></code></pre>
</td>
<td width="50">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email2</span></code></pre>
</td>
<td width="53">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email3</span></code></pre>
</td>
<td width="57">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email4</span></code></pre>
</td>
</tr>
<tr>
<td width="112">11</td>
<td width="478">
<p>emailx@email.com,emailc@email.com,emaild@email.com,emailv@email.com</p>
</td>
<td width="50"> </td>
<td width="53"> </td>
<td width="57"> </td>
</tr>
<tr>
<td width="112">12</td>
<td width="478">
<p>emailr@email.com,emailb@email.com,emailW@email.com,emailT@email.com</p>
</td>
<td width="50"> </td>
<td width="53"> </td>
<td width="57"> </td>
</tr>
<tr>
<td width="112">13</td>
<td width="478">
<p>emailJ@email.com,emailI@email.com,emailL@email.com,emailQ@email.com</p>
</td>
<td width="50"> </td>
<td width="53"> </td>
<td width="57"> </td>
</tr>
</table>
Well, your OP asked how to get them “horizontally”, and the query for that was provided. Now you’re saying that isn’t what you need at all.
If I’m correctly guessing what you really want, it would be something like this:
[ id1 => [ email1, email2, email3, email4]]
, [ id2 => [ email1, email2, email3, email4]]
, [ id3 => [ email1, email2, email3, email4]]
or this:
[ id1, email1, email2, email3, email4]
, [ id2, email1, email2, email3, email4]
, [ id3, email1, email2, email3, email4]
Can you more clearly describe what it is you need?
Please, i need to display an html resul similar to this
<table border="1" width="648">
<tr>
<td width="112">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">user_Id </span></code></pre>
</td>
<td width="123">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email1</span></code></pre>
</td>
<td width="122">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email2</span></code></pre>
</td>
<td width="130">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email3</span></code></pre>
</td>
<td width="127">
<pre class="default prettyprint prettyprinted" style=""><code><span class="pln">email4</span></code></pre>
</td>
</tr>
<tr>
<td width="112">11</td>
<td width="123">
<p>emailx@email.com</p>
</td>
<td width="122">
<p>emailc@email.com</p>
</td>
<td width="130">
<p>emaild@email.com</p>
</td>
<td width="127">
<p>emailv@email.com</p>
</td>
</tr>
<tr>
<td width="112">12</td>
<td width="123">
<p>emailr@email.com</p>
</td>
<td width="122">
<p>emailb@email.com</p>
</td>
<td width="130">
<p>emailW@email.com</p>
</td>
<td width="127">
<p>emailT@email.com</p>
</td>
</tr>
<tr>
<td width="112">13</td>
<td width="123">
<p>emailJ@email.com</p>
</td>
<td width="122">
<p>emailI@email.com</p>
</td>
<td width="130">
<p>emailL@email.com</p>
</td>
<td width="127">
<p>emailQ@email.com</p>
</td>
</tr>
</table>
I got how you want the end results to be displayed. And I got that the database table has rows with user id and email fields. What’s missing is what you’re currently doing in between.
I’m again guessing, that you are using PHP to loop through row results to populate the HTML table with values. If so, please post your PHP code and I’ll move this to the PHP category since ATM it seems this is more about PHP than table queries.
r937
April 19, 2018, 8:58am
8
if this is how you want to print your data –
[code]
12
emailr@email.com
emailb@email.com
emailW@email.com
emailT@email.com
[/code]
then this is the query you need –
SELECT user_Id
, email
FROM tbl
ORDER
BY user_id
1 Like
mysql_select_db($database_liste, $liste);
$query_listecontact = "SELECT user_Id, email, GROUP_CONCAT(email) AS emails
FROM email WHERE categorie = '2' GROUP BY user_Id";
$listecontact = mysql_query($query_listecontact, $liste) or die(mysql_error());
$row_listecontact = mysql_fetch_assoc($listecontact);
$totalRows_listecontact = mysql_num_rows($listecontact);
<table width="200" border="1">
<tr>
<td>ID TRAJET</td>
<td>email1 </td>
<td>email2</td>
<td>email4</td>
<td>email4</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_listecontact['user_Id']; ?></td>
<?php
for( $i= 0 ; $i < 4 ; $i++ )
{
$email = $row_listecontact['email'];
echo '<td>' . $email[$i] . '-' . $i . '</td>';
}
?>
<?php } while ($row_listecontact = mysql_fetch_assoc($listecontact)); ?>
</tr>
</table>
1 Like
r937
April 20, 2018, 7:25pm
11
aladin16hostel:
Hi
Any help please
perhaps you should ask to have this thread moved to the php forum, becuase that’s probably where you need help, yes?
Other than the fact that the “mysql” functions are obsolete and not worth being used, are you sure that’s the query you want to use? (GROUP BY instead of ORDER BY)
I’m guessing the result rows you get are similar to this array
$example_array = [['user_id'=>'user1'
,'email'=>'email_u1e1'
,'emails'=>'email_u1e1,email_u1e2,email_u1e3,email_u1e4']
,['user_id'=>'user2'
,'email'=>'email_u2e1'
,'emails'=>'email_u2e1,email_u2e2,email_u2e3,email_u2e4']
];
Notice that “email” is an indeterminate value, so is most likely useless. i.e might as well remove the “, email” from the SELECT.
Then it’s simply a matter of doing something like this (psuedo-code)
foreach ($row ...) {
// do row HTML stuff
// use $row['userid']
$exploded_emails = explode($row['emails'])
foreach ($exploded_emails) {
// do more HTML stuff
....
system
Closed
July 21, 2018, 3:16am
13
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.