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
https://forums.phpfreaks.com/topic/55147-loop-with-html-tables/
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
https://forums.phpfreaks.com/topic/55147-loop-with-html-tables/#findComment-272631
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
https://forums.phpfreaks.com/topic/55147-loop-with-html-tables/#findComment-272704
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
https://forums.phpfreaks.com/topic/55147-loop-with-html-tables/#findComment-273450
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.