Failing_Solutions Posted October 12, 2012 Share Posted October 12, 2012 (edited) First I want to say the new forum design looks nice The situation, I need to write text files for some labels. Each lable can have a max of 11 records printed on it, if more records are in the query results then I need to create new lable, which is done by writing a new text file. I have got to the point of my code working, provided I supress the offeset error warning. Which you can see in the code "@$lable_string" I was hoping some PHP guru's would like to take a look at this and see if I can do something better. Thank you in advance. <?php ///Get the Dynamic SQL from the page & the record count $sql=trim($_POST['displayed_sql']); $count=trim(intval($_POST['amount'])); //Getting Amount of Labels To Create $label_qty=floor($count / 11); if(($count % 11)>0) { $label_qty++; } //Get Data From SQL statement $query=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($query)){ $record[]=$row['received_id']; $part_number[]=$row['part_number']; $location[]=$row['location']; $qty[]=$row['qty_requested']; } $operator=$_COOKIE['ID_my_site']; //Get Users Printer Location $user_print_loc_sql="SELECT default_printer_location FROM operators where operator='$operator'"; $user_print_loc_query=mysql_query($user_print_loc_sql) or die(mysql_error()); while($user_row=mysql_fetch_array($user_print_loc_query)){ $default_printer=$user_row['default_printer_location']; } //Get Printers $printer_sql="SELECT * FROM printers Where printer_location_id ='$default_printer'"; $printer_query=mysql_query($printer_sql) or die(mysql_error()); while ($printer_row=mysql_fetch_array($printer_query)){ $printer_name=$printer_row['printer_name']; $printer_description=$printer_row['printer_location_description']; $printer_file_path=$printer_row['printer_path']; } //Start Writing Text Label Files //set pre counter $t=0; for ($n=0; $n<$label_qty; $n++){ //Set variable for string that clears each time through $lable_string=''; $newpartFile= $printer_file_path.'picking'.$n.'.txt'; $fh=fopen($newpartFile,'x+') or die ('Could Not Open New Lable File'); for($x=$t; $x<($t+11); $x++){ @$lable_string.=$record[$x] . ','. $part_number[$x] .','. $location[$x] .','. $qty[$x] .','; } //advance variable by 11 $t=($t+11); $lable_string=trim($lable_string); fwrite($fh,$lable_string) or die('Failed While Trying to Write The Lable File'); fclose($fh); } $good_message='<div class="shadeGood" style="text-align:center;">Successfully Printed '.$label_qty.' Lables</div>'; } ?> Edited October 12, 2012 by Failing_Solutions Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 The first thing you should do is run a case-insensitive find and replace and change all of your lable to label. /Get Data From SQL statement $query=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($query)){ $record[]=$row['received_id']; $part_number[]=$row['part_number']; $location[]=$row['location']; $qty[]=$row['qty_requested']; } Rather than making all these arrays, just stick with a multi-dimensional assoc array, then do a foreach on it, not a for. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2012 Share Posted October 12, 2012 I haven't read through all the code in detail, but I think there is an easy solution: array_chunk() Just dump each record as a sub array into the main array. Then use array_chunk() to split the array into sub-arrays containing 5 records each. Quote Link to comment Share on other sites More sharing options...
kicken Posted October 12, 2012 Share Posted October 12, 2012 $operator=$_COOKIE['ID_my_site']; //Get Users Printer Location $user_print_loc_sql="SELECT default_printer_location FROM operators where operator='$operator'"; $user_print_loc_query=mysql_query($user_print_loc_sql) or die(mysql_error()); while($user_row=mysql_fetch_array($user_print_loc_query)){ $default_printer=$user_row['default_printer_location']; } //Get Printers $printer_sql="SELECT * FROM printers Where printer_location_id ='$default_printer'"; $printer_query=mysql_query($printer_sql) or die(mysql_error()); while ($printer_row=mysql_fetch_array($printer_query)){ That block of code could be reduced to this: $operator=$_COOKIE['ID_my_site']; //Get Users Printer Location $user_print_loc_sql="SELECT printers.* FROM operators INNER JOIN printers ON operators.default_printer_location=printers.printer_location_id WHERE operator='$operator'"; $printer_query=mysql_query($user_print_loc_sql) or die(mysql_error()); while ($printer_row=mysql_fetch_array($printer_query)){ It joins the tables and selects the printer info for the default printer in a single query rather than two. Quote Link to comment Share on other sites More sharing options...
Failing_Solutions Posted October 12, 2012 Author Share Posted October 12, 2012 Thank you @Psycho & @kicken your advice helps. I forgot about array_chunk and that seems just what I need here, also your psycho is correct on the query overhead thank you both. Quote Link to comment 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.