hi i want to create table in php but i dont know how many row are in database. but i want to show 2 records per row. please help me how can i do that ??

here is my code but it wasnt work as i desired it create i column not 2.

<?php

require_once("config.inc");

function getBookSeller ()
{

    $con = mysql_connect(server , userName , password ) or die('DataBase cannot connect. Reason: ' . mysql_errno());

    mysql_select_db(db_name) or die('Table cannot connect. ' . mysql_errno());

    $sql = "SELECT * 
            FROM book_sellers";

    $result = mysql_query($sql, $con) or die('Query Error. ' . mysql_errno());

    $totalrows = mysql_affected_rows();
    $totalrows = ceil( $totalrows/2);

    //echo $totalrows;


        echo "<table width='600' border='1' cellspacing='2' cellpadding='3' bordercolor='#FFFFFF'>";
        echo "<tr align='center'>";
        echo "<td colspan='2' bordercolor='#000000' bgcolor='#9999CC'><b>Get 
    your copy today</b></td>";
        echo "</tr>";



    while($row = mysql_fetch_array($result))
    {

        $output = "<tr bordercolor='#CCCCCC'>";
        $output .= "<td> <b>".$row["book_shop"]."</b><br><br>";
        $output .= "<b>Att: </b>".$row["name"]."<br>";
        $output .= "<b>Add: </b>".$row["address"]."<br>";
        $output .= "<b>Phone: </b>".$row["ph"]."<br>";
        $output .= "<b>Fax: </b>".$row["fax"]."<br>";
        $output .= "<b>Mobile: </b>".$row["mobile"]."<br>";
        $output .= "<b>Email: </b>".$row["email"]."<br>";
        $output .= "<b>Website: </b>".$row["website"]."<br>";
        $output .= "</td>";
        $output .= "</tr>";

        echo $output;
    }

    echo "</table>";

}

?>

Recommended Answers

All 4 Replies

Add a variable that counts record and check in the loop whether is odd or even. If it is odd then you start the row and the cell, if it is even end the cell and the row of the table.

function getBookSeller ()
{
    $con = mysql_connect(server , userName , password ) or die('DataBase cannot connect. Reason: ' . mysql_errno());
    mysql_select_db(db_name) or die('Table cannot connect. ' . mysql_errno());
    $sql = "SELECT *
    FROM book_sellers";
    $result = mysql_query($sql, $con) or die('Query Error. ' . mysql_errno());
    $totalrows = mysql_affected_rows();
    $totalrows = ceil( $totalrows/2);
    //echo $totalrows;
    echo "<table width='600' border='1' cellspacing='2' cellpadding='3' bordercolor='#FFFFFF'>";
    echo "<tr align='center'>";
    echo "<td colspan='2' bordercolor='#000000' bgcolor='#9999CC'><b>Get
    your copy today</b></td>";
    echo "</tr>";
    while($row = mysql_fetch_array($result))
    {
        // initialize output string
        $output = "";

        // if the current record is odd (1,3,5...) then start the row and the cell
        if($current_record % 2 == 1) {

            $output .= "<tr bordercolor='#CCCCCC'><td>";
        }

        $output .= "<b>".$row["book_shop"]."</b><br><br>";
        $output .= "<b>Att: </b>".$row["name"]."<br>";
        $output .= "<b>Add: </b>".$row["address"]."<br>";
        $output .= "<b>Phone: </b>".$row["ph"]."<br>";
        $output .= "<b>Fax: </b>".$row["fax"]."<br>";
        $output .= "<b>Mobile: </b>".$row["mobile"]."<br>";
        $output .= "<b>Email: </b>".$row["email"]."<br>";
        $output .= "<b>Website: </b>".$row["website"]."<br>";

        // if the current record is even (2,4,6...) then end the cell and the row
        if($current_record % 2 == 0) {

            $output .= "</td></tr>";
        }
        echo $output;

        $current_record++;
    }

    // if the last row was odd then you have to end the row when out of the loop
    if($current_record % 2 == 0) {

        echo "</td></tr>";
    }

    echo "</table>";
}

To get the number of records in the db table make a query using COUT() function:

$toral_rows_query = 'SELECT COUNT(*) FROM book_sellers';

You can not use mysql_affected_rows() since this function is used only for queries that change records (UPDATE, DELETE, INSER).

commented: good +5

whre i get $current_record value ?

echo "</tr>";
while($row = mysql_fetch_array($result))
{

just before the while make it and increment on each loop

echo "</tr>";
$current_record = 0;
while($row = mysql_fetch_array($result))
{
//$current_record++; this increments the value each loop later on

Sorry for my silence, I was away for some time.
I forgot to initialize the $current_record variable before the while loop. As Biim said put the missing line before the while loop begins.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.