We help IT Professionals succeed at work.
Get Started

MySQL - How do I Display query results in 3 columns

rlb1
rlb1 asked
on
1,670 Views
Last Modified: 2013-12-12
Experts,

How do I display query results in 3 columns?  I have several of the results showing up over and over again.  Here are the results and the query:  (I have tried DISTINCT and some other things and cannot get just one result of each category.)  Thanks for your help!!
RESULTS:
Cable Management Panels & Racks Cable Management Panels & Racks Cable Management Panels & Racks Cable Management Panels & Racks Modular & Telephone Modular & Telephone Cable Ties - Standard Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire Cat6 - PVC (Solid & Stranded) Composite (Multi-Signal) Modular & Telephone Modular & Telephone Modular & Telephone Modular & Telephone Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire Bulk Speaker Wire RG6 RG6 RG6 RG6 RG6 RG6 RG6 RG6 RG6 RG6 Cable Ties - Screw-Mountable Cable Ties - Screw-Mountable Wraps Wraps Flat Ribbon Flat Ribbon Multi-Conductor Multi-Conductor Multi-Conductor Multi-Conductor Multi-Conductor Multi-Conductor

QUERY:  
$SearchResult=mysql_query("SELECT DISTINCT * FROM `products` WHERE `Category` = '$SearchString'") or die(mysql_error());

While($row2 = mysql_fetch_object($SearchResult)) {
Echo $row2->Subcategory . "  ";
}

<form name="form" action="cat.php" method="get">
<!--<input type="hidden" name="p" value=''/>-->
 
 
 
 
<SELECT NAME=p>
<option>SELECT CATEGORY</option>
<option value='Adapters & Couplers'>Adapters & Couplers</option>
<option value='Audio Video Cables'>Audio Video Cables</option>
<option value='Audio Video Devices'>Audio Video Devices</option>
<option value='Bulk Cable'>Bulk Cable</option>
<option value='Cat5e & Cat6 Cables'>Cat5e & Cat6 Cables</option>
<option value='Connectors'>Connectors</option>
<option value='DVI'>DVI</option>
<option value='Fiber Cables'>Fiber Cables</option>
<option value='HDMI'>HDMI</option>
<option value='Home Distribution'>Home Distribution</option>
<option value='KVM Cables & Switches'>KVM Cables & Switches</option>
<option value='Monitor Cables'>Monitor Cables</option>
<option value='Network Cables'>Network Cables</option>
<option value='Network Devices'>Network Devices</option>
<option value='PC Cables'>PC Cables</option>
<option value='Power Cords & Chargers'>Power Cords & Chargers</option>
<option value='Retail Items'>Retail Items</option>
<option value='SCSI'>SCSI</option>
<option value='Tools & Testers'>Tools & Testers</option>
<option value='USB'>USB</option>
<option value='VGA'>VGA</option>
<option value='Wall Plates'>Wall Plates</option>
</select>
<input type="submit" name="Submit" value="Search" />
</form>
 
 
 
 
 
 
 
<?
//Connect to DB
mysql_connect("       ","         ","        ") or die("Unable to connect to SQL server");
mysql_select_db("         ") or die("Unable to SELECT DB");
 
$Limit = 10; //Number of results per page
$SearchString=$_POST["p"]; // Get the search term
If($SearchString == "") $SearchString=$_GET["p"]; // Get the search term
If($SearchString == "") {
Echo"No results found for $SearchString <BR> Please enter a valid search term";
exit();
}
$page=$_GET["page"]; //Get the page number to show
If($page == "") $page=1; //If no page number is set, the default page is 1
 
 
 
// NEED ASSISTANCE HERE
 
 
$SearchResult=mysql_query("SELECT DISTINCT * FROM `products` WHERE `Category` = '$SearchString'") or die(mysql_error());
 
While($row2 = mysql_fetch_object($SearchResult)) {
Echo $row2->Subcategory . "  ";
}
 
 
 
 
 
//Get the number of results
$SearchResult=mysql_query("SELECT * FROM `products` WHERE `Category` = '$SearchString'") or die(mysql_error());
$NumberOfResults=mysql_num_rows($SearchResult);
 
 
 
//Get the number of pages
$NumberOfPages = (ceil($NumberOfResults/$Limit)>=20) ? 8 : ceil($NumberOfResults/$Limit);
 
$SearchResult=mysql_query("SELECT * FROM `products` WHERE `Category` = '$SearchString' LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
 
 
 
 
 
 
 
Echo $NumberOfResults . "  ";
Echo "results for";
Echo "  ";
Echo $SearchString;
Echo "<BR><BR>";
 
 
 
 
$Nav="";
If($page > 1) {
$Nav .= "<A HREF=\"cat.php?page=" . ($page-1) . "&p=" .urlencode($SearchString) . "\"><< Prev </A>&nbsp;&nbsp;&nbsp;";
}
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= "<font size=+1><B>&nbsp;$i&nbsp;</B></FONT>";
}Else{
$Nav .= "<A HREF=\"cat.php?page=" . $i . "&p=" .urlencode($SearchString) . "\"> $i </A>";
}
}
If($page < $NumberOfPages) {
$Nav .= "&nbsp;&nbsp;&nbsp;<A HREF=\"cat.php?page=" . ($page+1) . "&p=" .urlencode($SearchString) . "\">Next >> </A>";
}
 
Echo $Nav;
Echo "<BR><BR>";
 
 
While($row = mysql_fetch_object($SearchResult)) {
 
 
 
Echo "<form name='form' action='product.php' method='GET'>";
Echo "<input type='hidden' name='UPC' value=".$row->UPC." />";
Echo $row->ProductName . "  ";
Echo $row->Sku . "  ";
Echo $row->UPC . "  ";
Echo "<img src=";
Echo "http://www.cablestogo.com/assets/product_images/category_favorites_images/";
Echo $row->PictureSku;
Echo ".jpg>";
Echo "<input type='submit' name='Submit' value='More Info' />";
Echo "<HR width=500>";
Echo "</form>";
}
 
Echo "<BR><BR>" . $Nav;
 
?>

Open in new window

Comment
Watch Question
Web Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
This problem has been solved!
Unlock 2 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE