Jump to content

Recommended Posts

I have a random exercise picker which allows the user to "okay" various choices which my script then picks from. It works fine and what I'm here to inquire about is if there's any way I can shorten the code. I imagine there's something I could do with the array_push sections since it's so repetitive.

If you want to see what the script is doing, it's at my site:

http://www.julianbrezon.com/exercises.php

<?php
/* Theory: 
Check to see if each key, individually is true. 
If it is true, array_push it's value into the $keys array.
Count the number of objects in that array.
Use the random picker but use the number of items instead of 12. 
*/
//----------
//
//Create key array
$key = array();
//Check to see if box is checked. If it is checked, add it to the array:
if ($_POST["c"] == "true"){
array_push($key, "C");
}
else {}
if ($_POST["f"] == "true"){
array_push($key, "F");
}
else {}
if ($_POST["bb"] == "true"){
array_push($key, "Bb");
}
else {}
if ($_POST["eb"] == "true"){
array_push($key, "Eb");
}
else {}
if ($_POST["ab"] == "true"){
array_push($key, "Ab");
}
else {}
if ($_POST["c#"] == "true"){
array_push($key, "C#");
}
else {}
if ($_POST["f#"] == "true"){
array_push($key, "F#");
}
else {}
if ($_POST["b"] == "true"){
array_push($key, "B");
}
else {}
if ($_POST["e"] == "true"){
array_push($key, "E");
}
else {}
if ($_POST["a"] == "true"){
array_push($key, "A");
}
else {}
if ($_POST["d"] == "true"){
array_push($key, "D");
}
else {}
if ($_POST["g"] == "true"){
array_push($key, "G");
}
else {}
//iterations 
$iterations = $_POST["iterations"];
//Count the number of objects in the array 
$howmanykeys = count($key);
//How many iterations?
for ($i=0; $i < $iterations; $i++){
// Choose the exercise
$exercise=array("Zero",""Up a Chord"",""Up a Chord, Down a Scale"",""Up a Chord Down a Chord"",""Winding"",""Up a Triad"",""Half Step to Triads"",""Third of Triad Up"",""Third of Triad Down"");
$whatex = (mt_rand()%8+1);
$whatkey = (mt_rand(0, $howmanykeys - 1));
// Print the exercises!!
echo "Perform the " . $exercise[$whatex] . " exercise in the key of " . $key[$whatkey] . "<br />";
sleep(.5);
}	
?>

Link to comment
https://forums.phpfreaks.com/topic/203517-streamlining-post-checks/
Share on other sites

$checkBoxes = array("c", "f", "bb"); // etc
foreach ($checkBoxes as $key) {
    if (isset($_POST[$key]) && $_POST[$key] == "true") {
        array_push($key, ucwords($key));
    }
}

// iterations

 

One way to shorten it. You also do not need "else {}" after every if statement. It is not a required piece.

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.