cutielou22 Posted March 7, 2013 Share Posted March 7, 2013 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. :/ Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/ Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 (edited) 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 March 7, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417327 Share on other sites More sharing options...
cutielou22 Posted March 7, 2013 Author Share Posted March 7, 2013 So are you saying to just move $allteammembers out of the "if statement"? Because I tried that. That does not work. I get the exact same problem. And no - I do not want to exclude or skip records that do not have a $placement. Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417331 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 (edited) 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 March 7, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417333 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 So what - EXACTLY - are you wanting to do differently if $placement is empty? Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417334 Share on other sites More sharing options...
cutielou22 Posted March 7, 2013 Author Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417335 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417338 Share on other sites More sharing options...
cutielou22 Posted March 7, 2013 Author Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417339 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417340 Share on other sites More sharing options...
cutielou22 Posted March 7, 2013 Author Share Posted March 7, 2013 (edited) 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 March 7, 2013 by cutielou22 Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417342 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417343 Share on other sites More sharing options...
cutielou22 Posted March 7, 2013 Author Share Posted March 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417344 Share on other sites More sharing options...
Psycho Posted March 7, 2013 Share Posted March 7, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275385-results-show-up-when-not-needed/#findComment-1417348 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.