Jump to content

Php Foreach Loop + Html


@li

Recommended Posts

Right, someone must have the answer to this..

 

The idea;

Print some data out into an HTML table - 2 columns, as many rows as it takes.

 

 

------------------------------

data set 1 | data set 2 |

-----------------------------

data set 1 | data set 2 |

------------------------------

data set 1 | data set 2 |

-------------------------------

data set 1 | data set 2 |

------------------------------

 

Problems;

Printing from 2 different "foreach" loops in PHP.

+ my lack of PHP syntax....

 

How to fix;

stop printing from 2 "foreach" loops and somehow combine to one loop!?

 

code:

<table id="working" border="0" cellspacing="1" cellpadding="3">
<tr>
	   <?php foreach ($csv->titles as $value): ?>
	<td><?php echo $value; ?></td>
	<?php endforeach; ?>
</tr>
<?php foreach ($csv->data as $key => $row): ?>


 <tr>
   <?php foreach ($row as $value): ?>
	<td><?php echo $value; ?></td>
	<?php endforeach; ?>
 </tr>
<?php endforeach; ?>
</table>

 

The above code looks like this;

---------------------------------------------------------

data set 1 | data set 1 | data set 1 | data set 1 |

---------------------------------------------------------

data set 2 | data set 2 | data set 2 | data set 2 |

---------------------------------------------------------

 

So as you can see everything is horizontal. I am trying to get it to print down the column not accross in rows...

Any ideas?

Is there a way I can combine a foreach loop >

foreach ($csv->titles as $value) AND (statement2):

echo $value;

echo $value2;

endforeach;

 

???? <_<

 

thanks for reading.

Share this post


Link to post
Share on other sites

You could get the length of both arrays, find out which is the biggest, and then do a loop from i = 0 to biggest.

 

Print out each array with $array[$i] and $array2[$i] as needed, checking each time that $i isn't bigger than either array before you access it.

 

I think that would work.

 

It should be something like the following:

 

<?php
$largestArray = 0;

$array1Size = count($array1);
if ($array1Size > $largestArray) $largestArray = $array1Size;

$arrray2Size = count($array2);
if ($array2Size > $largestArray) $largestArray = $array2Size;

echo "<table>";

for ($i = 0; $i < $largestArray; $i++)
{
echo "<tr>";

if ($i < $array1Size)
{
	echo "<td>";
	echo $array1[$i];
	echo "</td>";		
}
else
{
	echo "<td>";
	echo "null";
	echo "</td>";		
}

if ($i < $array2Size)
{
	echo "<td>";
	echo $array2[$i];
	echo "</td>";		
}
else
{
	echo "<td>";
	echo "null";
	echo "</td>";		
}

echo "</tr>";
}

echo "</table>";
?>

 

Not tested. Might work, might not. Might be a better way.. etc etc etc :)

Edited by markiemrboo

Share this post


Link to post
Share on other sites

Sweeet!!!

Thank you markiemrboo for that!!!

I will certainly file that away in my useful code box :thumbs-up:

 

I was being a bit silly though.. I had not understood my own array structures so all i really needed was:

 

 <?
for ($n = 0; $n < count($csv->data); $n++) {
  		for ($i = 0; $i < count($csv->titles); $i++) {
		print '<h1>'.$csv->titles[$i].':</h1> ';
		print '<h2>'.$csv->data[$n][$csv->titles[$i]].'</h2><br />';

	}
}
?>

 

displays as:

 

--------------------------------------

 

title:

some content

 

title2:

some more contents around here

 

title3:

content

 

--------------------------------------

 

Took my ages to get that right.... You might have noticed in the end i strayed away from the table idea... was too complex for me. Though looking at your code, I might give it another crack.

 

Thanks super-duper.

Share this post


Link to post
Share on other sites

  • 1 year later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...