Jump to content

dynamic html table with php


davids_media

Recommended Posts

I want to create a table with data populated from a query.

 

At the moment, I have have 8 records, however I want to have three columns and I want the layout like this;

 

Name

Age

Name

Age

Name

Age

 

Here is the code thus far?

 

<?php

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);

require ('includes/config.inc.php');

include ('./includes/header.html');

require (MYSQL);

include ('./includes/main.html');

if($id = isset($_GET['catID'])) 
{
  //Create and run query to get product category names for the selected cat ID
    $query = "SELECT `product`.`product`, `category`.`cat`, `product`.`prod_descr`, `category`.`cat_descr`, `product`.`price`, `product`.`image`
              FROM `product`
              LEFT JOIN `category` ON `product`.`catID` = `category`.`catID`
              WHERE `product`.`catID`='{$_GET['catID']}'
              ORDER BY `product`.`product`";
    $r = mysqli_query($dbc, $query);

    //Create flag variable to determine if header needs to be displayed
    $showHeader = true;
    echo "<div id='right'>";

echo "<table cellpadding='5' cols='2'>";


    while($row = mysqli_fetch_array($r))
    {
        if($showHeader)
        {
            //Display category header
            echo "<h1>" . "<span>" . "# " . "</span>" . $row['cat'] .  "<span>" . " #" . "</span>" . "</h1>" . "\n";
		echo "<h2>" . $row['cat_descr'] . "</h2>" . "\n";
            $showHeader = false;
        }
        //Display product name	

	echo "<tr>";

	echo "<td><img src='db/images/".$row['image']."' height=50px width=50px /></td>";
		echo "<td>". "<li>" . "<a>" . $row['product'] . "</a>" . "</li>" . "</td>";
		echo "<td class='price'>" . "£". $row['price'] . "</td>"; 

	echo "</tr>";

    }

echo "</table>";

    echo "</div>";

}

include ('./includes/footer.html');

?>

 

how do I achieve this properly?

Link to comment
Share on other sites

Well for one, you can't have html code between table tags.  Maybe this is more what you want to do.

 if($showHeader)
        {
            //Display category header
	echo "<tr>";		 
		echo "<td><h1><span>#</span></h1></td>";
 		echo "<td><h1>" . $row['cat'] .  "</h1></td>";
 		echo "<td><h1><span>#</span></h1></td>";
		echo "</tr>\n";
	echo "<tr>";		 
		echo "<td colspan=\"3\"><h2>" . $row['cat_descr'] . "</h2></td>";
		echo "</tr>\n";
            $showHeader = false;
        }

Link to comment
Share on other sites

You need to be more specific. You say you want three columns with data in the format

 

Name Age

Name Age

Name Age

 

But, that looks like data in two columns. So, are you wanting data in 6 columns (2 per record) or is the name and age in the same TD cell? Not really clear what you need/want.

Link to comment
Share on other sites

what i want is the layout of the data table to be like this websites;

 

http://www.riverisland.com/Online/men/just-arrived

 

Well, that doesn't tell us if you want the records to be output left to right or top to bottom

 

1   2   3
4   5   6
7   8   9

 

Or

1   4   7
2   5   8
3   6   9

 

The first option is much easier to implement, so I'll provide a sample for that in a few minutes

Link to comment
Share on other sites

Not tested, but logic should be good

 

<?php

//Set number of columns to use
$maxCols = 3;

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);

require ('includes/config.inc.php');
include ('./includes/header.html');
require (MYSQL);
include ('./includes/main.html');

if($id = isset($_GET['catID'])) 
{
    //Create and run query to get product category names for the selected cat ID
    $query = "SELECT `product`.`product`, `category`.`cat`, `product`.`prod_descr`, `category`.`cat_descr`, `product`.`price`, `product`.`image`
              FROM `product`
              LEFT JOIN `category` ON `product`.`catID` = `category`.`catID`
              WHERE `product`.`catID`='{$_GET['catID']}'
              ORDER BY `product`.`product`";
    $r = mysqli_query($dbc, $query);

    if(!$f)
    {
        echo "Database query failed";
    }
    else
    {
        //Create flag variable to determine if header needs to be displayed
        $catHead = mysql_result($r, 0, 'cat');
        $cat_descrHead = mysql_result($r, 0, 'cat_descr');

        //Open div
        echo "<div id='right'>";
        //Display header
        echo "<h1><span># </span>{$row['cat']}<span> #</span></h1>\n";
        echo "<h2>{$row['cat_descr']}</h2>\n";
        //Open table
        echo "<table cellpadding='5' cols='2'>";

        //Set index var to track record count
        $recIdx = 0;
        while($row = mysqli_fetch_array($r))
        {
            $recIdx++;

            //Open new row if needed
            if($recIdx % $maxCols == 1)
            {
                echo "<tr>\n";
            }

            //Display product	
            echo "<td>";
            echo "<img src='db/images/{$row['image']}' height=50px width=50px /><br>";
            echo "<a>{$row['product']}</a><br>";
            echo "<span class='price'>£{$row['price']}</span>"; 
            echo "</td>\n";

            //Close row if needed
            if($recIdx % $maxCols == 0)
            {
                echo "</tr>\n";
            }
        }

        //Close last row if needed
        if($recIdx % $maxCols == 0)
        {
            echo "</tr>\n";
        }

        //Close table & div
        echo "</table>";
        echo "</div>";
    }
}

include ('./includes/footer.html');

?>

Link to comment
Share on other sites

Did you verify if all the records you expect were included in the result set (hint, echo the value of mysql_num_rows() to the page). Did you inspect the HTML source to see if the records were there but not displayed due to a problem with the table formatting? Did you do any debugging and, if so, what did you find?

Link to comment
Share on other sites

Where's your code? If the results are off by 1, I typically see this when a mysql_fetch is called before the loop starts.

 

The only other thing I can think of is the use of mysql_result() before the loop. But, the manual doesn't state that using mysql_result() will move the pointer ahead a record. So, that doesn't make sense.

Link to comment
Share on other sites

<?php

//Set number of columns to use
$maxCols = 3;

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);

require ('includes/config.inc.php');
include ('./includes/header.html');
require (MYSQL);
include ('./includes/main.html');

if($id = isset($_GET['catID'])) 
{
    //Create and run query to get product category names for the selected cat ID
    $query = "SELECT `product`.`prodID`, `product`.`product`, `category`.`cat`, `product`.`prod_descr`, `category`.`cat_descr`, `product`.`price`, `product`.`image`
              FROM `product`
              LEFT JOIN `category` ON `product`.`catID` = `category`.`catID`
              WHERE `product`.`catID`='{$_GET['catID']}'
              ORDER BY `product`.`product`";
    $r = mysqli_query($dbc, $query);
$num_rows = mysqli_num_rows($r); 

$showHeader = true;

echo "<div id='right'>";

    while($row = mysqli_fetch_array($r))
    {
        if($showHeader)
        {
            //Display category header
            echo "<h1>" . "<span>" . "# " . "</span>" . $row['cat'] .  "<span>" . " #" . "</span>" . "</h1>";
		echo "<h2>" . $row['cat_descr'] . "</h2>";
            $showHeader = false;
        }
        //Open table
        echo "<table>";

        //Set index var to track record count
        $recIdx = 0;
        while($row = mysqli_fetch_array($r))
        {
            $recIdx++;

            //Open new row if needed
            if($recIdx % $maxCols == 1)
            {
                echo "<tr>";
            }

            //Display product	
            echo "<td>";
            echo "<img src='db/images/".$row['image']."' height=150px width=150px /><br>";
            echo "<li>" . "<a href='item.php?prodID={$row['prodID']}'>" . $row['product'] . "</a>" . "</li>";
            echo "<span>" . "£". $row['price'] . "</span>"; 
            echo "</td>\n";

            //Close row if needed
            if($recIdx % $maxCols == 0)
            {
                echo "</tr>\n";
            }
        }

        //Close last row if needed
        if($recIdx % $maxCols == 0)
        {
            echo "</tr>\n";
        }

        //Close table & div
        echo "</table>";
	echo "$num_rows Rows\n";
        echo "</div>";
    }
}

include ('./includes/footer.html');

?>

Link to comment
Share on other sites

Hmm . . . when I provide code I don't expect the person receiving the code to use it verbatim. But, I do expect the recipient to try and understand the code. And, if something is not understood then to ask questions. If you had used the code as I submitted it it probably would have worked perfectly assuming there were no minor typos.

 

What you implemented makes no sense. You have two while loops to process the query results with one embedded in another. The first while loop will extract the first record and create the header (which I showed a better method). Then the inner while loop will process the individual records. But, it will start at record #2 since the first record was already consumed.

 

 

Link to comment
Share on other sites

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.