Jump to content

Recommended Posts

Hey!

 

Right now, I'm creating a listing of files in a certain directory as such:

 

foreach(glob('*.jpg') as $picFileJPG)
{
echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$picFileJPG.'</a>'.'<br>';
}

 

What I want to do though, is to run through, and echo out html tables so that the first ten results are each in a separate row in a table, and the next ten are in separate rows in a new table, etc., so that it goes ten down per column.

 

I've tried doing the start of this, limiting it to one table with ten rows; heres the code I've got:

 

echo '<center>';
	echo '<table>';
		foreach(glob('*.jpg') as $picFileJPG)
		{
			$tableRowNumber = 1;

			if($tableRowNumber <= 10)
			{
				echo '<tr>';
				echo '<td>';
				echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$picFileJPG.'</a>'.'<br>';
				echo '</td>';
				echo '</tr>';

				$tableRowNumber++;
			}
		}
	echo '</table>';
	echo '</center>';

 

Now, it *should* only go through ten and print out, no? But it prints out all results.

 

Even if I got that right, I still have the issue of starting a new table for the next ten results.

 

Can anyone help me on this? Possibly with some psuedo-code? Thanks so much!

 

FlyingIsFun1217

Link to comment
https://forums.phpfreaks.com/topic/107651-solved-table-output-help/
Share on other sites

Hope this helps.

 

$col = 0;
echo "<table>";
$q = mysql_query("SELECT * FROM chat");
while($data = mysql_fetch_array($q)){
	if($col === 4){
	echo "<tr>";
	}
	echo "<td>".$data['id']."$col</td>";
	$col++;
	if($col === 4){
	echo "</tr>";
	$col = 0;
	}

}

echo "</table>";

Cool, thanks for the tip on the variable location, I didn't think of that strangely enough.

 

And definitely thanks on the example, I'll look it over in a little while (right now, I'm working on an upload form).

 

Thanks again!

FlyingIsFun1217

Two posts up, it was suggested to do all work in one table. Thing is, I want to construct as many tables as needed (with each holding one ten rows and one column), so that the listing goes down alphabetically, like the following:

 

tbl1    tbl2

abc    klm

bcd    lmn

cde    mno

def    nop

efg    opq

fgh    pqr

ghi    qrs

hij    rst

ijk    stu

jkl    tuv

 

So every ten entries, I need to start a new table (ideally), and give each result a new <tr><td> entry.

 

Any examples of this somewhere? Thanks!

FlyingIsFun1217

Alright, I'm creating some psuedo-code, here's what I have so far:

 

echo '<table>';
echo '<tr>';

$rowNumber = 1;

foreach(image as $imgVar)
{	
if($rowNumber <= 10)
{
	if($rowNumber == 1)
	{
		echo '<td>';
		echo '<table>';
	}

	// Insert data, up until the tenth row
	echo '<tr><td>'.$imgVar.'</td></tr>';

	if($rowNumber == 10)
	{
		//Alread inserted the data to the table, now we just need to end the table
		echo '</table>';
		echo '</td>';

		$rowNumber == 1;
	}
}
}

echo '</tr>';
echo '</table>';

 

Do you see anything wrong with this? I would expect it to create one nested table containing tables like the ones that I showed above, each being in their own column of the master table.

 

FlyingIsFun1217

Alright, I've got it mostly worked out. Here is my current code:

 

$tableRowNumber = 1;

	echo '<center>';
	//========================================================================================================================================================================//
	echo '<table border="1">';
	echo '<tr>';

		foreach(glob('*.jpg') as $picFileJPG)
		{
			$simplePicName = substr_replace($picFileJPG,'',-4);

			if($tableRowNumber == 1)
			{
				echo '<td>';
				echo '<table border="1">';
			}

			echo '<tr>';
			echo '<td>';
			echo '<a href="'.$baseURL.'viewPic.php?pID='.$picFileJPG.'">'.$simplePicName.'</a>';
			echo '</td>';
			echo '</tr>';

			if($tableRowNumber == 10)
			{
				echo '</table>';
				echo '</td>';

				$tableRowNumber = 1;
			}

			if($tableRowNumber != 10)
			{
				$tableRowNumber++;
			}
		}

	echo '</table>';
	echo '</td>';
	echo '</tr>';
	echo '</table>';
	//========================================================================================================================================================================//
	echo '</center>';

 

My only problem is that at the start of a new nested table, I don't get the "<td><table>" printout as I should. I looked through, and it seems I should, since I am setting $tableRowNumber back to '1' when it reaches ten.

 

See anything that I'm not?

 

FlyingIsFun1217

Change:

if($tableRowNumber == 10)

{

echo '</table>';

echo '</td>';

 

$tableRowNumber = 1;

}

 

if($tableRowNumber != 10)

{

$tableRowNumber++;

}

 

To:

if($tableRowNumber == 10)

{

echo '</table>';

echo '</td>';

 

$tableRowNumber = 1;

}

 

else

{

$tableRowNumber++;

}

 

Because you're setting $tableRowNumber to 1 right before it, so obviously it won't equal 10, and it'll increase past one.

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.