Jump to content

[SOLVED] Filling in <table> values


alphadeltaviii

Recommended Posts

use the modulus operator

 

for($i=1;$i<12;$i++)
    echo "$i" . (($i % 3)?' .&nbsp':'<BR>');
[code]

which is basicly the same as keeping a counter
[code]
for($i=1,$counter=0;$i<12;$i++,$counter++)
{
  echo "$i";
  if($counter<3) echo " . ";
  else echo "<BR>";
}

[/code][/code]

Link to comment
Share on other sites

                Here is what I have so far     

                <?php

include ("dbinfo.php");

$sql = "SELECT * FROM picture ORDER BY 'ID' ASC";

$result = mysql_query($sql); print mysql_error();

if(mysql_num_rows($result))

{

while($row = mysql_fetch_assoc($result))

{

$id = $row['ID'];

$plink = $row['link'];

$ptitle = $row['title'];

$palt = $row['alt'];

echo ('<table width="510px">');

echo ('<tr>');

echo ('<td><a href=...');

echo ('<td><a href=...');

echo ('<td><a href=...');

}

}

Link to comment
Share on other sites

you're on the right track,

 

you're if statement might want to look like

 

if ((mysql_num_rows($result)) >= 1 ) {

 

a simple method of going from there would be a counter,

 

so something like

 

$i = 1;
echo ('<table width="510px">');

while ($row = mysql_fetch_assoc($result)) {
       if ($i == 1) {
               echo '<tr>';
       }
       
       $i ++;
       $id = $row['ID'];
       $plink = $row['link'];
       $ptitle = $row['title'];
       $palt = $row['alt'];
       echo '<td><a href=...';
       if ($i == 3) {
                 echo '</tr>';
                 $i = 1;
       }
}
echo '</table';

 

does this make sense?

 

* edit *

forgot to close the loop

Link to comment
Share on other sites

I'm having trouble finding my issue, the page isn't loading

 

		  <?php
			include ("dbinfo.php");
				$sql = "SELECT * FROM picture ORDER BY 'ID' ASC";
				$result = mysql_query($sql); print mysql_error();
			if(mysql_num_rows($result))
			{
				$i = 1;
				echo ('<table width=\"510\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">');

				while ($row = mysql_fetch_assoc($result)) {
			       if ($i == 1) {
		               echo ('<tr>');
			       				}
       
   			       $i ++;
			       $id = $row['ID'];
			       $plink = $row['link'];
			       $ptitle = $row['title'];
			       $palt = $row['alt'];
			       echo ('<td width=\"170px\" height=\"120px\"><a href=\"/pgallery/$plink.jpg\" rel=\"lightbox\" title=\"$title\"><img src=\"/pgallery/thumb/$plink.jpg\" /></a>');
			       if ($i == 3) {
               				  echo ('</tr>');
		                 $i = 1;
       								}

				echo ('</table>');
			}
			?>

Link to comment
Share on other sites

<?php
include ("dbinfo.php");
$sql = "SELECT * FROM picture ORDER BY ID ASC"; //remove ' around ID
$result = mysql_query($sql); print mysql_error();
if(mysql_num_rows($result)){
$i = 1;
echo ('<table width=\"510\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">');

while ($row = mysql_fetch_assoc($result)) {
	if ($i == 1) {
		echo ('<tr>');
	}

	$i ++;
	$id = $row['ID'];
	$plink = $row['link'];
	$ptitle = $row['title'];
	$palt = $row['alt'];
	echo ("<td width=\"170px\" height=\"120px\"><a href=\"/pgallery/$plink.jpg\" rel=\"lightbox\" title=\"$ptitle\"><img src=\"/pgallery/thumb/$plink.jpg\" /></a></td>");
	// in line befure chenge ' to " and add </td> to end
	if ($i == 4) {//change 3 to 4
		echo ('</tr>');
		$i = 1;
	}
}// close while loop
echo ('</table>');
}
?>

Link to comment
Share on other sites

simple oo php example:

<?php
<?php

class ImageDetails {
/**
 * all image details you need from your db.
 * use "mysql_fetch_object" to build your results as objects
 */
public function getName() {
	return 'x';
	// image name for example...
}
}


class ImageGallery {

private $list;

private $size;

/**
 * @param array $list, list of images details
 * @param int $size, number of gallery collumns 
 */
public function __construct($list, $size = 3) {
	$this->list = $list;
	$this->size = $size;
}

public function show() {
	$counter = 0;

	echo '<table border="1">';
	foreach ($this->list as $image) {
		if ($counter == 0) { // open row
			echo '<tr>';
		}

		$this->_printRow($image);
		++$counter;

		if ($counter == $this->size) { // close row
			echo '</tr>';
			$counter = 0;
		}
	}
	if ($counter != 0) { // padding the table
		$this->_paddTable($counter);
	}

	echo '</table>';
}

private function _printRow($image) {
	// here you print the image in any form you want
	echo '<td>' . $image->getName() . '</td>';
}

private function _paddTable($counter) {
	for ($i=$counter ; $i<$this->size ; ++$i) {
		echo '<td> </td>';
	}
}
}

// 'list of your images (ImageDetails objects)
$list = array(new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails());

$gallery = new ImageGallery($list);
$gallery->show();

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.