PNewCode Posted June 15, 2023 Share Posted June 15, 2023 Hello all. I think this one will be very simple for most of you (if not all) but yet I'm missing something here and I can't tell what it is. Any insight will be great as I am ready to wave the white flag on this. I have tried switching the if and else and that didn't work. I tried to make it an echo statement to test and that didn't work. PROBLEM: No matter what I put in the IF first part, it gets ignored and just moves to the ELSE statement. The column "pageimg" in the database is set to NULL, however I tried to make it not null to see if that made a difference and I got the same bad results. Goal: If the user has an image title in the database column "pageimg" (ex: myimage.jpg) then the background will show that (this part works) if database column "pageimg" is blank (no entry at all) then it SHOULD show "nouserback.jpg" as the image (this does not work) Any thoughts?NOTE: I didn't include below the connection and stuff because that is working fine to get the image and the other functions of the page. This is the only part that is messing with me (below)Example of what is happening now... User "Jane" uploads an image for her profile background. Jane now has the background image showing ("pageimg") User "John" doesn't upload an image for his background. John has a blank white background instead of showing the image "nouserback.jpg" .... john should have "nouserback.jpg" as a background if (!empty($pageimg)) { $imagePageURL = 'img/nouserback.jpg'; } else{ $imagePageURL = 'img/'.$row["pageimg"]; } Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/ Share on other sites More sharing options...
ginerjm Posted June 15, 2023 Share Posted June 15, 2023 Jane should have an image and John should NOT. Your code says that if there is an image set you will see the nouserback.jpg in the url and otherwise (if there IS NO image set) show the image that has been set. Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609717 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 @ginerjm I tried the following (code added below) and what happens is that everyone has "nouserback.jpg" as a background, regardless if they uploaded an image or not (when they upload an image, then the file name gets entered in the column "pageimg". For some reason this is going to the else statement if there is an image file name in that column or not So another words Jane uploads an image, and her image is showing john does not upload an image so the image file named "nouserback.jpg" should be showing. But it doesn't if (!empty($pageimg)) { $imagePageURL = 'img/'.$row["pageimg"]; } else{ $imagePageURL = 'img/nouserback.jpg'; } Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609718 Share on other sites More sharing options...
ginerjm Posted June 15, 2023 Share Posted June 15, 2023 (edited) Do you have error checking turned on? Have you tried using a different image in place of the nouser one? Just to see what happens. And if that works, then what does that tell you? Edited June 15, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609722 Share on other sites More sharing options...
cyberRobot Posted June 15, 2023 Share Posted June 15, 2023 Note that you're using two different variables for the page image ($row["pageimg"] and $pageimg). Is that intentional? Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609723 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 (edited) @ginerjm Yes turned on and no errors. Also, I did try a different image. It's going to the else image url for each time. Also checked the URL of the image on it's own and it's good @cyberRobot yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name the column (pageimg) is NULL in the database. Could that be the issue? I DID change it to Not Null once but that didn't make a difference so I put it back to Null Edited June 15, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609729 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 Here is a mock image of the database. User 2 should have the nouserback.jpg showing on the page because it is blank (empty) in the database Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609731 Share on other sites More sharing options...
kicken Posted June 15, 2023 Share Posted June 15, 2023 28 minutes ago, PNewCode said: yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name Where do you define the variable $pageimg then? Why are you using two variables instead of just the row result? Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609737 Share on other sites More sharing options...
cyberRobot Posted June 15, 2023 Share Posted June 15, 2023 30 minutes ago, PNewCode said: yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name If I'm understanding correctly, $pageimg indicates if the row is empty or not. Then $row["pageimg"] contains the file name, if available. Is that correct? If so, shouldn't you be testing whether or not $row["pageimg"] contains a value? If it does, use the image name...else, use nouserback.jpg. Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609739 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 @kickenThats not the part that doesn't work. If there is something in the pageimg row like in the screen shot, then it appears in the page. It's definition is in the part of the script that I'm having trouble with. The part that isn't working, is if it's blank. But it works if something is in that column of the database. Below is the script that defines and uses it. if (!empty($pageimg)) { $imagePageURL = 'nouserback.jpg'; } else{ $imagePageURL = '/img/'.$row["pageimg"]; } Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609740 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 @cyberRobot Yes exactly Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609741 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 @cyberRobot Thats what this script should be doing, right? Or am I missing some code somewhere? if (!empty($pageimg)) { $imagePageURL = 'nouserback.jpg'; } else{ $imagePageURL = '/img/'.$row["pageimg"]; } Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609742 Share on other sites More sharing options...
Solution cyberRobot Posted June 15, 2023 Solution Share Posted June 15, 2023 To me it seems like the code should be this. Note that I changed $pageimg to $row["pageimg"]. //IF IMAGE NAME IS PROVIDED, SHOW CUSTOM IMAGE if (!empty($row["pageimg"])) { $imagePageURL = '/img/'.$row["pageimg"]; //ELSE...SHOW FALLBACK IMAGE } else{ $imagePageURL = 'nouserback.jpg'; } Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609743 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 Maybe this will help The background of the page is getting called from the PHP. Below is that code and the results. Notice that user 2 has no background. It should be using "nouserback.jpg" however if there is one entered, as you can see it works <style> body { background-image: url('<?= $imagePageURL; ?>'); background-position: center center; background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-color: #000000; } </style> Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609744 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 (edited) @cyberRobot You almost got it. Except with that, it's showing a blank background if the column isn't blank instead of what is entered in the db column EDIT... CYBERROOT YOU DID IT!!! I forgot to put the root back in the path when I used yours (I deleted it for posting in here).... YOU NAILED IT THANK YOU!!! For my education, can you please tell me the difference of what I had and what you did so I understand it? MANY THANKS!!! Edited June 15, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609746 Share on other sites More sharing options...
cyberRobot Posted June 15, 2023 Share Posted June 15, 2023 I believe that's what kicken was trying to help us figure out. Seeing how $pageimg is defined should let us know what the variable contains. You could add the following debug lines to see what the different variables contain: echo '<pre>' . print_r($pageimg, true) . '</pre>'; echo '<pre>' . print_r($row["pageimg"], true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609747 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 Oh I see. Because they are both looking at the actual row instead of just the name that wasn't defined separately? Am I understanding that right? Sorry @kicken I thought that what I had was defining it all in it's own. Thank you again @cyberRobot and @kicken very much! Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609749 Share on other sites More sharing options...
cyberRobot Posted June 15, 2023 Share Posted June 15, 2023 3 minutes ago, PNewCode said: Because they are both looking at the actual row instead of just the name that wasn't defined separately? Am I understanding that right? Too be honest, I'm still not sure what $pageimg contains. My guess is that it contains the result object that's returned when you run the query. It's difficult to tell without seeing the code. $row["pageimg"] on the other hand will contain just the image name for whichever row (i.e., $row) is being processed by the loop extracting data from the result object. Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609750 Share on other sites More sharing options...
PNewCode Posted June 15, 2023 Author Share Posted June 15, 2023 @cyberRobot $pageimage is also containing the same as $row["pageimg"]. That row and it's name are serving 2 purposes. Your code got it on point what the goal was to get it done. AND I can use that now to know what to do with other pages now. Thank you again. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609753 Share on other sites More sharing options...
kicken Posted June 15, 2023 Share Posted June 15, 2023 17 minutes ago, PNewCode said: $pageimage is also containing the same as $row["pageimg"]. Unless you have code somewhere above that conditional that is doing $pageimg = $row['pageimg']; then the variable $pageimg is not the same as $row['pageimg']. That's why I asked to see how it's being defined. It seems to me like maybe you are expecting that $pageimg just magically exists because it's the name of a column in your DB table, but that's not how things work. If you don't assign a variable a value at some point, then it doesn't exist. And if it doesn't exist, then using empty() on it will always return true, which is the problem you seemed to be having. Quote Link to comment https://forums.phpfreaks.com/topic/316976-returning-a-value-if-blank-in-mysql-with-php/#findComment-1609756 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.