Group By Result?

Here is the code I have so far. It extracts the 2nd word of a set of model numbers. Trouble is with this is there are multiple same model number results. How can I acquire just the one result? I can’t use GROUP on the statement as the names can be different yet the model number the same. I somehow need to apply a limit to the rest of the code.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$querytp1 = $_GET['querytp1']; 

echo $querytp1;

// Make a MySQL Connection
include('../ukshop/con.php');
$conDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('Error: Could not connect to database.');

echo '<br>Con Made<br>';

if ($querytp1 > "") {

$statement = mysqli_query($conDB,"SELECT * from affiliSt_products1 WHERE prodName like '%$querytp1%' AND prodCategory like '%television%' order by prodName ");

if (mysqli_num_rows($statement) != 0) {
  
	// displaying records.
    while ($row = mysqli_fetch_array($statement)) {

$prodName = $row['prodName'];
$tagWords = $prodName;

$tagWords = explode(" ", $tagWords);

if ($tagWords[0] > "") {
$querytp7 = $tagWords[1];

}

if (strpos($querytp7, 'U') === 0) {
   // It starts with 'U'

echo '<div style="float: left">';
echo 'Model: ';
echo '<a href="index.php?querytp1=';
echo $querytp7;
echo '">';
echo $querytp7;
echo '</a>';
echo '</div>';

echo '<div style="float: left; padding-left: 10px;">';
echo 'Screen Size: ';
echo substr("$querytp7", 2, 2);  // TV Size
echo '</div>';

echo '<div style="clear: both;"></div>';

}
}
}
}

Cheers

Some sample of the data, and the required output, might make it easier to visualise exactly what you’re asking for.

Presuming that $querytp7 is the bit you’re trying to group, could you perhaps use the value of that string as an array key and build an array, then display the array?

$xx = array;
while (fetch_array()) { 
  explode description
  $modelnumber = whatever
  $xx[$modelnumber] = "1";
  }
foreach ($xx as $y) { 
  // display $y and link and so on
}

Incidentally, you only set the value of $querytp7 when there’s something in $tagwords[0], which gives you the chance that you may re-display the results from the previous row. You should blank it at the start of the loop, or add an ‘else’ clause.

1 Like

Thanks for that. Looking at the above I think it will work, but in the meantime I have had a bit of a brain wave. Instead of attempting to use the data as is I could feed the results back into a database and then run another select group.

As for what the code is attempting to do, it is just extracting model numbers from a title and then creating a search link for a shopping script. Speed it not that important as the output will be inserted into a static html page.

Cheers

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.