Jump to content

Results Show Up When Not Needed


cutielou22

Recommended Posts

What it is doing:

Right now it is taking the last known team members ($allteammembers) on all the rest of the results that do not have any team members. - So it should show nothing.

 

What I want it to do:

To show nothing for $allteammembers when there is no data found. - Like the way I think I have it coded.

 

The Full Code Going Through Database:



	if ($stmt = $mysqli->prepare("SELECT title, caption, category, image, placement, teammember1, teammember2, teammember3, otherpeople, downloads, views FROM photogallery WHERE year = ? AND timeofday = ? AND status = 1 ORDER BY category, placement")) {
    $stmt->bind_param('is', $year, $timeofday);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($title, $caption, $category, $image, $placement, $teammember1, $teammember2, $teammember3, $otherpeople, $downloads, $views);

	while($stmt->fetch()) {
	
	if ((!empty($placement))) {
	$stmt2 = $mysqli->prepare("SELECT boat, teammember1, teammember2, teammember3, channel, flathead, blue, total, bigfish, totalweight FROM tourneyresults WHERE year = ? AND timeofday = ? AND placement = ?");
	$stmt2->bind_param('isi', $year, $timeofday, $placement);
	$stmt2->execute();
	$stmt2->store_result();
	$stmt2->bind_result($boat, $member1, $member2, $member3, $channels, $flatheads, $blue, $total, $bigfish, $weight);
	$stmt2->fetch();
	$stmt2->close();
	
	$link1 = addplus($member1);
	$link2 = addplus($member2);
	$link3 = addplus($member3);

	if ((!empty($member1)) && (!empty($link1))) {$member1 = "<a href='$mainurl/fisher/$link1'>$member1</a>";}
	if ((!empty($member2)) && (!empty($link2))) {$member2 = "and <a href='$mainurl/fisher/$link2'>$member2</a>";}
	if ((!empty($member3)) && (!empty($link3))) {$member3 = "and <a href='$mainurl/fisher/$link3'>$member3</a>";}

	if ($boat) {$boat2 = "~ Boat #$boat";} else {$boat2 = "";}
	if ($channels) {$channels2 = "$channels Channel(s)";} else {$channels2 = "";}
	if ($flatheads) {$flatheads2 = " | $flatheads Flathead(s)";} else {$flatheads2 = "";}
	if ($blue) {$blue2 = " | $blue Blue(s)";} else {$blue2 = "";}
	
	if ((!empty($member1)) && (!empty($link1))) {$allteammembers = "<b>Team Members: </b>$member1 $member2 $member3 $boat2<br><span style=\"padding-left: 20px\">Caught: $channels2$flatheads2$blue2</span><br><br>";} else {$allteammembers = "";}
	}
	
$link5 = addplus($teammember1);
$link6 = addplus($teammember2);
$link7 = addplus($teammember3);

if ((!empty($teammember1)) && (!empty($link5))) {$teammember1 = "<a href='$mainurl/fisher/$link5'>$teammember1</a>";}
if (!empty($teammember2) && !empty($link6)) {$teammember2 = "and <a href='$mainurl/fisher/$link6'>$teammember2</a>";}
if (!empty($teammember3) && !empty($link7)) {$teammember3 = "and <a href='$mainurl/fisher/$link7'>$teammember3</a>";}

if ((!empty($teammember1)) && (!empty($link5))) {$teammemberspictured = "<b>Team Members Pictured: </b>$teammember1 $teammember2 $teammember3<br>";} else {$teammemberspictured = "";}
	
	if ((!empty($otherpeople)) && (((!empty($teammember1)) && (!empty($link5))) || ((!empty($member1)) && (!empty($link1))))) {$otherpeople2 = "<b>Other People in This Photo:</b> $otherpeople<br>";}
	
	if (!empty($otherpeople) && ((empty($teammember1)) && (empty($link5))) && ((empty($member1)) && (empty($link1)))) {$otherpeople2 = "<b>People in This Photo:</b> $otherpeople<br>";}
	
	if (($category == "0") || (empty($category))) {$category2 = "No Category";}
	if (($category == "1")) {$category2 = "First Place Winner";}
	if (($category == "2")) {$category2 = "2nd-5th Place Winners";}
	if (($category == "3")) {$category2 = "6th-12th Place Winners";}
	if (($category == "4")) {$category2 = "13th-20th Place Winners";}
	if (($category == "5")) {$category2 = "13th-24th Place Winners";}
	if (($category == "6")) {$category2 = "Big Fish Winners";}
	if (($category == "7")) {$category2 = "Take Off";}
	if (($category == "8")) {$category2 = "Weigh In";}
	if (($category == "9")) {$category2 = "Scoreboard";}
	if (($category == "10")) {$category2 = "Prizes from Drawing";}
	if (($category == "11")) {$category2 = "Kids Prizes and Face Painting";}
	if (($category == "12")) {$category2 = "Morning Meeting";}
	if (($category == "13")) {$category2 = "Sign Up";}
	if (($category == "14")) {$category2 = "Spectators";}
	if (($category == "15")) {$category2 = "Misc.";}
	if (($category == "16")) {$category2 = "Extra Photos";}
	
	if ($timeofday == "night") {$subfolder = "NightTourney";} else {$subfolder = "year";}
	
ECHO<<<END
	<div class="gallery" id="container">
  	<div id="left">
		<img src="$mainurl/images/$subfolder/$year/$image">
  	</div>
	
  	<div id="bottom">
		$category2 | <a href="$mainurl/images/year/$year/$image" class='links'>Download Image</a> | $downloads downloads | $views views | Report Error | Add A Comment
  	</div>
	
  	<div id="right">
		<h3>$title</h3>
		$allteammembers
		$teammemberspictured
		$otherpeople2
		
		<p>$caption</p>
  	</div>
	
	</div>
END;
	}
    $stmt->close();
	}

 

Snippet Not Working Correctly:



	if ((!empty($member1)) && (!empty($link1))) {$allteammembers = "<b>Team Members: </b>$member1 $member2 $member3 $boat2<br><span style=\"padding-left: 20px\">Caught: $channels2$flatheads2$blue2</span><br><br>";} else {$allteammembers = "";}


I would like to know how to fix it and why it is doing it exactly? All my ideas failed. :/

Link to comment
Share on other sites

Your code is a really hard to "read" because there is no structure (although that might be a copy/paste error). But, if you were to properly structure the code you it seems that about 1/2 of that logic is dependent upon this condition

 

if ((!empty($placement)))

 

So, the part to set $allteammembers is within that condition, but the logic to display the output is NOT within that condition. So, if $placement is empty $allteammembers is not being evaluated, but the code to output that value is still executed - so it will display the value from the last record.

 

EDIT: You are also running a second query within the loop. You should never run queries in loops. In this case you should be running ONE query with a JOIN statement.

 

EDIT#2: It's not clear whether you WANT to entirely skip records where $placement is empty or not. If so, then your query should be excluding those records.

Edited by Psycho
Link to comment
Share on other sites

I can't really determine what you want to do based upon your current code. When you say "move $allteammembers out of the "if statement"?" that doesn't say WHICH line you moved out.

 

Here is your current code with some indenting to show the logical structure. You can see which lines of code are executed when $placement is empty and which lines are not.

 

 

<?php

    $query = "SELECT title, caption, category, image, placement, teammember1, teammember2, teammember3, otherpeople, downloads, views
              FROM photogallery
              WHERE year = ? AND timeofday = ? AND status = 1
              ORDER BY category, placement"
    if ($stmt = $mysqli->prepare($query))
    {
        $stmt->bind_param('is', $year, $timeofday);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($title, $caption, $category, $image, $placement, $teammember1, $teammember2, $teammember3, $otherpeople, $downloads, $views);
     
        while($stmt->fetch())
        {
            if ((!empty($placement)))
            {
                ###############################################################
                ## THE CODE IN THIS SECTION IS ONLY RUN FOR THE CONDITION ABOVE
                ###############################################################
                $query = "SELECT boat, teammember1, teammember2, teammember3, channel, flathead, blue, total, bigfish, totalweight
                           FROM tourneyresults
                           WHERE year = ? AND timeofday = ? AND placement = ?"
                $stmt2 = $mysqli->prepare($query);
                $stmt2->bind_param('isi', $year, $timeofday, $placement);
                $stmt2->execute();
                $stmt2->store_result();
                $stmt2->bind_result($boat, $member1, $member2, $member3, $channels, $flatheads, $blue, $total, $bigfish, $weight);
                $stmt2->fetch();
                $stmt2->close();
                $link1 = addplus($member1);
                $link2 = addplus($member2);
                $link3 = addplus($member3);
         
                if ((!empty($member1)) && (!empty($link1))) {$member1 = "<a href='$mainurl/fisher/$link1'>$member1</a>";}
                if ((!empty($member2)) && (!empty($link2))) {$member2 = "and <a href='$mainurl/fisher/$link2'>$member2</a>";}
                if ((!empty($member3)) && (!empty($link3))) {$member3 = "and <a href='$mainurl/fisher/$link3'>$member3</a>";}
                 
                if ($boat) {$boat2 = "~ Boat #$boat";} else {$boat2 = "";}
                if ($channels) {$channels2 = "$channels Channel(s)";} else {$channels2 = "";}
                if ($flatheads) {$flatheads2 = " | $flatheads Flathead(s)";} else {$flatheads2 = "";}
                if ($blue) {$blue2 = " | $blue Blue(s)";} else {$blue2 = "";}
                if ((!empty($member1)) && (!empty($link1)))
                {
                    $allteammembers = "<b>Team Members: </b>$member1 $member2 $member3 $boat2<br><span style=\"padding-left: 20px\">Caught: $channels2$flatheads2$blue2</span><br><br>";
                }
                else
                {
                    $allteammembers = "";
                }
            }

            ###############################################################
            ## THE CODE IN THIS SECTION IS RUN FOR EVERY RECORD
            ###############################################################
            $link5 = addplus($teammember1);
            $link6 = addplus($teammember2);
            $link7 = addplus($teammember3);
         
            if ((!empty($teammember1)) && (!empty($link5)))
            {
                $teammember1 = "<a href='$mainurl/fisher/$link5'>$teammember1</a>";
            }
            if (!empty($teammember2) && !empty($link6))
            {
                $teammember2 = "and <a href='$mainurl/fisher/$link6'>$teammember2</a>";
            }
            if (!empty($teammember3) && !empty($link7))
            {
                $teammember3 = "and <a href='$mainurl/fisher/$link7'>$teammember3</a>";
            }
         
            if ((!empty($teammember1)) && (!empty($link5)))
            {
                $teammemberspictured = "<b>Team Members Pictured: </b>$teammember1 $teammember2 $teammember3<br>";
            }
            else
            {
                $teammemberspictured = "";
            }
            if ((!empty($otherpeople)) && (((!empty($teammember1)) && (!empty($link5))) || ((!empty($member1)) && (!empty($link1)))))
            {
                $otherpeople2 = "<b>Other People in This Photo:</b> $otherpeople<br>";
            }
            if (!empty($otherpeople) && ((empty($teammember1)) && (empty($link5))) && ((empty($member1)) && (empty($link1))))
            {
                $otherpeople2 = "<b>People in This Photo:</b> $otherpeople<br>";
            }
            if (($category == "0") || (empty($category))) {$category2 = "No Category";}
            if (($category == "1")) {$category2 = "First Place Winner";}
            if (($category == "2")) {$category2 = "2nd-5th Place Winners";}
            if (($category == "3")) {$category2 = "6th-12th Place Winners";}
            if (($category == "4")) {$category2 = "13th-20th Place Winners";}
            if (($category == "5")) {$category2 = "13th-24th Place Winners";}
            if (($category == "6")) {$category2 = "Big Fish Winners";}
            if (($category == "7")) {$category2 = "Take Off";}
            if (($category == "8")) {$category2 = "Weigh In";}
            if (($category == "9")) {$category2 = "Scoreboard";}
            if (($category == "10")) {$category2 = "Prizes from Drawing";}
            if (($category == "11")) {$category2 = "Kids Prizes and Face Painting";}
            if (($category == "12")) {$category2 = "Morning Meeting";}
            if (($category == "13")) {$category2 = "Sign Up";}
            if (($category == "14")) {$category2 = "Spectators";}
            if (($category == "15")) {$category2 = "Misc.";}
            if (($category == "16")) {$category2 = "Extra Photos";}
            if ($timeofday == "night") {$subfolder = "NightTourney";} else {$subfolder = "year";}

            ECHO<<<END
            <div class="gallery" id="container">
            <div id="left">
            <img src="$mainurl/images/$subfolder/$year/$image">
            </div>
            <div id="bottom">
            $category2 | <a href="$mainurl/images/year/$year/$image" class='links'>Download Image</a> | $downloads downloads | $views views | Report Error | Add A Comment
            </div>
            <div id="right">
            <h3>$title</h3>
            $allteammembers
            $teammemberspictured
            $otherpeople2
            <p>$caption</p>
            </div>
            </div>
            END;
        }
        $stmt->close();
    }
Edited by Psycho
Link to comment
Share on other sites

Line 49 is the line I was referring to about moving.

if ((!empty($member1)) && (!empty($link1))) {$allteammembers = "<b>Team Members: </b>$member1 $member2 $member3 $boat2<br><span style=\"padding-left: 20px\">Caught: $channels2$flatheads2$blue2</span><br><br>";} else {$allteammembers = "";}

The above can be moved, but does not need too is what I am seeing now (from what your comments state in your reply). - So it will always go through even if there is no placement.

 

I want it to work like line 60. Which is structured exactly the same.

if ((!empty($teammember1)) && (!empty($link5))) {$teammemberspictured = "<b>Team Members Pictured: </b>$teammember1 $teammember2 $teammember3<br>";} else {$teammemberspictured = "";}

 

I never used JOIN in mysql successfully before. Would that maybe fix the problem like you suggested?

I'm googling it now. :)

Link to comment
Share on other sites

Well, line 49 is INSIDE the conditional block of code and line 60 is OUTSIDE that conditional block of code. I can provide some sample code, but YOU need to state what you want to do differently for records where $placement is empty. Right now we know that you are not setting $allteammembers if placement is empty and you apparently don't want to be doing that based on your comments.

Link to comment
Share on other sites

So what - EXACTLY - are you wanting to do differently if $placement is empty?

 

If placement is empty I want  $allteammembers to be empty also.

 

Right now it is showing the last known result used for $allteammembers and putting it on all the other results found.

Link to comment
Share on other sites

Another thing. I see that you are defining links ($link1, $link2, etc) using the process

$link1 = addplus($member1);

 

Question: if $member1 is anything other than an empty value, will the returned value ever be empty? The reason I ask is you have a ton of conditional statements such as

if ((!empty($member1)) && (!empty($link1)))

 

But, if $link1 would only be empty if $member1 is empty then you only need to check the member variable

Link to comment
Share on other sites

Oh man. I feel stupid. I fixed it. . . .When I finished reading your last reply I thought of adding an else statement afterwards. Not sure why I did not think of this. Something so simple.

 

So it now works. Thanks for the help anyways. I appreciate it. :)

 

Added After if Statement:



else {$allteammembers = "";}
Edited by cutielou22
Link to comment
Share on other sites

If placement is empty I want  $allteammembers to be empty also.

 

Right now it is showing the last known result used for $allteammembers and putting it on all the other results found.

 

You are not understanding my question. You have two sections of code; one which is only executed if $placement is not empty and the other which is always executed. So, you obviously want something to be different when $placement is empty. I doubt that the problem with $allteammembers is really the only problem.

Link to comment
Share on other sites

Another thing. I see that you are defining links ($link1, $link2, etc) using the process

$link1 = addplus($member1);

 

Question: if $member1 is anything other than an empty value, will the returned value ever be empty? The reason I ask is you have a ton of conditional statements such as

if ((!empty($member1)) && (!empty($link1)))

 

But, if $link1 would only be empty if $member1 is empty then you only need to check the member variable

Yeah, you have a point there. haha

Link to comment
Share on other sites

I believe this is the ONE query you should be running

 

SELECT p.category, p.downloads, p.views, p.title, p.caption, , p.image,
       p.placement, p.teammember1, p.teammember2, p.teammember3, p.otherpeople,
       t.boat, t.teammember1, t.teammember2, t.teammember3, t.channel,
       t.flathead, t.blue, t.total, t.bigfish, t.totalweight
FROM photogallery AS p
LEFT JOIN tourneyresults AS t
  ON p.year = t.year AND p.timeofday = t.timeofday AND p.placement = t.placement
WHERE p.year = ? AND p.timeofday = ? AND p.status = 1 AND p.placement <> ""
ORDER BY p.category, p.placement
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.