?
Solved

How would I make this for loop work?

Posted on 2014-11-25
4
Medium Priority
?
77 Views
Last Modified: 2014-11-25
Here's my function:

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;	

Open in new window


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;
}	

Open in new window


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?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 40465465
I would need to know the format of '$columns'.  If it is an arrays of arrays like you would have by collecting the rows from a MySQL query, then you would need to address the individual items with array syntax.  The code that you are showing might work with a single $columns array if you set a counter ahead of the foreach.  I don't think that 'id' is going to be the value for all columns.
function csv_sample($columns) {
$snippet='';
$cntr = 0;
foreach ($columns as $cell)
	{
		if($cntr == 0 OR $cntr = 7)
			{
				$snippet.="";
			}
		else
			{
				$snippet.="
				<td class=\"csv_sample\">$cell</td>
				";
			}
$cntr++;
	}
	return $snippet;
}	

Open in new window

0
 

Author Comment

by:brucegust
ID: 40465542
Dave, we're poised on the threshold of great things, but I'm still one thing short.

Here's what I've got, based on your counsel:

function csv_table($sample_array,$number_columns) {
$snippet='';
$cntr=0;
foreach ($sample_array as $sample) 
	{
	echo $cntr;
		if($cntr==0 OR $cntr==$number_columns)
			{
				$snippet.="";
			}
		else	
			{
				$snippet.="
				<tr>
					<td class=\"csv_sample_cell\">$sample</td>
				</tr>";
			}
		$cntr=$cntr+1;
	}
	
return $snippet;	
}

Open in new window


I'm getting this error:

Array to string conversion in C:\wamp\www\SouthArea\csv_storeView.php on line 36

Line 36 being <td class=\"csv_sample_cell\">$sample</td>

...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:

function csv_display($table_name) {

	global $mysqli;

	$sql="select * from $table_name order by posted_time LIMIT 10";
	//echo $sql;
	$query=$mysqli->query($sql);
	$result_array = array();
	while ($row=$query->fetch_array())
		{
			$result_array[]=$row;
		}
	return $result_array;
	}
}

Open in new window


How else can I document $sample so it the data will be accurately reflected on the page?
0
 

Author Comment

by:brucegust
ID: 40465632
Dave!

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;
}

Open in new window


Thanks!
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40465758
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question