Jump to content

Colums and rows


ansipants

Recommended Posts

I Have a page that will display the data that was posted into a database, but it displays it like this:

 

id logoname locationname address city state zip phone website etc...

 

I want to display the data like this:

 

id <--- this will repeat records 4 columns across

logoname

locationname

etc...

 

id <--- this will repeat records 4 columns across

logoname

locationname

etc...

 

and then repeat similar for the rest of the data.

Sorry Im a n00b, but trying. Here is the current code:

 

<?php

 

include "config.php";

$con = mysql_connect("$dbhost","$dbusr","$dbpass");

if (!$con)

{

die('Could not connect:'. mysql_error());

}

mysql_select_db("$dbname",$con);

 

$q="select * from venues";

$result=mysql_query($q);

 

if (!$result) {

    die("Query to show fields from table failed");

}

 

$fields_num = mysql_num_fields($result);

 

echo "<table border='0'><tr>";

for($i=0; $i<$fields_num; $i++)

{

    $field = mysql_fetch_field($result);

    echo "<td></td>";

}

echo "</tr>\n";

 

while($row = mysql_fetch_row($result))

{

    echo "<tr>";

 

 

    foreach($row as $cell)

        echo "<td>$cell</td>";

 

    echo "</tr>\n";

}

mysql_free_result($result);

?>

 

Help would be greatly appreciated and an explanation so that I don't have to keep asking  :D

Link to comment
https://forums.phpfreaks.com/topic/247859-colums-and-rows/
Share on other sites

I 'THINK' I understand what you are wanting. When using SELECT * it will select all and basically show a row at a time.... you are wanting to show a column at a time right? If so use the following SQL.

 

SELECT logonname FROM venues;

 

And then rinse and repeat for the other columns :)

 

Hope this helps

 

--LiquidFusi0n

Link to comment
https://forums.phpfreaks.com/topic/247859-colums-and-rows/#findComment-1272876
Share on other sites

I think he is wanting to display the records four across, then start a new row. I.e.

Record 1 | Record 2  | Record 3  | Record 4
--------------------------------------------
Record 6 | Record 6  | Record 7  | Record 8
--------------------------------------------
Record 9 | Record 10 | Record 11 | Record 12

 

 

Here you go. Made the number of columns configurable using the variable $max_columns. Also cleaned up some of the logic.

<?php

include "config.php";
$con = mysql_connect($dbhost, $dbusr, $dbpass);
if (!$con)
{
    die('Could not connect:'. mysql_error());
}
mysql_select_db($dbname, $con);

$query = "SELECT * FROM venues";
$result = mysql_query($query);

$output = '';
if (!$result)
{
    $output .= "<tr><td>Query to show fields from table failed</td></tr>\n";
}
elseif(mysql_num_rows($result)==0)
{
    $output .= "<tr><td>No records returned</td></tr>\n";
}
else
{
    $max_columns = 4;
    $record_count = 0;
    while($row = mysql_fetch_assoc($result))
    {
        $record_count++;
        //Start new row if 1st record or $max_columns
        if($record_count%$max_columns==1)
        {
            $output .= "<tr>\n";
        }

        //Display record
        $output .= "<td>{$row['id']}</td>\n";
        $output .= "<td>{$row['logonname']}</td>\n";
        $output .= "<td>{$row['locationname']}</td>\n";
        //Add additional fields as needed

        //Close row if last record or $max_columns
        if($record_count%$max_columns==0)
        {
            $output .= "</tr>\n";
        }
    }
    //Close last row if still open
    if($record_count%$max_columns!=0)
    {
        $output .= "</tr>\n";
    }
}

?>
<table>
  <?php echo $output; ?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/247859-colums-and-rows/#findComment-1272917
Share on other sites

Ok, so I got it to print downward instead of across for the records, which is fine, but it doesn't start a new column after 4

Show the code you are currently using.

Edit: Bah! I see what I did. Just change this

        $output .= "<td>{$row['id']}</td>\n";
        $output .= "<td>{$row['logonname']}</td>\n";
        $output .= "<td>{$row['locationname']}</td>\n";

 

To this:

        $output .= "<td>\n";
        $output .= "{$row['id']}<br>\n";
        $output .= "{$row['logonname']}<br>\n";
        $output .= "{$row['locationname']}<br>\n";
        $output .= "</td>\n";

Link to comment
https://forums.phpfreaks.com/topic/247859-colums-and-rows/#findComment-1273104
Share on other sites

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.