Jump to content

multiple columns and rows


Alicia

Recommended Posts

Hi guys,

 

can somebody give me an idea or anywherer i can find a reference how to output the records i fetched from the database in multiple columns and rows ?

 

I was able to output the records in multiple columns but only in a row.,, it doesnt really add another row by itself..

 

please advise where to find a solution for this.. thanks,

Link to comment
https://forums.phpfreaks.com/topic/132977-multiple-columns-and-rows/
Share on other sites

use a while loop .........

 

if u need to do anythink more then post your database stucture

or read up your self on mysql joins

 


<?php

//database connection

$sql="select * from what_ever";

$result=mysql_query($sql)or die(mysql_error());

while($data=mysql_fetch_assoc($result)){

echo " <br><br> ".$date['ehat_ever_info']." <br><br>";

}
?>

Here is a very simple example:

 

<?php
// database connection information
$hostname = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';

// open database
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database) or die("Unable to select database");

// query database
$query = "SELECT column1, column2 FROM table";
$result = mysql_query($query) or die(mysql_error());

// print results
?>
<table>
    <tr>
        <th>column1</th>
        <th>column2</th>
    </tr>
<?php while ($row = mysql_fetch_assoc($result)): ?>
    <tr>
        <td><?php echo $row['column1'] ?></td>
        <td><?php echo $row['column2'] ?></td>
    </tr>
<?php endwhile; ?>
</table>

Hi,

 

I need somethng like Column 1 and column 2 in the seperate row then second and third columns will show different records..

 

it is like what we can see for normal online store page.. show multiple items in a page..

 

please advise how can i accomplish that.. thanks

<?php
// database connection information
$hostname = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';

// open database
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database) or die("Unable to select database");

// query database
$query = "SELECT column1, column2 FROM table";
$result = mysql_query($query) or die(mysql_error());

// print results
$number_of_columns = 3;
$x = 1;
?>
<table>
<?php while ($row = mysql_fetch_assoc($result)): ?>
    <?php if ((($x - 1) % $number_of_columns) == 0): ?>
        <tr>
    <?php endif; ?>
    <td>
        <h2><?php echo $row['column1'] ?><h2>
        <p><?php echo $row['column2'] ?></p>
    </td>
    <?php if (($x % $number_of_columns) == 0): ?>
        </tr>
    <?php endif; ?>
    <?php $x++; ?>
<?php endwhile; ?>
</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.