Hi All,
I'm trying to write some code to read a CSV file and produce HTML from it. I'm using MAMP to develop the page, but no code at all will display in the browser. I would like the first line of the file to be the header, and then read each line and putting them into paragraphs.

Structure of events.csv

Summer 2010
Event 1^some information
Event 2^some information^more information

PHP Code:

if (($handle=fopen("events.csv","r"))!=FALSE)
	{
		while (($data=fgetcsv($handle),500,"^"))!=FALSE)
		{
			if ($row==0)
			{
				$num=count($data);
				row++;
				
				for ($c=0;$c<$num;c++)
				{
					?><h1> <?php echo $data[$c]; ?></h1>
				}
			}
			
			else
			{
				while (($column=fgetcsv($handle1,500,"^"))!=FALSE)
				{
					$num=count($column);
					
					for ($c=0;$c<$num;$c++)
					{
						if ($c=0)
						{
							?>
							<h1><?php echo $column[$c]; ?></h1>
							<?php
						}
						
						else
						{
							?>
							<p><?php echo $column[$c]; ?></p>
							<?php
						}
					}
				}
			}
}
}
?>

What's wrong? I've tried debugging the code and the page won't display once it enters the while loop. What do I need to do to fix this code?

Cheers,

Recommended Answers

All 2 Replies

You have syntax error in following line numbers of your code
line no. 3. fgetcsv($handle),500,"^")) // remove UNWANTED ) AFTER $HANDLE
line no. 8. write $row++ instead of row++
line no. 11. for ($c=0;$c<$num;$c++) // WRITE $c++ instead of c++

I just managed to get it working after starting from scratch :)

if (($handle=fopen("events.csv","r"))!==FALSE)
	{
		$rowNo=1;
				
		while (($data = fgetcsv($handle, 1000, "^")) !== FALSE) 
		{
			$num=count($data);
			
			if ($rowNo==1)
			{
				for ($c=0; $c < $num; $c++)
				{?>
					<h1><?php echo $data[$c]; ?></h1>
				<?php		
				}
			}
			
			else
			{	
				echo "<p class=\"test\">";
				for ($c=0; $c < $num; $c++)
				{
					echo $data[$c]; ?><br />
				<?php		
				}
				echo "</p>";
				
			}
			
			$rowNo++;
		}
	}	
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.