Jump to content

While loop duplicate display


davidjones1990

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.