davidjones1990 Posted November 20, 2011 Share Posted November 20, 2011 Hi everybody, I have a problem and was wondering if I could pick your brains. My situation is that I am making a fitness website and one feature is that the user can set themselves targets. Now each target has some fields that are mandatory (for example the date to be completed) and some fields that are unique to each target (for example if the target is weight loss I will need to grab the current weight/target weight fields). Just to make it clear I have all fields in the same database table. So I have written a script that displays that users current targets they have set. It grabs all the data correctly but it duplicates the display and although I am pretty new to php even I can tell the code if not best practise because I have a query inside a loop and a loop inside another loop. Here is the code I have done so far: //Get target types from database for that particular user $get_user_targets = mysql_query("SELECT target_type FROM user_targets WHERE user_id='$id'") or die (mysql_error()); while ($row = mysql_fetch_array($get_user_targets)){ $target_type_display = $row['target_type']; //If the target type is equal to Weight Loss run this script to get all the information and make a display if($target_type_display == 'Weight Loss'){ //Search the database for all weight loass goals $weight_loss_display_sql = mysql_query("SELECT current_weight, target_weight, target_date, date_set, extra_info FROM user_targets WHERE user_id='$id' AND target_type='$target_type_display'") or die (mysql_error()); //Put the data from the query into page variables while($row1 = mysql_fetch_array($weight_loss_display_sql)){ $current_weight_loss_dis = $row1['current_weight']; $target_weight_loss_dis = $row1['target_weight']; $weight_loss_date_set_dis = $row1['date_set']; $weight_loss_target_date_dis = $row1['target_date']; $weight_loss_extra_info_dis = $row1['extra_info']; //Convert date_set into ago time $convertedTime = ($myObject -> convert_datetime($weight_loss_date_set_dis)); $weight_loss_set = ($myObject -> makeAgo($convertedTime)); //Construst a display out of the collected variables $weight_loss_display .= '<div class="blackText" style="width: 475px"> <table width="475px" class="blackText"> <tr><td class="profileUsername" colspan="2">'.$target_type_display.'</td></tr> <tr><td width="250px">Current Weight:</td><td width="225px">'.$current_weight_loss_dis.'</td></tr> <tr><td>Target Weight:</td><td>'.$target_weight_loss_dis.'</td></tr> <tr><td>Date for completion:</td><td>'.$weight_loss_target_date_dis.'</td></tr> <tr><td>'.$weight_loss_extra_info_dis.'</td><td><h6>Date set: '.$weight_loss_set.'</h6></td></tr> </table> </div>'; } } } So far it just checks for a weight loss goal and I was going to expand the code with more if statements for each of the other types of target. So my questions are: 1)Can anybody see why the data is displaying twice? 2) Is there a better way of doing this? (NOTE: im not too worried that the code isn't best practise, I just want to get it functioning properly) Thanks in advance for any help Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/ Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 How is the duplicate result shown can you please show Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289762 Share on other sites More sharing options...
davidjones1990 Posted November 20, 2011 Author Share Posted November 20, 2011 Yes of course sorry for not be clear. So there are two targets that should appear, so it should look like this TARGET 1 TARGET 2 Instead it displays like this TARGET 1 TARGET 2 TARGET 1 TARGET 2 Hope this answers your question. Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289769 Share on other sites More sharing options...
btellez Posted November 20, 2011 Share Posted November 20, 2011 I've glanced through the code, and the code looks ok to me. Have you looked in your database table to make sure there are no duplicate entries? Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289773 Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 while ($row = mysql_fetch_array($get_user_targets)){ this loop is executing twice. check for the database or limit your result to 1 Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289774 Share on other sites More sharing options...
davidjones1990 Posted November 20, 2011 Author Share Posted November 20, 2011 I've glanced through the code, and the code looks ok to me. Have you looked in your database table to make sure there are no duplicate entries? No duplicates, that was the first thing I looked for while ($row = mysql_fetch_array($get_user_targets)){ this loop is executing twice. check for the database or limit your result to 1 Not sure that would sort it because the user can have more than one target set Also I noticed something strange. I used the same if statement but changed the query and variables etc. to Lower BMI instead of weight loss and again works but displays duplicate data. The new thing is if there are two targets displayed it will repeat two times and if there are three targets it will repeat three times. So here is a mock up of what the page looks like now. WEIGHT LOSS 1 WEIGHT LOSS 2 WEIGHT LOSS 1 WEIGHT LOSS 2 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 See what im getting at? Do you think maybe I should count the number of rows being returned with each target type and loop that number of times? Thanks for your responses. Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289777 Share on other sites More sharing options...
davidjones1990 Posted November 20, 2011 Author Share Posted November 20, 2011 I know now, in the first query I need to get a distinct target type I'll let you know how i get on Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289779 Share on other sites More sharing options...
davidjones1990 Posted November 20, 2011 Author Share Posted November 20, 2011 Yep that sorted it Thanks for you help Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289780 Share on other sites More sharing options...
PFMaBiSmAd Posted November 20, 2011 Share Posted November 20, 2011 You don't even need the first query/loop. It is not doing anything that your second query doesn't alreay do. Your goal, when retrieving information from a database, should be to execute ONE query that gets the data you want, in the order that you want it. You then simply iterate over that data and output the results the way you want them to be. You should be ordering the data by the target_type in the (one) query and then put if($row['target_type'] == 'Weight Loss'){ tests inside the (one) loop that iterates over the data. Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289782 Share on other sites More sharing options...
davidjones1990 Posted November 20, 2011 Author Share Posted November 20, 2011 You don't even need the first query/loop. It is not doing anything that your second query doesn't alreay do. Your goal, when retrieving information from a database, should be to execute ONE query that gets the data you want, in the order that you want it. You then simply iterate over that data and output the results the way you want them to be. You should be ordering the data by the target_type in the (one) query and then put if($row['target_type'] == 'Weight Loss'){ tests inside the (one) loop that iterates over the data. Thank you, your method has cleaned the code up and sorted my ordering problem, many thanks Quote Link to comment https://forums.phpfreaks.com/topic/251482-while-loop-duplicate-display/#findComment-1289833 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.