ash55 Posted March 16, 2009 Share Posted March 16, 2009 Hi I'm new to php. I'm trying to select a grouped radio button from a form to retrieve stuff from my database, but regardless of which radio button i select, it seem to only retrieve stuff from one table. In other words, my radio button doesn't work. My form: <form action="weightloss_beg_3day_exercise.php" method="POST"> <font color="#D3D3D3" size="2" face="Verdana"><b>What is your goal?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Weight Loss:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="group1" value="weightloss" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Tone:</b></font></td> <td><input type="radio" name="group1" value="tone"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Muscle Building:</b></font></td> <td><input type="radio" name="group1" value="musclebuilding"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>Are you a/an?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Beginner:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="group2" value="beginner" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Intermediate:</b></font></td> <td><input type="radio" name="group2" value="intermediate"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>How many days can you train per week?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>3 Days:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="group3" value="3days" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>4 Days:</b></font></td> <td><input type="radio" name="group3" value="4days"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>5 Days:</b></font></td> <td><input type="radio" name="group3" value="5days"></td> </tr> <tr> <td align="center"><input type="submit" name="Submitcreateworkout" value="Get Program"></td> </tr> </table> </form> [b]My PHP code:[/b] <?php error_reporting(E_ALL ^ E_NOTICE); include "../config.php"; $connection = mysql_connect($hostname,$user,$pass) or die("Could not connect: ".mysql_error()); mysql_select_db($database,$connection) or die("Error in selecting the database:".mysql_error()); #$sql_result = mysql_query($query,$connection) or exit("Sql Error".mysql_error()); //select radio buttons for weightloss, begginer or intermediate and 3,4,5 days program $abutton_status = 'unchecked'; $bbutton_status = 'unchecked'; $cbutton_status = 'unchecked'; $dbutton_status = 'unchecked'; $ebutton_status = 'unchecked'; $fbutton_status = 'unchecked'; $gbutton_status = 'unchecked'; $hbutton_status = 'unchecked'; $ibutton_status = 'unchecked'; $jbutton_status = 'unchecked'; $kbutton_status = 'unchecked'; $lbutton_status = 'unchecked'; $mbutton_status = 'unchecked'; $nbutton_status = 'unchecked'; $obutton_status = 'unchecked'; $pbutton_status = 'unchecked'; $qbutton_status = 'unchecked'; $rbutton_status = 'unchecked'; if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1'] && $_POST['group2'] && $_POST['group3']); if ($selected_radio == ('weightloss' && 'beginner' && '3days')) { $abutton_status = 'checked'; } echo "<b>Your weightloss program for day 1 is: </b><br><br>"; $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query, $connection)or exit("Sql Error".mysql_error()); while ($row = mysql_fetch_array($sql_result)) { echo "<table width =\"50%\ align =\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"font-size: 70%\">"; echo "<tr>"; echo "<td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">"; echo "<a href=\"".$row['link'].".php\">".$row['name']."</a>"; #echo "<a href=\"popup(".$row['link'].".php)\">".$row['name']."</a>"; #echo "<A HREF=\".$row['link']." onClick=\"return popup(".$row['link'].".php)"\>".$row['name']."</A>"; echo "</td>"; echo "<td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">"; echo $row['sets']; echo "</td>"; echo "<td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">"; echo $row['reps']; echo "</td>"; echo "<td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">"; echo $row['duration']; echo "</td>"; echo "</tr>"; echo "</table><br>"; } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('weightloss' && 'beginner' && '4days')) { $bbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercise"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('weightloss' && 'beginner' && '5days')) { $cbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('weightloss' && 'intermediate' && '3days')) { $dbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('weightloss' && 'intermediate' && '4days')) { $ebutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('weightloss' && 'intermediate' && '5days')) { $fbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } //select radio buttons for tone, begginer or intermediate and 3,4,5 days program else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'beginner' && '3days')) { $gbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'beginner' && '4days')) { $hbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'beginner' && '5days')) { $ibutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'intermediate' && '3days')) { $jbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'intermediate' && '4days')) { $kbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('tone' && 'intermediate' && '5days')) { $lbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } //select radio buttons for muscle building, begginer or intermediate and 3,4,5 days program else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'beginner' && '3days')) { $mbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exerci"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'beginner' && '4days')) { $nbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'beginner' && '5days')) { $obutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'intermediate' && '3days')) { $pbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'intermediate' && '4days')) { $qbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } else if (isset($_POST['Submitcreateworkout'])) { $selected_radio = ($_POST['group1']) && ($_POST['group2']) && ($_POST['group3']); if ($selected_radio == ('musclebuilding' && 'intermediate' && '5days')) { $rbutton_status = 'checked'; } $query = "SELECT * FROM weightloss_beg_3day_exercis"; $sql_result = mysql_query($query); while ($row = mysql_fetch_array($sql_result)) { printf("ID: %s Name: %s", $row[0], $row[1]); } } Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 1. Use code tags when positing code. makes it MUCH easier to read. 2. Are you kidding me with all those if/else's 3. All of your IF statemetns are if (isset($_POST['Submitcreateworkout'])) { The submit button is the field named 'Submitcreateworkout', so that IF will always return true and only the first one will run since all of the others are ELSE clauses. I'll take a stab at correcting the code. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 Ok, I've made a lot of changes. You should really give variables descriptive names - not group1, group2, etc. I used 'goal', 'level' and 'days'. I also changed the values to values you can use directly in the code without a lot of IF/ELSE statements. I noticed that all of the queries were using a table with 'beg' in the name and there were no intermediate tables (I assumed that was a mistake and that there are tables with 'int' in the name). Although, I rewrote the code using mutiple tables as you had in the original code, I will state that that is a terrible idea. Simply create one table with additional fields for goal, level & days. You can then query that one table using those fields as criteria. Using multiple tables is very poor design in this case. Note: I did not test any of this, so there are probably some syntax errors New Form <form action="weightloss_beg_3day_exercise.php" method="POST"> <font color="#D3D3D3" size="2" face="Verdana"><b>What is your goal?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Weight Loss:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="goal" value="weightloss" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Tone:</b></font></td> <td><input type="radio" name="goal" value="tone"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Muscle Building:</b></font></td> <td><input type="radio" name="goal" value="musclebuilding"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>Are you a/an?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Beginner:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="level" value="beg" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Intermediate:</b></font></td> <td><input type="radio" name="level" value="int"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>How many days can you train per week?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>3 Days:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="days" value="3" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>4 Days:</b></font></td> <td><input type="radio" name="days" value="4"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>5 Days:</b></font></td> <td><input type="radio" name="days" value="5"></td> </tr> <tr> <td align="center"><input type="submit" name="Submitcreateworkout" value="Get Program"></td> </tr> </table> </form> New PHP <?php if (isset($_POST['Submitcreateworkout'])) { $query = "SELECT * FROM {$goal}_{$level}_{$days}day_exercise"; $result = mysql_query($query) or die (mysql_error()); $day = 1; while ($row = mysql_fetch_array($sql_result)) { echo "<b>Your weightloss program for day {$day} is:</b><br><br>"; echo "<table width =\"50%\ align =\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"font-size: 70%\">\n"; echo " <tr>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n"; echo " <a href=\"{$row['link']}.php\">{$row['name']}</a>"; #echo " <a href=\"popup(".$row['link'].".php)\">".$row['name']."</a>"; #echo " <A HREF=\".$row['link']." onClick=\"return popup(".$row['link'].".php)"\>".$row['name']."</A>"; echo " </td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['sets']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['reps']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['duration']}</td>\n" echo " </tr>"; echo "</table><br>\n"; } } ?> Wow, look how much smaller that is. If you were to go with one table, as I think you really should, the query would look something like this: $query = "SELECT * FROM programs WHERE goal='$goal' AND level='$level' AND days='$days'"; Much, much easier to maintain. Quote Link to comment Share on other sites More sharing options...
ash55 Posted March 19, 2009 Author Share Posted March 19, 2009 Thanks a million for the help. Are you suggesting i use on table for all my workout programs(all the exercises for each workout program) ? Well the aim of this web app is, when the user selects his goal, level and number of days he wishes to train and on clicking on the submit button it searches through the database and returns their workout. I have adjusted the program table in my database and i have attached it to this post for you to take a look at. In case you are wondering what the goaltype field is: purpose of the goaltype field in the database program table is for the user to be able to get an alternative goal exercise or workout. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2009 Share Posted March 19, 2009 Yes, I am suggesting you use one table for ALL of the workout programs. Having mutiple tables doesn't make sense. Part of the power of a database is the ability to extract ONLY the data that you want. In this case you could query the one table with all the workout programs and filter it by goal, level, etc. In fact, I would imagine that you have some of the same workouts for different goals, levels, etc. In that case to "normalize" the database you would have one table of each unique workout. Then in separate tables you would associate each workout to goals, levels, days. as appropriate. For example, the bench press exercise could be associated with weight loss & strength trainign goals, beginner, intermediate & advanced levels, and 3 & 5 day workouts. The table structure would look like this exercises id | name | sets | reps | minutes 1 | bench press | 10 | 10 | 20 goals exercise_id | goal 1 | weight loss 1 | strength training (note: you could have a separate table to translate the goal_ids into their names or handle in the PHP code) levels exercise_id | level 1 | beginner 1 | intermediate 1 | advanced days exercise_id | days 1 | 3 1 | 5 Of course this may mean a lot of rework. So, decide if its worth it to you to take this approach. Also, if you have a field named sets, do not put "3 sets" as the value. Just use 3. Quote Link to comment Share on other sites More sharing options...
ash55 Posted March 19, 2009 Author Share Posted March 19, 2009 I have decided to go with using one table for all the exercises but so far my query returns a blank page. This is the code: <?php error_reporting(E_ALL ^ E_NOTICE); include "../config.php"; $connection = mysql_connect($hostname,$user,$pass) or die("Could not connect: ".mysql_error()); mysql_select_db($database,$connection) or die("Error in selecting the database:".mysql_error()); if (isset($_POST['Submitcreateworkout'])) { #$query = "SELECT * FROM {$goal}_{$level}_{$days}day_exercise"; $query = "SELECT * FROM programs WHERE goal='$goal' AND level='$level' AND days='$days'"; $result = mysql_query($query) or die (mysql_error()); $day = 1; while ($row = mysql_fetch_array($result)) { echo "<b>Your weightloss program for day {$day} is:</b><br><br>"; echo "<table width =\"50%\ align =\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"font-size: 70%\">\n"; echo " <tr>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n"; echo " <a href=\"{$row['link']}.php\">{$row['exercisename']}</a>"; #echo " <a href=\"popup(".$row['link'].".php)\">".$row['name']."</a>"; #echo " <A HREF=\".$row['link']." onClick=\"return popup(".$row['link'].".php)"\>".$row['name']."</A>"; echo " </td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['sets']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['reps']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['duration']}</td>\n"; echo " </tr>\n"; echo "</table><br>\n"; } } ?><br><br> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.