Jump to content

Recommended Posts

I have the following code that searches my database and displays results in a table:

 

$fields = array("field1", "field2", "field3")
$cols   = implode (', ', $fields); 

$result= mysql_query 
(" SELECT $cols FROM tablename  WHERE ................... ");

if (!$result) {die('Could not search database: ' . mysql_error());}

if($result)
{   
    
    if(mysql_num_rows($result) == 0)
    {
        return "Sorry. No records found in the database";                       
    }
    else 
    {
        $table = "<table border='1' cellpadding='5' cellspacing='5'>";
        while($arr = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $table .= "\t\t<tr>\n";
            foreach ($arr as $val_col) 
            {
                $table .= "\t\t\t".'<td>'.$val_col.'</td>'."\n";
            }
            $table .= "\t\t</tr>\n";  
        }
        $table .= "</table>";
        echo $table;
    }
    mysql_free_result($result);       
}

 

As you can see each of the MySQL table fields specified by $fields is displayed in a new column in the html table.

I want to change this so that e.g. "field3" is displayed in a new row instead.

 

So, instead of the html table looking like:

 

| "field1-result1" | "field2-result1" | "field3-result1" |

| "field1-result2" | "field2-result2" | "field3-result2" |

| "field1-result3" | "field2-result3" | "field3-result3" |

 

I want it to look like:

 

| "field1-result1" | "field2-result1" | 

| "field3-result1" |                          |

| "field1-result2" | "field2-result2" |

| "field3-result2" |                          |

| "field1-result3" | "field2-result3" |

| "field3-result3" |                          |

 

I guess this is quite straightforward, but I can't work it out! Pls help!

 

Thanks.

 

 

 

Change your while statement to:

 

while($arr = mysql_fetch_array($result, MYSQL_NUM))
        {
            $table .= "\t\t<tr>\n";
            
                $table .= "\t\t\t".'<td>'.$arr[0].'</td>'."\n
                                 \t\t\t".'<td>'.$arr[1].'</td>
                                    </tr> 
                                     <tr>'.
                                "\n\t\t\t".'<td colspan="2">'.$arr[2].'</td>'."\n";
            
            $table .= "\t\t</tr>\n";  
        }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.