Is this a case for a for loop

I have

	try{
		$sql = "SELECT bay_id,x_coord,y_coord,width,height FROM bays WHERE column_id = ".$_POST['row_id'];   
		
		echo $sql."<br>";

		$result = $pdo->query($sql);

		if($result->rowCount() > 0){
			while($row = $result->fetch()){
					echo "old coordinates: " . (int)$row['x_coord'].','. (int)$row['y_coord']."<br>";
				
				try{
					$sql = "UPDATE bays SET x_coord = ".$new_x.", y_coord = ".$new_y.",width = ".$new_width.",height  = ".$new_height.", updated_by = '".$_SESSION['email']."',updated_date = CURRENT_TIMESTAMP WHERE column_id = ".$_POST['row_id'];    

					echo $sql."<br>";

producing

SELECT bay_id,x_coord,y_coord,width,height FROM bays WHERE column_id = 1
old coordinates: 153,99
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,117
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,135
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,153
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,171
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,189
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,207
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,225
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,243
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,261
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,279
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,297
UPDATE bays SET x_coord = 203, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1

since I want to increase the x_coord on all the UPDATE statements by 18, should I just wrap it in a foreach loop?

Just use one UPDATE query - UPDATE bays SET x_coord = x_coord + 18 the rest of your query here…

Why are you updating the y_coord and the dimension columns? If there is no change in their values, just leave them out of the query.

2 Likes

I can put a + in my query like?

UPDATE bays SET x_coord = $x_coord + 18, y_coord = 99,width = 18,height = 24, updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1

and ill get rid of the y aspect

Just x_coord = x_coord + 18, no need for any $. This is pure SQL, which can do calculations too without any need for PHP.

then I dont understand this output

UPDATE columns SET name = ‘One’, rotated = 1,width = 28, height = 220, x_coord = 201, y_coord = 0,updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
SELECT bay_id,x_coord,y_coord,width,height FROM bays WHERE column_id = 1
old coordinates: 153,99
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,117
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,135
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,153
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,171
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,189
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,207
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,225
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,243
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,261
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,279
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1
old coordinates: 153,297
UPDATE bays SET x_coord = x_coord + 18, y_coord = 2,width = 18,height = 24,\ updated_by = ‘ssam@industechnology.com’,updated_date = CURRENT_TIMESTAMP WHERE column_id = 1

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.