Solved

Too much is printed.

Posted on 2011-03-11
7
289 Views
Last Modified: 2012-08-13
Given the code below
<?php
$dag = $dagen_1a;
//echo "<tr>";
$cols = 0;
$rows = 0;
while ($dag <= $sista_vakt_dagen)
{
$dag2 = ($dag - 75000);
//Print date (first print column)
echo "<tr><td valign=\"top\" height=\"20px\" class=\"vaktvisning\"><strong>".date('Y-m-d',$dag)."</strong></td>";
//---------------------------------------
$SQL1 = " SELECT * FROM medlem_vakt_test LEFT JOIN bryggplatser ON medlem_vakt_test.medlemsnr = bryggplatser.medlemsnr  WHERE (medlem_vakt_test.dagkod1 between '$dag2' AND '$dag') OR (medlem_vakt_test.dagkod2 between '$dag2' AND '$dag') ORDER BY medlem_vakt_test.dag_bev1 ASC ";
$ret1 = mysql_query($SQL1);
if (!$ret1) { echo( mysql_error()); }
else {
while ($row = mysql_fetch_array($ret1)) {
            $id = $row['id'];
            $fornamn = $row["fornamn"];
            $efternamn = $row["efternamn"];
            $medlemsnr = $row['medlemsnr'];
            $telnr = $row['telnr'];
            $mobilnr = $row['mobilnr'];
            $bryggplats = $row['bryggplatser.bryggplats'];
//Print mooring nr (second print column)
echo "<td><span class=\"style36\">$bryggplats</span></td>";
//Print yacht owners' first name (third print column)
echo "<td><span class=\"style36\">$fornamn</td>";
//Print yacht owners' last name (fourth print column)
echo "<td><span class=\"style36\">$efternamn</span></td>";
//Print yacht owners' telephone number (fifth print column)
echo "<td><span class=\"style36\">$telnr</span></td>";
//Print yacht owners' cell phone number (sixth print column)
echo "<td><span class=\"style36\">$mobilnr</span></td>";
echo "</tr>";
}}
$dag = $dag + 60*60*24;
}
?>


The expected printout:
2011-05-08          62            Peter               Taylor                       0752-745041     0708-148319
2011-05-09          90            Sergio             Gonzales                  0738-990711     0708-990711
2011-05-10          91            Sergio             Gonzales                  0738-990711     0708-990711
2011-05-11          254          John                Fitzgerald                 0719-123456
This is how it is printed:
2011-05-08          62            Peter               Taylor                       7450410             0708-148319
2011-05-09          90            Sergio             Gonzales                  0738-990711      0738-990711
91          Sergio      Gonzales       0738-990711           0738-990711            
2011-05-10          90            Sergio              Gonzales                  0738-990711       0738-990711
91          Sergio      Gonzales       0738-990711           0738-990711       
2011-05-11          254          John      Fitzgerald                 0719-123456      
When a name appears twice; i. e. on two dates, it comes up as doubles. That's not wanted.
How can I get it to print out the way I want it?
0
Comment
Question by:lericson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Randy Downs
ID: 35108673
Try using DISTINCT - Used to select unique records. Only unique values are returned.

SELECT DISTINCT(COLUMN) FROM TABLE

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35108747
you shall want to use GROUP BY for your query ...

an interesting article to read in that context: http://www.experts-exchange.com/A_3203.html
0
 

Author Comment

by:lericson
ID: 35109537
angelIII,
Thanks for your reply.
With this SQL sentence:
$SQL1 = " SELECT * FROM medlem_vakt_test LEFT JOIN bryggplatser ON medlem_vakt_test.medlemsnr = bryggplatser.medlemsnr  WHERE (medlem_vakt_test.dagkod1 between '$dag2' AND '$dag') OR (medlem_vakt_test.dagkod2 between '$dag2' AND '$dag') GROUP BY medlem_vakt_test.dag_bev1, medlem_vakt_test.dag_bev2 ";
I get
2011-05-08 62 Peter Taylor   7450410 0708-148319
2011-05-09 90 Sergio Gonzales  0738-990711 0738-990711
2011-05-10 90 Sergio Gonzales  0738-990711 0738-990711
which is not wrong in itself, but I'd like to have the last line above like
2011-05-10 91 Sergio Gonzales 0738-990711  0738-990711
I. e. second column 91 instead of 90.
How could I achieve that?
0
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35109621
then you are grouping by the wrong columns, or not enough.
what column is that 90/91 coming from? add that to the group by ...
0
 

Author Comment

by:lericson
ID: 35109800
The column 90/91 is coming from is bryggpltser.bryggplats
If I group by bryggpltser.bryggplats, I get this outprint:
2011-05-08 62 Peter Taylor7450410 0708-148319
2011-05-09 90 Sergio Gonzales  0738-990711 0738-990711
91 Sergio Gonzales 0738-990711 0738-990711
2011-05-10 90 Sergio Gonzales 0738-990711 0738-990711
91 Sergio Gonzales 0738-990711 0738-990711
It is repeated twice. Please, give me an advice.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35116873
>Please, give me an advice.
well, actually, I gave you some advice in giving the link to the article which describes the issue in general.

you need to find why it is "duplicated", aka which of the columns in the group by you need to remove to avoid what you consider a duplicate.
0
 

Author Closing Comment

by:lericson
ID: 35117768
Thanks! Excellent article.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 82
What is key combo for a b with a forward slash through it? 12 66
Conditional Array Element 7 28
MySQL_Development_Traininng.. 10 22
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question