Jump to content
BrainPill

three arrays in 1 foreach loop

Recommended Posts


I can not fix this script, because the output is not in sync. I have 3 arrays.

1) present column names.

2) prefix (fixed name).

3) suffix (uniqid value).

Can someone explain how exactly I should create a foreach loop in a foreach loop with 3 arrays and fetching the field value from the get_result command as the the value it is related to. This would result in:

<---->Present Column Name <-----> New Column Name

<---->value1 <--------------------------------> value1_abcd

<---->value2 <------------------------------> value2_abcd

etc.

 

<?php 
      $prefix_arr = array( 'col_a' , 'col_b' ,'col_c' , 'col_d', 
			'col_e'  , 'col_f' , 'col_g' , ) ;
			
				 $col2 = uniqid();  
				  $suffix = substr($col2, 5, 11);  
			
		$servername = "localhost";
		$username = "userx";
		$password = "pass12345";
		$dbname = "test_database";  
		  

		$conn = new mysqli($servername, $username, $password, $dbname);

		if ($conn->connect_error) {
			die("Connection failed: " . $conn->connect_error);
		}
			

			$stmt = $conn->prepare("SHOW COLUMNS	
					FROM table_xyz");
			$stmt->execute();
			
		$res = $stmt->get_result();
			 
				$pres_colname = $res;	
			//	var_dump($prefix_arr);
			 foreach ($prefix_arr as $los_ele){
				 $prefix = $los_ele;
			//	var_dump($prefix); 
			//	var_dump($suffix); 
				   
				$col_new = $prefix."_".$suffix;
				//var_dump($col_new);
				
				 foreach ($pres_colname as $val){
					
					 $col = $val['Field'];
			//	var_dump($col);
				?>
				 <br><input type="text" value ="
				 <?php echo $col;
				 ?>"><input type="text" value ="<?php echo $col_new;?>">
				<?php
				
		 $stmt = $conn->prepare("ALTER TABLE `table_xyz` CHANGE COLUMN `$col` `$col_new` VARCHAR(30) NOT NULL;"); 


		$stmt->execute();
				
			}
			}
					
		//$stmt->close();
				$conn->close(); ?>

The complicated thing this time is that I need the exact 'Field' value which is an output of get_result, so I can not fit it in.

 

 

  • Haha 1

Share this post


Link to post
Share on other sites

I'm not sure I understand.  What is $prefix_arr, what's the point of it?  If you're trying to get the list of column names and rename them, what's that array for?  You're already getting the column names and you have a suffix, so what do you need the other array for?

Share this post


Link to post
Share on other sites

This is what i meant

       $prefix_arr = array('col_a', 'col_b', 'col_c', 'col_d', 'col_e', 'col_f', 'col_g',);

          $col2 = uniqid();
          $suffix = substr($col2, 5, 11);
        
          $servername = "xxxxxxxx";
          $username = "xxxxxx";
          $password = "xxxxxxxx";
          $dbname = "test_database";

          $conn = new mysqli($servername, $username, $password, $dbname);

          if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
          } else {
          echo "Connection successful<br>";
          }

          $stmt = $conn->prepare("SHOW COLUMNS FROM table_xyz");
          $stmt->execute();

          $res = $stmt->get_result();
          $pres_colname = $res;

          foreach ($pres_colname as $key => $val) {

          $col = $val['Field'];
          $col_new = $prefix_arr[$key] . "_" . $suffix;
          
          ?>
          <br><input type="text" value ="<?php echo $col; ?>">
          <input type="text" value ="<?php echo $col_new; ?>">
          <?php
          $stmtAlter = $conn->prepare("ALTER TABLE `table_xyz` CHANGE COLUMN `$col` `$col_new` VARCHAR(30) NOT NULL;");


          $stmtAlter->execute();
          }

          $conn->close();

 

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...