Jump to content

Php Loop In A Loop Optimization Help Request


Failing_Solutions

Recommended Posts

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 by Failing_Solutions
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

	$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.

Link to comment
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.