bakhtn Posted December 16, 2011 Share Posted December 16, 2011 //if any items in order table then display all items in order table $order_sql = "select * from orders"; $order_result = $handle -> query( $order_sql ); if($order_result->num_rows) { echo '<table bgcolor=#FFFF66 ><td>'; while ($order = $order_result -> fetch_assoc()) { $category_sql = "select category from category where category_id = {$order['category_id']}"; $category_result = $handle -> query( $category_sql ); $category_name = $category_result -> fetch_row(); $menu_sql = "select item_name from menu where item_id = {$order['item_id']}"; $menu_result = $handle -> query( $menu_sql ); $menu_name = $menu_result -> fetch_row(); echo $category_name[0] . ': ' . $menu_name[0] . ' ' . $order['size'] . ' ' . $order['type'] . ' ' . $order['amount'] . '<br />'; //insert data into order.txt file for print $file_insert = $category_name[0] . ": " . $menu_name[0] . " " . $order['size'] . " " . $order['type'] . " " . $order['amount'] . "\r\n"; @ $fp = fopen( 'order.txt', 'ab' ); if($fp) { fwrite( $fp, $file_insert, strlen( $file_insert ) ); } else { echo 'could not insert data into order.txt file for print, CLICK ON NEW ORDER'; } fclose($fp); } Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/ Share on other sites More sharing options...
melloorr Posted December 16, 2011 Share Posted December 16, 2011 Try closing the While statement before the if statement Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/#findComment-1298684 Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 Well, you should also move the fopen() outside the while() loop as well. Also, you should not run a query inside the while loop. Instead run ONE query to get all the needed info. Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/#findComment-1298690 Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 //if any items in order table then display all items in order table $order_sql = "SELECT o.size, o.type, o.amount, c.category, m.item_name FROM orders AS o JOIN category AS c USING(category_id) JOIN menu AS m USING(item_id)"; $order_result = $handle->query($order_sql); if($order_result->num_rows) { echo "<table bgcolor='#FFFF66'>\n"; echo "<tr>\n"; echo "<th></th>\n"; echo "<th></th>\n"; echo "<th></th>\n"; echo "<th></th>\n"; echo "<th></th>\n"; echo "<\tr>\n"; while ($order = $order_result -> fetch_assoc()) { echo "<tr>\n"; echo "<td>$order['category']}</td>\n"; echo "<td>{$order['item_name']}</td>\n"; echo "<td>{$order['size']}</td>\n"; echo "<td>{$order['type']}</td>\n"; echo "<td>{$order['amount']}</td>\n"; echo "</tr>\n"; //Append insert data to write into order.txt file $file_insert .="{$order['category']}: {$order['item_name']} {$order['size']} {$order['type']} {$order['amount']}\r\n"; } echo "</table>\n"; @$fp = fopen( 'order.txt', 'ab' ); if($fp) { fwrite($fp, $file_insert, strlen($file_insert) ); fclose($fp); } else { echo 'could not insert data into order.txt file for print, CLICK ON NEW ORDER'; } } Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/#findComment-1298694 Share on other sites More sharing options...
bakhtn Posted December 17, 2011 Author Share Posted December 17, 2011 I needed to have the sql statements with in the loop, I needed one row for each row of data , its a relational database. and the while loop does close in the actual script, Ive found a way round it, but why does this code produce a duplicate data in the file, and not when outputted to the browser. I just want to know why. Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/#findComment-1298854 Share on other sites More sharing options...
Psycho Posted December 17, 2011 Share Posted December 17, 2011 I needed to have the sql statements with in the loop, I needed one row for each row of data , its a relational database. No you do not need to run the query within the loop. The whole point of a relational database is that you can 'relate' the data, for example, using a JOIN. Your initial code was terribly inefficient. 1. Again, no need to run those queries in a loop. Simply do ONE query with appropriate JOINs to get all the data you need. 2. Do not open/close the file within the while loop. Just open it once, write your data, then close it one. I really don't see why your original code would write duplicates, but suspect it might have something to do with opening/closing the file repeatedly. If it isn't hat then you just might have duplicate data. Quote Link to comment https://forums.phpfreaks.com/topic/253337-confused-im-gettinbg-double-entries-in-text-file/#findComment-1298872 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.