CSV Export - why the undefineds??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

CSV Export - why the undefineds??

Post by simonmlewis »

I have this script that works on another site with no errors, like this:

Code: Select all

<?php

$cookietype = isset($_COOKIE['type']) ? $_COOKIE['type'] : null;

$todaydate = date('Y-m-d');
$catid = isset($_REQUEST['catid']) ? $_REQUEST['catid'] : null;
if ($cookietype == "admin") {

include "dbconn.php";
$csv_output = '"Product ID","Product","Romancode","Category"';
$csv_output .= "\015\012";

$result = mysql_query("SELECT id, title, romancode, catname FROM products WHERE rcstock = 'out of stock' AND catid = '$catid'");
while($row = mysql_fetch_array($result)) 
{      
  $csv_output .= '"'.$row[id].'","'.$row[title].'","'.$row[romancode].'","'.$row[catname].'"';
    $csv_output .= "\015\012";
}
  //You cannot have the breaks in the same feed as the content. 
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv; filename=SoldOutProducts_" . date("d-m-Y") .".csv");
  print $csv_output;
  exit;
       	mysql_close($sqlconn);
  echo "Extract in progress - close page when completed.";
	}

	else 
	{
	echo "<meta http-equiv='Refresh' content='0 ;URL=/'>";
	}
	?>
But this version, looking on a different DB, where all fields do have data, is erroring:

Code: Select all

<?php

$cookietype = isset($_COOKIE['type']) ? $_COOKIE['type'] : null;

if ($cookietype == "admin") {

include "dbconn.php";
$csv_output = '"Firstname","Lastname","Email","Password"';
$csv_output .= "\015\012";

$result = mysql_query("SELECT firstname, lastname, email, password, `type` FROM admin WHERE `type` <> 'admin'");
while($row = mysql_fetch_array($result)) 
{      
  $csv_output .= '"'.$row[firstname].'","'.$row[lastname].'","'.$row[email].'","'.$row[password].'","'.$row[type].'"';
    $csv_output .= "\015\012";
}
  //You cannot have the breaks in the same feed as the content. 
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv; filename=SoldOutProducts_" . date("d-m-Y") .".csv");
  print $csv_output;
  exit;
       	mysql_close($sqlconn);
  echo "Extract in progress - close page when completed.";
	}

	else 
	{
	echo "<meta http-equiv='Refresh' content='0 ;URL=/'>";
	}
	?>
[text]Notice: Use of undefined constant firstname - assumed 'firstname' in C:\xampp\phpMyAdmin\site\csv_users.php on line 14

Notice: Use of undefined constant lastname - assumed 'lastname' in C:\xampp\phpMyAdmin\site\csv_users.php on line 14

Notice: Use of undefined constant email - assumed 'email' in C:\xampp\phpMyAdmin\site\csv_users.php on line 14

Notice: Use of undefined constant password - assumed 'password' in C:\xampp\phpMyAdmin\site\csv_users.php on line 14

Notice: Use of undefined constant type - assumed 'type' in C:\xampp\phpMyAdmin\site\csv_users.php on line 14
[/text]
100s of times over... and then it shows the results, but NOT with a CSV export. It does it only on screen.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: CSV Export - why the undefineds??

Post by Celauran »

You're trying to reference the array keys as constants instead of strings. You need to use $row['firstname'] rather than $row[firstname]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: CSV Export - why the undefineds??

Post by simonmlewis »

Yeah that works - how the heck does it work on the other site then, without the '' in there? lol
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: CSV Export - why the undefineds??

Post by Celauran »

Because PHP. Treat string indices as strings and you won't ever go wrong.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: CSV Export - why the undefineds??

Post by simonmlewis »

Ok. Any further thoughts on this thread: viewtopic.php?f=68&t=140163
I'm stumped.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: CSV Export - why the undefineds??

Post by Celauran »

You want to do 'responsive' without resizing or changing the flow. You're trying to fit 850px of content into 768px of viewport. It's not going to work.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: CSV Export - why the undefineds??

Post by simonmlewis »

It does work. If I set the Viewport to 1024, it fits into the ipad in portrait just fine, but then it forces iPhone to use the same thing.
Surely I don't have to set a CSS @media for lots of different sizes?

I want the main site to be displayed on all but -768px wide screens. If it's 768, it then shows the smaller version.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: CSV Export - why the undefineds??

Post by Celauran »

simonmlewis wrote:If I set the Viewport to 1024, it fits into the ipad in portrait just fine, but then it forces iPhone to use the same thing.
That's a peculiar definition of 'working'.
simonmlewis wrote:Surely I don't have to set a CSS @media for lots of different sizes?
That is, in fact, how breakpoints work.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: CSV Export - why the undefineds??

Post by simonmlewis »

Viewport, as I understand it, "fits to screen" whatever is on the screen to that set width.
If you set the width to width=device-width, should it not fit it to screen no matter what is there?

So in theory, why can I not say "if it is less than 768px, use @media for small screen, if over 768px, use the normal one, and use Viewport to "fit to screen"??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply