Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    120
    Thanks
    1
    Thanked 0 Times in 0 Posts

    loop trought sql without repeat results

    Hello

    I have a mysql database that looks like this:

    id_name - phone number - email
    1 - 927567348 - lalala@gmail.com
    1 - 965437823 - lalala@gmail.com
    1 - 963423495 - lalala@gmail.com
    2 - 4357986345 - bubu@hotmail.com
    2 - 4395874355 - bubu@hotmail.com
    3 - 3459875234 - ghjfgh@mail.com

    and now i want to print it like this:

    1
    927567348
    965437823
    963423495
    lalala@gmail.com

    2
    4357986345
    4395874355
    bubu@hotmail.com

    3
    3459875234
    ghjfgh@mail.com


    I don't know how do this.

    if i didn't needed to print the email in the end it would be "simple"

    like this:
    Code:
    <?php
    $id_name = $row_rs_livros['id_name'];
    while ($row_rs_livros = mysql_fetch_assoc($rs_livros)) {
      if ($iddolivro != $row_rs_livros['id_name']) {
       echo $row_rs_livros['id_name'];
       }
      echo $row_rs_livros['phonenumber'];
      $iddolivro = $row_rs_livros['id_li'];
      }
    ?>
    But in this case i'm lost.

    Anyone can help me please?

    Thanks

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    I'm a bit rust here but in your sql query, I think you might want to use "group by `email`". That would then return the results grouped by the email addy and then you can print the email addy at the bottom when the next rows email addy is different.

    Again, not sure, been a while since I've needed to group anything.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    120
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi

    I just realized that my example was too simple for my final needs

    In my real example this tables have books and authors.

    some books have more then 1 author..

    and i need to print it like this:

    Code:
    <div id="contentdiv">
    <?php
    $i = 0;
    do {
    ?>
    <?php                
    $i = $i + 1;
    ?>
                 <div id="contentdiv<?php echo $row_rs_livros2['id_li']; ?>" <?php if ($i <= 1) { echo "style='display: block;'"; } else { echo "style='display: none;'"; }?>>
                     <a href="livros.php?id_li=<?php echo $row_rs_livros2['id_li']; ?>" class="titulos_vermelho_livros"><?php echo $row_rs_livros2['livro']; ?></a><br/><br/>
                    
                    
                     <?php //here i'll have more then 1 author sometimes ?>
                     <span class="texto"><a href="autores.php?id=<?php echo $row_rs_livros2['id']; ?>" class="titulos_livros"><?php echo $row_rs_livros2['nome']; ?><?php echo $row_rs_livros2['apelido']; ?></a><br>
        
                    
                    
         <?php    
         mb_internal_encoding("UTF-8");
         $str = $row_rs_livros2['texto'];
         $limit = 300; //Specify the length of the new substring
         if (substr($str, $limit, 1) != ' ' && ($l = mb_strrpos(mb_substr($str, 0, $limit), ' '))) {
             echo mb_substr($str, 0, $l);
         } else {
             echo mb_substr($str, 0, $limit);
         }
         ?>(...)<br />
        
                     <a href="livros.php?id_li=<?php echo $row_rs_livros2['id_li']; ?>" class="style3">ver mais »</a></span></div>
                     <?php } while ($row_rs_livros2 = mysql_fetch_assoc($rs_livros2)); ?>
    </div>
    </div>
    at the moment my query is still this:
    SELECT autores.id, autores.nome, autores.apelido, livros.id_li, livros.texto, livros.pdfs, livros.foto, livros.foto_grande, livros.livro, livros.ordem, ids.id_livro FROM autores INNER JOIN ids ON autores.id=ids.id_autores INNER JOIN livros ON ids.id_livro=livros.id_li WHERE estado = 'on' ORDER BY livros.ordem


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •