spillage Posted May 8, 2008 Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/ Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 yes, an if statement will work inside an if statement. this is valid: if ($something == 1) { if ($something_else == 2) { // Do something } } Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-535872 Share on other sites More sharing options...
revraz Posted May 8, 2008 Share Posted May 8, 2008 They are called nested if statements and work fine. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-535889 Share on other sites More sharing options...
spillage Posted May 8, 2008 Author Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-535929 Share on other sites More sharing options...
robos99 Posted May 8, 2008 Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-535938 Share on other sites More sharing options...
wildteen88 Posted May 8, 2008 Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536119 Share on other sites More sharing options...
spillage Posted May 8, 2008 Author Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536144 Share on other sites More sharing options...
wildteen88 Posted May 8, 2008 Share Posted May 8, 2008 Is there more code to this? I cannot see how it is duplicating the rows from the database. Not unless your query is returning duplicate results maybe. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536235 Share on other sites More sharing options...
spillage Posted May 8, 2008 Author Share Posted May 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104690-using-if-in-if/#findComment-536344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.