hyster Posted January 21, 2013 Share Posted January 21, 2013 im using the code below as part of a larger script but im getting each result echo twice i carnt see where i gone wrong im 99% sure the problem is in this piece but it looks fine to me. think i need fresh eyes lol. thx <?php $sql7 = "SELECT * FROM tanks JOIN tanks_own ON tanks.list = tanks_own.tanks WHERE tanks_own.player ='$player'"; $result7=mysql_query($sql7); while($row7 = mysql_fetch_array( $result7 )) { ?> <option value="<?php echo $row7['list']; ?>" selected="selected"><?php echo $row7['tier']." - ".$row7['name']; ?></option> <?php } ?> full code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><HTML><HEAD> <?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../phpbb3/'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup(); if ($user->data['user_id'] == ANONYMOUS) { echo 'Please login!'; } else { echo 'logged in as, ' . $user->data['username_clean']; } $player = $user->data['username_clean']; //database connection details $dbname =""; $username =""; $password =""; mysql_connect("localhost", "$username", "$password") or die(mysql_error()); mysql_select_db("$dbname") or die(mysql_error()); //do not edit below ?> </p> <p> </p> <form id="form1" name="form1" method="POST" action="list.php"> <div align="center"> <table width="200" border="1"> <tr> <td>UK</td> <td>USA</td> <td>German</td> <td>Soviet</td> <td>French</td> <td>Chinese</td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><label> <input name="country[]" type="checkbox" id="country" value="uk" /></label></td> <td><input name="country[]" type="checkbox" id="country" value="usa"/></td> <td><input name="country[]" type="checkbox" id="country" value="german" /></td> <td><input name="country[]" type="checkbox" id="country" value="soviet" /></td> <td><input name="country[]" type="checkbox" id="country" value="french" /></td> <td><input name="country[]" type="checkbox" id="country" value="chinese" /></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Light</td> <td>Medium</td> <td>Heavy</td> <td>TD</td> <td>SPG</td> </td> </tr> <tr> <td><input name="type[]" type="checkbox" id="type" value="light" /></td> <td><input name="type[]" type="checkbox" id="type" value="medium" /></td> <td><input name="type[]" type="checkbox" id="type" value="heavy" /></td> <td><input name="type[]" type="checkbox" id="type" value="td" /></td> <td><input name="type[]" type="checkbox" id="type" value="spg" /></td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td>6</td> <td>7</td> <td>8</td> <td>9</td> <td>10</td> </tr> <tr> <td><input name="tier[]" type="checkbox" value="I" /></td> <td><input name="tier[]" type="checkbox" value="II" /></td> <td><input name="tier[]" type="checkbox" value="III" /></td> <td><input name="tier[]" type="checkbox" value="IV" /></td> <td><input name="tier[]" type="checkbox" value="V" /></td> <td><input name="tier[]" type="checkbox" value="VI" /></td> <td><input name="tier[]" type="checkbox" value="VII" /></td> <td><input name="tier[]" type="checkbox" value="VIII" /></td> <td><input name="tier[]" type="checkbox" value="IX" /></td> <td><input name="tier[]" type="checkbox" value="X" /></td> </tr> <tr><td><input type="submit" id="submit" /></td> </tr></table> </div> </form> <p> </p> <div align="center"> <SCRIPT type="text/javascript" src="mixed2b.js"></SCRIPT> <SCRIPT type="text/javascript"> window.onload = setSubmitDebugOuput; function setSubmitDebugOuput() { document.getElementById('divOutput').innerHTML = window.location.search; } // Technique 1 function placeInHidden(delim, selStr, hidStr) { var selObj = document.getElementById(selStr); var hideObj = document.getElementById(hidStr); hideObj.value = ''; for (var i=0; i<selObj.options.length; i++) { hideObj.value = hideObj.value == '' ? selObj.options[i].value : hideObj.value + delim + selObj.options[i].value; } } // Technique 2 function selectAllOptions(selStr) { var selObj = document.getElementById(selStr); for (var i=0; i<selObj.options.length; i++) { selObj.options[i].selected = true; } } //--> </SCRIPT> <SCRIPT type="text/javascript"> _uacct = "UA-83058-1"; urchinTracker(); </SCRIPT> <META name="GENERATOR" content="MSHTML 9.00.8112.16457"></HEAD> <BODY> <DIV id="content"> <DIV id="innercontent"> <P></P> <TABLE border="0" align="center"> <TBODY> <TR> <TD> Hidden field<BR> <FORM onsubmit="placeInHidden('.', 'sel3', 'hide2');" method="post" action="insert_tanks.php"><SELECT id="sel3" multiple="multiple" size="5"> <?php $whereclause = ''; $where = array(); if (isset($_POST['country'])) { $countrylist = join ("','", $_POST['country']); $where[] = "(country IN ('$countrylist'))"; } if (isset($_POST['type'])) { $typelist = join ("','", $_POST['type']); $where[] = "(type IN ('$typelist'))"; } if (isset($_POST['tier'])) { $tierlist = join ("','", $_POST['tier']); $where[] = "(tier IN ('$tierlist'))"; } if (count($where) > 0) $whereclause = 'WHERE ' . join(' AND ', $where); $sql = "SELECT * FROM tanks $whereclause"; $result=mysql_query($sql); while($row = mysql_fetch_array( $result )) { ?> <option value="<?php echo $row['list']; ?>"><?php echo $row['tier']." - ".$row['name']; ?></option> <?php } ?> </SELECT><INPUT id="hide2" name="hide2Name" type="hidden"><BR></FORM></TD> <TD vAlign="middle" align="center"> <FORM><INPUT onclick="moveOptions(document.getElementById('sel3'), document.getElementById('sel4'));" value="-->" type="button"><BR><INPUT onclick="moveOptions(document.getElementById('sel4'), document.getElementById('sel3'));" value="<--" type="button"></FORM></TD> <TD> <FORM onsubmit="selectAllOptions('sel4');" method="get" action="insert_tank.php"> Auto-select<BR><SELECT id="sel4" multiple="multiple" size="5" name="sel[]"> <?php $sql7 = "SELECT * FROM tanks JOIN tanks_own ON tanks.list = tanks_own.tanks WHERE tanks_own.player ='$player'"; $result7=mysql_query($sql7); while($row7 = mysql_fetch_array( $result7 )) { ?> <option value="<?php echo $row7['list']; ?>" selected="selected"><?php echo $row7['tier']." - ".$row7['name']; ?></option> <?php } ?> </SELECT><BR><INPUT value="Submit" type="submit"> <label> <input name="player" type="hidden" id="player" value="<?php echo $player; ?>" /> </label> </FORM></TD></TR> </TBODY></TABLE> </DIV></DIV> </BODY></HTML> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 21, 2013 Share Posted January 21, 2013 A couple tips: separate your logic (i.e. PHP) from your presentation (i.e. HTML). Put all of your PHP logic at the top of the script (or even in another file) and use the PHP to put the appropriate HTML content into variables. Then in the HTML just use simple echo statements to put that output where needed. Trying to read code that goes in and out between HTML/PHP can get confusing. It will make it easier for people to help you and it will make it easier for you to maintain. As to your problem, have you tried running that query in your database admin software (e.g. phpmyadmin for MySQL)? Are the records duplicated there? If so, the problem is your database construction, the data, or your query. I have no idea how your database tables are set up or what data is in them. Based upon what you have posted I don't see anything specific as to whay the records in your select list would be duplicated if there was only one instance of each value in your result set from the query. Quote Link to comment Share on other sites More sharing options...
hyster Posted January 21, 2013 Author Share Posted January 21, 2013 im going to get the script working as i want it then go over the code and tidy it up. ive done a lot of stuff in it ive never done before so its very untidy atm. 1st thing i tryed was to use the sql directly in phpmyadmin (echo'ed it so i got what the php was doing) and it displays the content only once. tables tanks list - country - type - name - tier tanks_own player - tanks tanks_owned - tanks and tanks - list are the columns that tie together. again im doing this on the fly so things are not so well designed and will tidy it up when i get this step sorted. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 21, 2013 Share Posted January 21, 2013 If a record in tanks has 2 matching records in tank_own then you get the data from tank repeated in each record. That's what joins do - they join fields from one table with fields from the matching records. Quote Link to comment Share on other sites More sharing options...
hyster Posted January 21, 2013 Author Share Posted January 21, 2013 there can be only 1 record in tanks_own - tanks from tanks - list. currently i only have 1 record in tanks_own for testing so the same record is being displayed twice Quote Link to comment Share on other sites More sharing options...
hyster Posted January 21, 2013 Author Share Posted January 21, 2013 thinks its a problem with the database im using as now its showing all entrys twice 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.