Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2012, 06:35 PM   PM User | #1
alapimba
Regular Coder

 
Join Date: Jul 2006
Posts: 117
Thanks: 1
Thanked 0 Times in 0 Posts
alapimba is an unknown quantity at this point
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
alapimba is offline   Reply With Quote
Old 11-22-2012, 06:46 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,513
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 11-23-2012, 10:24 AM   PM User | #3
alapimba
Regular Coder

 
Join Date: Jul 2006
Posts: 117
Thanks: 1
Thanked 0 Times in 0 Posts
alapimba is an unknown quantity at this point
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
alapimba is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 AM.


Advertisement
Log in to turn off these ads.