Jump to content

[SOLVED] Filling in <table> values


alphadeltaviii

Recommended Posts

Hi

 

I have a script to pull values for an image gallery from a mysql db, but I am having trouble with the script filling a row with 3 images then moving on to the next row of the table. Any suggestions? (For example the table is 3 cells wide, then expands vertically)

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/
Share on other sites

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]

                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=...');

}

}

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

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>');
			}
			?>

<?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>');
}
?>

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();

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.