Results 1 to 3 of 3

Thread: [PHP/MySQL] Switch - Case, how to shorten the code..

  1. #1
    dfense's Avatar
    4
    posts
    ~ The Great Below ~

    [PHP/MySQL] Switch - Case, how to shorten the code..

    Hi,

    Is this a bad way to code?? Probably it is..

    How can I shorten the code, so that if I have to make a change, I don't need to replace alot..?

    Here the '$svar_antall' can be 2-4, but if it gets bigger.. I'll have a big problem ..

    Code:
     
    / --------------------------------------------------------------------
    //Insert into database
    if($_POST){ 
    $svar_antall = $_POST['svar_antall'];
    $ny_test_ID = $_POST['svar_test_ID'];
    $ny_spm = $_POST['spm_spm_ID'];
    $svar_spmID = $_POST['svar_spmID'];
     
     
    switch($svar_antall){
    case 1:
    	$svar_1 = $_POST['svar_1'];
    	break;
    case 2;
    	$svar_1 = $_POST['svar_1'];
    	$svar_2 = $_POST['svar_2'];
    	break;
    case 3:
    	$svar_1 = $_POST['svar_1'];
    	$svar_2 = $_POST['svar_2'];
    	$svar_3 = $_POST['svar_3'];
    	break;
    case 4:
    	$svar_1 = $_POST['svar_1'];
    	$svar_2 = $_POST['svar_2'];
    	$svar_3 = $_POST['svar_3'];
    	$svar_4 = $_POST['svar_4'];
    	break;
    }
     
    if( (strlen($svar_1) == 0) || strlen($svar_2) == 0 ){
    	die ("Noen felt mangler!");
    }else {
    	switch($svar_antall){
    	 case 1:
    	 $svar_1 = $_POST['svar_1'];
    	 break;
    	 case 2;
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_1')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<br><div align=\"center\" class=\"tekst\"><strong>Svar 1</strong> og ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_2')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 2 lagret</strong></div><br>";
    	 }else
    		skriv_mysql_feil(); 
     
    	 break;
    	 case 3:
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_1')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<br><div align=\"center\" class=\"tekst\"><strong>Svar 1<strong>, ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_2')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 2</strong> og ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_3')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 3 lagret</strong></div><br>";
    	 }else
    		skriv_mysql_feil(); 
    	 break;
    	 case 4:
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_1')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<br><div align=\"center\" class=\"tekst\"><strong>Svar 1</strong>, ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_2')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 2</strong>, ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_3')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 3</strong>, ";
    	 }else
    		skriv_mysql_feil(); 
     
    	 $svar_spmID++;
     
    	 $query = "INSERT INTO Kyle_Svar(TestID, SvarKey, SpmID, SvarText) 
    	 VALUES('$ny_test_ID', '$svar_spmID', '$ny_spm', '$svar_4')";
     
    	 if ($result2 = mysql_query($query)){
    	 echo "<strong>Svar 4 lagret</strong></div><br>";
    	 }else
    		skriv_mysql_feil(); 
    	 break;
    	}
     
     
    }
     
    }
    Thanx

  2. #2
    That's a lot of code to go through, but your first switch-case statement could be optimized to the following:

    Code:
    case 4:
    	$svar_4 = $_POST['svar_4'];
    
    case 3:
    	$svar_3 = $_POST['svar_3'];
    
    case 2;
    	$svar_2 = $_POST['svar_2'];
    
    case 1:
    	$svar_1 = $_POST['svar_1'];
    	break;

    I'll try and make a second post for the rest of the code


    [edit]
    I went through the code and I really couldn't predict what it's supposed to do...

  3. #3
    dfense's Avatar
    4
    posts
    ~ The Great Below ~

    Ah..

    Ah.. thanx alot!.. It would be great if you could help me with the rest!

    [EDIT]
    I probably can change the code as you did on the first Switch Case?

    Quote Originally Posted by ahmed
    That's a lot of code to go through, but your first switch-case statement could be optimized to the following:

    Code:
    case 4:
    	$svar_4 = $_POST['svar_4'];
     
    case 3:
    	$svar_3 = $_POST['svar_3'];
     
    case 2;
    	$svar_2 = $_POST['svar_2'];
     
    case 1:
    	$svar_1 = $_POST['svar_1'];
    	break;

    I'll try and make a second post for the rest of the code
    Last edited by dfense; February 26th, 2005 at 12:19 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012