I have the below code that grabs all the data I need, but as there are duplicate names in each row, I only want to display this once as a heading, and then display the array.

<?php
echo "<h1>Results Summary</h1>";
//TABLE RESULTS
$psba = $_POST['psba'];
$con = mysql_connect("localhost","root","");
mysql_select_db('project', $con) or die('Cant Connect');
$result = mysql_query("SELECT * FROM mpf WHERE psba = '".$psba."'");
echo "<h1>".$psba."</h1><br />";
while($row = mysql_fetch_array($result))
{
$pair = $row['pair'];
$project =$row['project'];
$psba = $row['psba'];
$hostname = $row['name'];
$circuit = $row['circuit'];
echo $pair;
echo $hostname;
echo $circuit."<br />";
}
?>

so shows this:

1Chepstow Community Hospital CCHLLSW0508002
2Chepstow Community Hospital CCHLLSW0508004
3Chepstow Community Hospital CCHLLSW0508001
4Chepstow Community Hospital CCHLLSW0508003

the first row and last row are the ones I need, but I have to have the hostname in the array, otherwise I cant display it?

I need it too grab the data, display $hostname as a heading, then the $pair and $circuit in the loop..

I cant seem to work it out,

thanks

Well if you need only one row, using the mysql extension you could do the following:

<?php
$query = 'SELECT something 
    FROM table 
    ORDER BY id DESC
    LIMIT 1';
$result = mysql_query($query) or die(mysql_error());
$fetch = mysql_fetch_assoc($result); // Contains only the first row, as we're not using it in a loop.

// $fetch now contains the first row. We have added a ORDER BY to your query and a LIMIT 1.
// This ensures that only one result, and, because of the ORDER BY, only the most recent
// result will be fetched (if "id" is your auto_increasing primary key field).

What your while() loop actually does is traverse to your result set and, after each loop execution, place the internal marker (which points to which result of the result set is being used) one further.

So, if your result sets contains 5 rows, for example, what it does is:

while(...)
{
    // Do stuff with first record.

    // ...

   // Loop finishes, on to the next record! 
   // This continues until the internal point can go no further, which is when the 5th item has been parsed. Your $fetch would then contain the data of the last element in the returned records set.

}

Is this of any help? I'm not 100% sure I fully understand what you want to do :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.