0

I use a while loop to echo member data to display each member's listing. I then use Jquery to filter the listings according to value(s) which are echoed from the items field. (i.e. the items field may contain "item1 item3 item10").This works well!!! Here's the code I have now.

<?php
$area = "Richmond";
$db_connect = @mysqli_connect("localhost", "user", "password","database");

    if (mysqli_connect_errno($db_connect)) { 
        echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
        }

$result = @mysqli_query($db_connect, "SELECT * FROM database WHERE territories LIKE '%$area%'");

    if (!$result){
        echo ("<p>Error performing listing query:" . mysql_error() . "</p>");
        }

$items = array();

while ($row = mysqli_fetch_array($result)){
    // Jquery uses these "$row["items"]" for it's filter
    $items[] = "<li class=\"services " . $row["items"] . "\">
    // more code here used to construct member's listing
    }
?>
//In the html, I echo each member's completed listing...
<?php echo $items[0]; echo $items[1]; echo $items[2]; etc... ?>

How can I set an echo limit to 12 duplicate value(s) from the items field? There are only 12 listings available for each value in items. So I want only the first 12 of each value to be included in the echo. If a member cancels, then items value(s) from next member will be echoed automatically. I hope this makes sense! :D

[EDIT] For more clarity, lets say 12 members list for "item1 item2" (both are in the items field). With the current code, the html for each listing would read <li class="services item1 item2"></li>.

Then a 13th member lists his services of "item1 item2 item3". Since "item1" and "item2" exist 12 times, only item3 should echo. This new member's listing should read <li class="services item3"></li>. If a previous member cancels, all three items would be echoed.

I picture something like if COUNT(items=%item1%) <= 12) or something similar... I can't seem to wrap my mind around using GROUP BY or DISTINCT with my current set up.

bcintegrity
  • 213
  • 2
  • 11
  • 1
    refine your Query, try using DISTINCT http://stackoverflow.com/questions/7250566/mysql-select-distinct – esdebon Mar 03 '14 at 02:36

2 Answers2

0

In your database call, you can use GROUP BY and DISTINCT to only return one row for that value. Also, if you were to add a check in the SELECT for whatever your cancellation flag is, that would remove all cancelled members from being pulled back anyways. Something like

SELECT DISTINCT * FROM Member WHERE IsActive = 'true' LIMIT 12

Without more information, this is the best I can come up with.

Bardicer
  • 1,405
  • 4
  • 26
  • 43
  • I edited my original post for clarity. Here is a test page to see a working example. [My Testpage](http://integritycontractingofva.com/asqlfile.php). Click the blue "Find a Professional" button to see the listings. – bcintegrity Mar 03 '14 at 04:03
0

I finally got it. The first thing I had to do was create a different column for each item. Then it was a matter of counting each item from each row, allowing only the first twelve. I added the following:

$item1cnt = 0; //I added this section before the while loop to start the count at "0" for each item
$item2cnt = 0;
$item3cnt = 0;
$item4cnt = 0;
$item5cnt = 0;
$item6cnt = 0;
$item7cnt = 0;
$item8cnt = 0;
$item9cnt = 0;
$item10cnt = 0;
$item11cnt = 0;
$item12cnt = 0;

//And this inside the while loop to return only the first 12 of each item
if($row["item1"]!=""){ $item1cnt++; if($item1cnt > 12){$row["item1"]="";}}
if($row["item2"]!=""){ $item2cnt++; if($item2cnt > 12){$row["item2"]="";}}
if($row["item3"]!=""){ $item3cnt++; if($item3cnt > 12){$row["item3"]="";}}
if($row["item4"]!=""){ $item4cnt++; if($item4cnt > 12){$row["item4"]="";}}
if($row["item5"]!=""){ $item5cnt++; if($item5cnt > 12){$row["item5"]="";}}
if($row["item6"]!=""){ $item6cnt++; if($item6cnt > 12){$row["item6"]="";}}
if($row["item7"]!=""){ $item7cnt++; if($item7cnt > 12){$row["item7"]="";}}
if($row["item8"]!=""){ $item8cnt++; if($item8cnt > 12){$row["item8"]="";}}
if($row["item9"]!=""){ $item9cnt++; if($item9cnt > 12){$row["item9"]="";}}
if($row["item10"]!=""){ $item10cnt++; if($item10cnt > 12){$row["item10"]="";}}
if($row["item11"]!=""){ $item11cnt++; if($item11cnt > 12){$row["item11"]="";}}
if($row["item12"]!=""){ $item12cnt++; if($item12cnt > 12){$row["item12"]="";}}
bcintegrity
  • 213
  • 2
  • 11