Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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