Jump to content

[SOLVED] Make results Vertical Not Horizantal


dezkit

Recommended Posts

<table border=0 cellpadding=8>
<tr>
   <td>Name:</td>
   <td>--</td>
</tr>
<tr>
   <td>Date:</td>
   <td>--</td>
</tr>
<tr>
   <td>Description:</td>
   <td>--</td>
</tr>
<tr>
   <td>Image:</td>
   <td>--</td>
</tr>
</table>

<table border=0 cellpadding=8>
<tr>
   <td>Name:</td>
   <td>--</td>
</tr>
<tr>
   <td>Date:</td>
   <td>--</td>
</tr>
<tr>
   <td>Description:</td>
   <td>--</td>
</tr>
<tr>
   <td>Image:</td>
   <td>--</td>
</tr>
</table>

 

This is your answer, it is a html table that will display a vertical structure.

*Clears throat*

 

This is horizantal:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM example") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>"; 
echo $row['age'];
echo "</td></tr>"; 
} 

echo "</table>";
?>

 

How do I make it vertical? >.>

ah thats better :P, some code ;).

 

You must change this part:

<?php
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['name'];
	echo "</td><td>"; 
	echo $row['age'];
	echo "</td></tr>"; 
} 

echo "</table>";
?>

 

This gives you a table row with 2 columns for each result, you want 2 table rows with 2 columns each, so you should modify:

 

 

<?php
echo "<table border='1'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>Name:</td> <td>".$row['name']."</td></tr>"; 
	echo "<tr><td>Age:</td> <td>".$row['age']."</td></tr>"; 
} 

echo "</table>";
?>

 

Something like this? :)

 

Hope this helps,

Or do you want them displayed like this?

| Name:        | Name1  | Name2  | Name3  |
| Date:        | Date1  | Date2  | Date3  |
| Description: | Descr1 | Descr2 | Descr3 |
| Image:       | Image1 | Image2 | Image3 |

 

<?php

//Query the DB
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());

//Populate results into an array
while($row = mysql_fetch_assoc($result))
{
    $result_ary['Name'][]        = $row['name'];
    $result_ary['Date'][]        = $row['date'];
    $result_ary['Description'][] = $row['description'];
    $result_ary['Image'][]       = $row['image'];
} 

//Populate table horizontally
echo "<table border='1'>\n";
foreach($result_ary as $field_name => $field_values)
{
    echo "<tr>\n";
    echo "<th>{$field_name}</th>";
    foreach($field_values as $value)
    {
        echo "<td>{$value}</td>";
    }
    echo "</tr>\n";
}
echo "</table>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.