php - Display href from a table based on checkbox values -
i new php. aware using mysql*functions, continue use them project. looking guidance on how display videos based on checkbox values inserted database. have 14 checkboxes, named 1, 2, 3, 4, 5 etc...(which insert enisatid column). name of each checkbox inserts 'enisatid' (from enisatanswer) column (1-14), value of 1 or 0 'enisat_watch' depending if checkbox has been selected or not.
my first piece of code works fine , inserts database. follows:
<?php session_start(); include_once 'dbconnect.php'; if(!isset($_session['user'])) { header("location: index.php"); } $res=mysql_query("select * users user_id=".$_session['user']); $userrow=mysql_fetch_array($res); if(isset($_post['submit'])) { header("location: enisatvids.php"); $userid=$_session['user']; $cb_names = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'); foreach ($cb_names $cb) { $cb_val = isset($_post['$cb']) ? 1 : 0; $sql = "insert enisatanswer (user_id, enisatid, enisat_watch) values ('$userid', '$cb', $cb_val)"; mysql_query($sql) or die(mysql_error()); } if($query==true) { echo'<script>alert("your choices have inserted \n \n please click on display enisat tutorials @ buttom of page view videos ")</script>'; } else { echo'<script>alert("failed insert")</script>'; } } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>welcome - <?php echo $userrow['username']; ?></title> /<link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="header"> <div id="left"> <label>nhsct enisat tutorials</label> </div> <div id="right"> <div id="content"> welcome <?php echo $userrow['forename']; ?> <a href="logout.php?logout">sign out</a> </div> </div> </div> <br> <p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg"> <br> <br> <center> <h2>please select tasks require assistance with, before clicking display enisat tutorials:<h2> <br> <table align="center" height="0" width="70%" border="1" bgcolor = "white"> <form action="" method="post" <tr> <td colspan="2">tick each relevant box:</td> </tr> <tr> <td>how login</td> <td><input type="checkbox" name="1" value="1"></td> <tr> <td>how manage worktray</td> <td><input type="checkbox" name="2" value="1"></td> <tr> <td>how change visual settings (colours , text size)</td> <td><input type="checkbox" name="3" value="1"></td> </tr> <tr> <td>how change own password on system</td> <td><input type="checkbox" name="4" value="1"></td> </tr> <tr> <td>how logout of system</td> <td><input type="checkbox" name="5" value="1"></td> </tr> <tr> <td>how search client on system</td> <td><input type="checkbox" name="6" value="1"></td> </tr> <tr> <td>how start assessment</td> <td><input type="checkbox" name="7" value="1"></td> </tr> <tr> <td>how finalise assessment</td> <td><input type="checkbox" name="8" value="1"></td> <tr> <td>how print assessment</td> <td><input type="checkbox" name="9" value="1"></td> </tr> <tr> <td>how create client , referral manually through find on h+c</td> <td><input type="checkbox" name="10" value="1"></td> </tr> <tr> <td>how submit referral lcid (lcid users only)</td> <td><input type="checkbox" name="11" value="1"></td> </tr> <tr> <td>how submit referral soscare (soscare users only)</td> <td><input type="checkbox" name="12" value="1"></td> </tr> <tr> <td>how reassign referral on enisat</td> <td><input type="checkbox" name="13" value="1"></td> </tr> <tr> <td>how close referral on enisat</td> <td><input type="checkbox" name="14" value="1"></td> </tr> <tr> <td <td><button name="submit" type="submit" onclick="window.location.href='enisatvids.php'">display enisat tutorials</button></td> </tr> </table> </div> </form> </body> </html>
can me second piece of code. have matched enisatid (checkbox name enisatanswer) enisatid (primary key enisatquestion uniquely identify row video) foreign key.
my table checkbox responses has following structure:
enisatanswersid
(auto-increment)- 'user_id' (users id logged on)
enisatid
(i want name of checkbox, 1-14)enisat_watch
(value of 1 or 0 depending if checkbox has been selected or not)
my table videos table has following stucture:
-'enisatid' (primary key - values 1-14) -'enisatquestion' (text forms wording on href) -'enisatvideo' (url video stored on server)
** when use simple query display of videos table, display fine, wish dispay videos relating checkbox selected , depending on user_id of logged on user. feel select query issue, can advise.
here code display videos enisatquestion table based on checkbox values inserted. have tried 2 different queries shown. 1 commented out //. **
<?php session_start(); include_once 'dbconnect.php'; if( !isset( $_session['user'] ) ) header("location: index.php"); $res=mysql_query("select * users user_id=".$_session['user']); $userrow=mysql_fetch_array( $res ); $userid=$_session['user']; $query = "select enisatquestion, enisatvideo enisatquestion inner join enisatanswer enisatanswer.enisatid = enisatquestion.enisatid , user_id = $userid , enisat_watch = 1"; /* default message if query fails or there no records */ $enisatquestion='<h2>sorry, there no records</h2>'; if( $result ) {/* if there recordset, proceed , generate html table */ $enisatquestion = "<table >"; while ( $row = mysql_fetch_assoc($result) ) { $enisatquestion .= "<tr><td><a href='{$row['enisatvideo']}'>{$row['enisatquestion']}</a></td></tr>"; } $enisatquestion .= "</table>"; } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>welcome - <?php echo $userrow['username']; ?></title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="header"> <div id="left"> <label>nhsct enisat tutorials</label> </div> <div id="right"> <div id="content"> welcome <?php echo $userrow['forename']; ?> <a href="home.php?home">return homepage</a> <a href="logout.php?logout">sign out</a> </div> </div> <br> <br> <br> <br> <p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg"> <br> <br> <center> <h2>click on each link open tutorial in windows media player<h2> <br> <?php /* output html table here, below header */ echo $enisatquestion; /* if query failed default gets displayed */ ?> </div> </body> </html>
it useful if post happens when run code first sql query (the second appears invalid, , missing join clause).
to ensure nothing in code @ fault, run sql queries directly against database first, ("hard-coding" dynamic, form-based data simulate actual, user-generated query).
if you're on os/x or linux using mysql client run query tell if query @ fault. otherwise use phpmyadmin (or whatever other gui-based tool wish) achieve same thing.
once database returns data expect, modify php code generate correct sql query, , work there.
Comments
Post a Comment