Jump to content

loop with html tables


fluxem

Recommended Posts

I realize this probably pretty basic but nonetheless I need a point in the right direction  .. :)

heres what I got:

 

I currently have it to where the user can input data and it is stored in a mysql db.. I am having problems with the output portion and it being formatted the way I would like it.

 

I want for it to print the all the data results inside of html tables...

5 across (columns) and 2 down (rows) so a total of ten records are printed out.  After 10 I want it to generate a second page with the next 10 records.. these records will all be sorted by date..

 

here is a screen shot.. http://fluxar.com/images/star.jpg

 

 

here is the code I currently have to print my results..

 

<?php include ('connect/index.php') ?>
<?php

    mysql_connect(localhost, $username, $password);
    @mysql_select_db($database) or die("Unable to select database");
    $query = "SELECT * FROM saleitems";
    $result = mysql_query($query);
   
    $num = mysql_numrows($result);
   
    mysql_close();
   
    echo "<center>Database Output</center><br><br>";
?>
   
    <table border="1" cellspacing="2" cellpadding="2" align="center">
    <tr>
   
<?php
    $i=0;
    while ($i < $num){
   
    $image = mysql_result($result, $i, "image");
    $name = mysql_result($result, $i, "name");
    $classification = mysql_result($result, $i, "classification");
    $percentoff = mysql_result($result, $i, "percentoff");
    $dateadded = mysql_result($result, $i, "dateadded");
?>

   
    <td><?php echo "<img src=\"$image\"><br>$name<br>$classification<br>$percentoff<br>"; ?></td>
  
   

<?php
   
    $i++;
   
    }
   
    echo "</tr></table>";

?>

 

any help on this would be greatly appreciated! thanks :)

 

Link to comment
Share on other sites

You could use something like this:

 

// display results
$totalColumns = 5;
$i = 1;
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){
if ($i == 1) echo "<tr>";
echo "<td>";
echo "place your database row calls in here";
echo "</td>";
if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}
echo "</table>";

Link to comment
Share on other sites

Him Simcoweb,, thank you for your reply!

 

I am getting this error..

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/fluxar/public_html/clients/starfurniture/sale-items3.php on line 20

 

here is my updated code:

 

<?php include ('connect/index.php') ?>
<?php

mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM saleitems";
$result = mysql_query($query);

$num = mysql_numrows($result);

mysql_close();

echo "<center>Database Output</center><br><br>";
?>
<?php 
// display results
$totalColumns = 5;
$i = 1;
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){
if ($i == 1) echo "<tr>";
echo "<td>";
echo "<img src=\"$image\"><br>$name<br>$classification<br>$percentoff<br>";
echo "</td>";
if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}
echo "</table>";
?>

Link to comment
Share on other sites

Hi Suma

I put that at the end and I still get the same error..

 

Here is my current code:

 

<?php include ('connect/index.php') ?>
<?php

mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM saleitems";
$result = mysql_query($query);

$num = mysql_numrows($result);



echo "<center>Database Output</center><br><br>";
?>
<?php 
// display results
$totalColumns = 5;
$i = 1;
echo "<table border='0' cellpadding='2'>";
while ($row = mysql_fetch_array($results)){
if ($i == 1) echo "<tr>";
echo "<td>";
echo "<img src=\"$image\"><br>$name<br>$classification<br>$percentoff<br>";
echo "</td>";
if ($i == $totalColumns)
	{
	echo "</tr>";
	$i = 0;
	}
$i++;
}
echo "</table>";
?>

<?php
mysql_close();
?>

 

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.