1

Firstly, I am PHP-tarded and appreciate any help. So, thanks in advance.

Goal: To echo query results and delete duplicated "company" results. I have a table with the following columns: id, Installer, company, address, phone, endorsements, parish

The "id" is the primary key, the "installer" is the first and last name of an individual excavation equipment operator and "company" is the company the installer works for, "parish" is like a county... but I am in Louisiana..so parish. I have a drop-down menu with all the parishes listed. When a parish is selected, all the installers and their associated companies is displayed on a web page. My problem is that one company can have multiple installers which causes the companies to be listed multiple times. I would like each company to be listed only one time.

Here is my PHP code:


getuser.php


<?php
$q=$_GET["q"];

$con = mysql_connect('000.000.000.000', 'shanehward', '********');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("shanehward", $con);

$sql="SELECT * FROM installer_list WHERE Parish = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<thead>
<tr>
<th>Company</th>
<th>Address</th>
<th>City</th>
<th>Endorsements</th>
<th>Phone</th>
</tr>
</thead>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['City'] . "</td>";
  echo "<td>" . $row['Endorsements'] . "</td>";
  echo "<td>" . $row['Phone'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

Thanks again for any input.

4
  • On another note, you should consider normalizing your database just a bit, to reduce the redundancies, such as companies. Obviously it will take a little bit of work on your part to do this correctly and be sure to fully back up your database before you do. You will also need a thorough understanding of your code specifically in regards to all the queries in your code to make sure you don't break anything but I will make your life a bit easier in the long run. just my two cents. Commented Mar 14, 2012 at 20:08
  • I agree with @JGibson about needing to normalise your db. On a different note, your approach needs clarifying. If you have these two installers for company A what data will you be displaying - (1, Installer 1, Company A, 23 some road, city, 0123456789, endorsements, parish), (2, Installer 2, Company A, 32 another road, city, 9876543210, endorsements, parish). So, when displaying Company A is address "23 some road" or "32 another road"? Or are all the attributes common to the company, not the installer? Commented Mar 14, 2012 at 21:54
  • Responding to nnichols - My website is www.sewer-hub.com. The "Installer Loc" page is an installer locater for a given area (parish). An installation company can have several installers and these installers move from company to company or form new companies. I have entered the installers individually under whatever company they are currently employed with. Commented Mar 14, 2012 at 23:18
  • I have to keep track on installer id's - a state regulated thing - so I created my table so that I could simply change the company name for a given installer as the installers' employers change. Go to www.sewer-hub.com and go to the installerpage (www.sewer-hub.com/installer.html) and select a parish. You will see several company names repeated because the companies may have several employees (installers). If you wan t me to, I can change what is presented on the website so that you can see the individual installers names... if that will help you. Commented Mar 14, 2012 at 23:19

2 Answers 2

3

You could try this query

SELECT DISTINCT company
FROM your_table
WHERE parish = selected_parish

or (if you need something more complex)

SELECT company, COUNT(installer) as tot
FROM your_table
WHERE parish = selected_parish
GROUP BY company
Sign up to request clarification or add additional context in comments.

Comments

0

You can do a GROUP BY to eliminate "duplicates".

$sql="SELECT * FROM installer_list WHERE Parish = '".$q."' GROUP BY company";

1 Comment

It worked. I had the data on one company input twice which was also throwing me off. Thanks for your input. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.