Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:20:02+00:00 2026-05-18T23:20:02+00:00

I am using a for loop to generate a table for a SQL query’s

  • 0

I am using a for loop to generate a table for a SQL query’s results. I am trying to alter the code so that new text is echoed into a specific column of the table (column 8)

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='0' id=resultTable><tr>";


for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td id" . $i;
    echo "></td>";
}

echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:20:03+00:00Added an answer on May 18, 2026 at 11:20 pm

    what actually do you mean by “new text” in “I am trying to alter the code so that new text is echoed into a specific column of the table”?

    Is “text” the name of a particular table field? If so, it would be better to alter the sequence of the fields in the mysql_query that you are using instead of doing any change in the html generation for this

    e.g.

    SELECT field1, field2, ... new_text from ... 
    

    Put new_text in the eighth position.

    Update
    If you want to display various things for various columns in the result table, you can put a counter and do what you want as per count like this:-

    // printing table rows
    while($row = mysql_fetch_row($result))
    {
        echo "<tr>";
    
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        $counter = 0;
        foreach($row as $cell)
        {
            if($counter == 7)
            {//eighth column
                echo "<td><img src='".$cell."' /></td>";
            }
            else if($counter == 5)
            {//sixth col
                echo //something else ...
            }
            else
                echo "<td>$cell</td>";
                //inside your default td
            $counter++;
        }
    
        echo "</tr>\n";
    }
    

    But it would be more readable,maintainable and customizable to have some config array where you specify which column is going to hold what like this:-

    function getColumnConfigs()
    {
       return $columnsConfigs = array(5=> "something_else", 7 => "img");
       //specify special cases for necessary columns. columns not mentioned here will take default.
    }
    

    and then use this in your iteration:-

     // printing table rows
    $columnsConfig = getColumnConfigs();
    while($row = mysql_fetch_row($result))
    {
        echo "<tr>";
    
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        $counter = 0;
        foreach($row as $cell)
        {
            if(isset($columnsConfig[$counter]))
            {
                switch($columnsConfig[$counter])
                {
                     case "img":
                                  $html = "<td><img src='".$cell."' /></td>";
                                  break;
                     case "something_else"
                                  $html = "<td><img src='".$cell."' /></td>";
                                  break;
                } 
            }
            else
            {//default
                $html = echo "<td>$cell</td>";
            }
    
        }
    
        echo "</tr>\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to construct an sql query using a while loop that increments
I'm using SQL Server 2K8 and have a table used solely to generate ids
I am trying to dynamically generate content using JSP. I have a <c:forEach> loop
I am Using LINQ to query a table that has a foreign key for
By using the code below in CodeIgniter , we can generate a table as
I'm using a loop to try to generate keyword combinations and also find the
I need to generate a ms-word document from a pl/sql query to export a
I am stuck on a query to generate a temporary table having values according
I am arranging a dynamic table of textboxes using a loop. Please review the
I am trying to generate a board (10X10) using the app engine templates and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.