Bruce Gust
asked on
How would I make this for loop work?
Here's my function:
Here's the challenge...
The $sample_array is coming in as an eight column body of data. The first column is going to be the id, the second column is going to be the session_id, neither of which I want to display to my user.
What I've been doing, when I'm getting ready to enumerate an array is do a "foreach," like this:
But I'm trying to alter things a bit so I can avoid the first and last column of the array.
I thought I could do a for statement and somehow position some kind of gatekeeper at the [0] position and the very last column, whatever that value would be based on the number of columns ($i).
Bottom line, its not working and I'm needing some help.
Any ideas?
function csv_table($sample_array,$number_columns) {
$snippet='';
for($i=0; $i<$number_columns; $i++)
{
$snippet.="
<tr>
<td class=\"csv_sample_cell\">$i</td>
</tr>
";
}
return $snippet;
Here's the challenge...
The $sample_array is coming in as an eight column body of data. The first column is going to be the id, the second column is going to be the session_id, neither of which I want to display to my user.
What I've been doing, when I'm getting ready to enumerate an array is do a "foreach," like this:
function csv_sample($columns) {
$snippet='';
foreach ($columns as $cell)
{
if($cell=="id" OR $cell=="session_id")
{
$snippet.="";
}
else
{
$snippet.="
<td class=\"csv_sample\">$cell</td>
";
}
}
return $snippet;
}
But I'm trying to alter things a bit so I can avoid the first and last column of the array.
I thought I could do a for statement and somehow position some kind of gatekeeper at the [0] position and the very last column, whatever that value would be based on the number of columns ($i).
Bottom line, its not working and I'm needing some help.
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dave!
I got it!
Thanks!
I got it!
function csv_table($sample_array,$number_columns) {
$snippet='';
foreach ($sample_array as $sample)
{
$snippet.="
<tr>";
for($i=1; $i<($number_columns-1); $i++)
{
$snippet.="<td class=\"csv_sample_cell\">{$sample[$i]}</td>";
}
$snippet."</tr>";
}
return $snippet;
}
Thanks!
Glad you 'got it'... but you have posted 3 different versions of your function. Please, next post only the version that you are actually having problems with. Don't 'simplify' it unless we ask you to.
ASKER
Here's what I've got, based on your counsel:
Open in new window
I'm getting this error:
Array to string conversion in C:\wamp\www\SouthArea\csv_
Line 36 being <td class=\"csv_sample_cell\">
...which means that "$sample" isn't cutting it, as far as accurately representing the contents of the array, yes?
The function that's driving that part of the script is this:
Open in new window
How else can I document $sample so it the data will be accurately reflected on the page?