Jump to content

Recommended Posts

Hi

 

Just womdering if this code should work as not sure an if statement will work within an if statement.

As you can see I am trying to write to a csv file whist also printing to a table (the table works ok),

so it might be me making a hash of the writing to csv.

 

else if ($cust=='cust')
       {
    echo "<table border='1'>
<tr>
<th>Email ID</th>
<th>Cust Email</th>
<th>Time</th>
<th>Printer</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['cust'] . "</td>";
  echo "<td>" . $row['time'] . "</td>";
  echo "<td>" . $row['printer'] . "</td>";
  echo "</tr>";
  
    }
echo "</table>";
if ($csv==true)
    {
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$output = implode.".".$result."\t\n";
$rf=fopen ("$DOCUMENT_ROOT/email.csv",'w+');
fwrite ($rf,$output, strlen($output));
    fclose($rf);
    }
    }

 

$result is the mysql_query.

 

Just looking for any advice even if its that will work but that is wrong type answers

although have always had great help here.

 

Cheers,

 

Spill.

Link to comment
https://forums.phpfreaks.com/topic/104690-using-if-in-if/
Share on other sites

A CSV file is just a comma delimited file. I'm not sure but it doesn't look like you're delimiting anything with your code. Open your CSV file in a text editor and see what it's printing. If your data fields aren't actually delimited with a comma, then excel probably won't open it as a CSV file...or at least it will open but be all messed up.

Link to comment
https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-535938
Share on other sites

Thanks for the info.

 

I'll take it I need to google more to

resolve the csv issue not working then.

Could I just send it to a txt file as I'm sure

excel opens those??

 

Cheers,

 

Spill.

Are you wanting to write the results returned from your SQL query to your CSV file. If so the following is not the correct way to do it:

if ($csv==true)
    {
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$output = implode.".".$result."\t\n";
$rf=fopen ("$DOCUMENT_ROOT/email.csv",'w+');
fwrite ($rf,$output, strlen($output));
    fclose($rf);
    }
    }

$result does not return anything. It only holds a Result Resource.

 

What you'll want to do is add the following line:

    $csvData .= implode(',',$row) . "\n";

after:

while($row = mysql_fetch_array($result))
  {

Now change

$output = implode.".".$result."\t\n";
$rf=fopen ("$DOCUMENT_ROOT/email.csv",'w+');
fwrite ($rf,$output, strlen($output));

to:

$rf=fopen ("$DOCUMENT_ROOT/email.csv",'w+');
fwrite ($rf,$csvData, strlen($csvData));

PHP should now populate your email.csv file with the results returned from your query.

Link to comment
https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536119
Share on other sites

Thanks for the info wildteen88.

 

Have done exactly what you have sugested but this will not write to a file. I have made a couple of adjustments but only the file name and $csv to $excel. If I echo $csvData it doubles every input from the db so it shows aa bb cc dd ee ff. The changed code is below. I keep staring at this but cannot see any problems.

while($row = mysql_fetch_array($result))
  { $csvData .= implode(',',$row) . "\n";
  echo "<tr>";
  echo "<td>" . $row['Email_ED'] . "</td>";
  echo "<td>" . $row['Cust_Email'] . "</td>";
  echo "<td>" . $row['Friends_Email'] . "</td>";
  echo "<td>" . $row['Time_Sub'] . "</td>";
  echo "<td>" . $row['printer_type'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
echo $csvData;
if ($excel==true)
    {
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$rf=fopen ("$DOCUMENT_ROOT/email.txt",'w+');
fwrite ($rf,$csvData, strlen($csvData));
    fclose($rf);
    }
    }

 

Cheers,

 

Spill.

Link to comment
https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536144
Share on other sites

Hope this helps

 

$result = mysql_query("SELECT * FROM submit WHERE printer_type LIKE '%$printer%' AND Time_sub BETWEEN '$date' AND '$todate'");

if ($cust=='all')
    {
    echo "<table border='1'>
<tr>
<th>Email ID</th>
<th>Cust Email</th>
<th>Friend Email</th>
<th>Time</th>
<th>Printer</th>
</tr>";

while($row = mysql_fetch_array($result))
  { $csvData .= implode(',',$row) . "\n";
  echo "<tr>";
  echo "<td>" . $row['Email_ED'] . "</td>";
  echo "<td>" . $row['Cust_Email'] . "</td>";
  echo "<td>" . $row['Friends_Email'] . "</td>";
  echo "<td>" . $row['Time_Sub'] . "</td>";
  echo "<td>" . $row['printer_type'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
echo $csvData;
if ($excel==true)
    {
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$rf=fopen ("$DOCUMENT_ROOT/email.txt",'w+');
fwrite ($rf,$csvData, strlen($csvData));
    fclose($rf);
    }
    }

 

 

Cheers,

 

Spill.

Link to comment
https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536344
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.