0

In php working with mysql, how can I output two columns values only once, and then loop thru the others.

Here:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
echo $row['category'];    //Only once
echo $row['subcategory']; //Only once

{
echo $row['title'];
echo $row['body'];
echo $row['views'];
}

Desired output:

Category->SubCategory //Outputted only once

Title,  //Title Body and Views depend on whatever's there in the db. Could be 10 or more
Body,
Views

Title,
Body,
Views

Title,
Body,
Views
1
  • add a flag to false out of the loop then to true after first display Commented Nov 26, 2012 at 6:18

4 Answers 4

3

This code shoud do the trick

    $exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
    $row_once = mysql_fetch_array($exe_fetch_data)
    echo $row_once['category'];    //Only once
    echo $row_once['subcategory']; //Only once

   while($row = mysql_fetch_array($exe_fetch_data)) {
    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
    }

You could also try using a flag whose value is initially true and will turn false after first iteration. But I think above code will be faster than using a flag as it wont compare/check flag value after each iteration and with a huge database it will save execution time.

Sign up to request clarification or add additional context in comments.

Comments

2

One solution might be a bool that remembers if the category has been outputed already:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$category_output_done = false;
while($row = mysql_fetch_array($exe_fetch_data))
{
    if (!$category_output_done) {
        echo $row['category'];    //Only once
        echo $row['subcategory']; //Only once
        $category_output_done = true;
    }

    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
}

Another solution could be a counter that increments on every iteration.

Comments

1

Add a simple counter-

$counter = 0;
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
if ($counter == 0){
echo $row['category'];    //Only once
echo $row['subcategory']; //Only once
$counter = 1;
}

Comments

1

As you might have lots of results, keeping a flag in your while loop whether you have displayed the categories already consumes CPU. As you are always showing the categories with the first result set I would not keep a flag in the while loop but do it like this:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$iNrOfResults = mysql_num_rows($exe_fetch_data);
//See if the result set has any rows at all, display the categories once
if(iNrOfResults > 0)
{
    $row = mysql_fetch_array($exe_fetch_data)
    echo $row['category'];    //Only once
    echo $row['subcategory']; //Only once
    echo $row['title'];
    echo $row['body'];
    echo $row['views'];
}

//See if there are any more results, do not display the categories anymore
if(iNrOfResults > 1)
{
    while($row = mysql_fetch_array($exe_fetch_data))
    {
        echo $row['title'];
        echo $row['body'];
        echo $row['views'];
    }
}

Comments

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.