justlukeyou Posted January 8, 2013 Share Posted January 8, 2013 Hi, I have a code which displays the events that users have created. However if they have not created an event it shows the empty DIV cells. So I am trying to display a message "You have no events. Create an event." if the user has not created an events. I can get this to work if against the users login but I cant seem to be able to check with the cell is empty. I think I almost have it. Can anyone please advise how I complete it? So far it displays "You have no events. Create an event." Regardless of whether there is content in the database or not. <?php $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; if ($result = mysql_query($query)) if (empty($organiserid)) { echo 'You have no events. Create an event.'; } else { $row = mysql_fetch_array($result); { ?> <div class="eventjoblistings"> <div class="boardeventfield"> <div class="boardeventfieldintro"> <div class="boardeventdatatitle"> <?php echo (!empty($row['eventname'])) ? $row['eventname'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['eventdetails'])) ? $row['eventdetails'] : ''; ?> </div> <div class="boardeventdatahalf"> <?php echo (!empty($row['eventlocation'])) ? $row['eventlocation'] : ''; ?>, </div> <div class="boardeventdatahalf"> <?php echo (!empty($row['eventcountry'])) ? $row['eventcountry'] : ''; ?> </div> </div> <div class="boardeventfielddetails"> <div class="boardeventdatasupplier"> <?php echo (!empty($row['supplier1'])) ? $row['supplier1'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['supplierdetails1'])) ? $row['supplierdetails1'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['budget1'])) ? $row['budget1'] : ''; ?> </div> </div> <div class="boardeventfielddetails"> <div class="boardeventdata"> <?php echo (!empty($row['supplier2'])) ? $row['supplier1'] : ''; ?> </div> <?php echo (!empty($row['details2'])) ? $row['details2'] : ''; ?> <div class="boardeventdata"> <?php echo (!empty($row['budget2'])) ? $row['budget2'] : ''; ?> </div> </div> </div> </div> <?php } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/ Share on other sites More sharing options...
Beeeeney Posted January 8, 2013 Share Posted January 8, 2013 Where do you define $organiserid? Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404186 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 (edited) Hi, I haven't yet but when I just run a query it works fine. Do I need to say that the organiserid is at standard "0". So if ID 350 is entered then this is different so it will display everything? Or would I say its blank? So this would echo the eventname. <?php $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; if ($result = mysql_query($query)) { $row = mysql_fetch_array($result); { ?> <div class="eventjoblistings"> <div class="boardeventfield"> <div class="boardeventfieldintro"> <div class="boardeventdatatitle"> <?php echo (!empty($row['eventname'])) ? $row['eventname'] : ''; ?> Edited January 8, 2013 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404188 Share on other sites More sharing options...
Beeeeney Posted January 8, 2013 Share Posted January 8, 2013 You've told your code to echo something if a variable is empty, on a variable you haven't declared. Then you're asking why it's echoing. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404190 Share on other sites More sharing options...
Beeeeney Posted January 8, 2013 Share Posted January 8, 2013 Hi, I haven't yet but when I just run a query it works fine. Do I need to say that the organiserid is at standard "0". So if ID 350 is entered then this is different so it will display everything? Or would I say its blank? So this would echo the eventname. <?php $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; if ($result = mysql_query($query)) { $row = mysql_fetch_array($result); { ?> <div class="eventjoblistings"> <div class="boardeventfield"> <div class="boardeventfieldintro"> <div class="boardeventdatatitle"> <?php echo (!empty($row['eventname'])) ? $row['eventname'] : ''; ?> if ($result = mysql_query($query)) { Will always return false, seeing as you haven't assigned anything to $result. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404193 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 You've told your code to echo something if a variable is empty, on a variable you haven't declared. Then you're asking why it's echoing. So what method should I use to declare it? Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404194 Share on other sites More sharing options...
Beeeeney Posted January 8, 2013 Share Posted January 8, 2013 (edited) Stab in the dark, but try this: <?php $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; $result = mysql_query($query); $row = mysql_fetch_array($result); if (empty($result)) { echo 'You have no events. Create an event.'; } else { ?> <div class="eventjoblistings"> <div class="boardeventfield"> <div class="boardeventfieldintro"> <div class="boardeventdatatitle"> <?php echo (!empty($row['eventname'])) ? $row['eventname'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['eventdetails'])) ? $row['eventdetails'] : ''; ?> </div> <div class="boardeventdatahalf"> <?php echo (!empty($row['eventlocation'])) ? $row['eventlocation'] : ''; ?>, </div> <div class="boardeventdatahalf"> <?php echo (!empty($row['eventcountry'])) ? $row['eventcountry'] : ''; ?> </div> </div> <div class="boardeventfielddetails"> <div class="boardeventdatasupplier"> <?php echo (!empty($row['supplier1'])) ? $row['supplier1'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['supplierdetails1'])) ? $row['supplierdetails1'] : ''; ?> </div> <div class="boardeventdata"> <?php echo (!empty($row['budget1'])) ? $row['budget1'] : ''; ?> </div> </div> <div class="boardeventfielddetails"> <div class="boardeventdata"> <?php echo (!empty($row['supplier2'])) ? $row['supplier1'] : ''; ?> </div> <?php echo (!empty($row['details2'])) ? $row['details2'] : ''; ?> <div class="boardeventdata"> <?php echo (!empty($row['budget2'])) ? $row['budget2'] : ''; ?> </div> </div> </div> </div> <?php } ?> Edited January 8, 2013 by Beeeeney Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404197 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 Hi, Unfortunately that doesn't work. It skips the first echo and displays the empty cells. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404199 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 Hi, This work..If I say its greater than zero. $organiserid = 0; $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($organiserid > 0) { echo 'You have no events. Create an event.'; } else { { Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404200 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 Ah, no its doesn't. When the database is empty it still skips the first echo. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404201 Share on other sites More sharing options...
Beeeeney Posted January 8, 2013 Share Posted January 8, 2013 Hi, This work..If I say its greater than zero. $organiserid = 0; $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($organiserid > 0) { echo 'You have no events. Create an event.'; } else { { But $organiserid IS 0. You told your code that it is 0. I hope you find the help you need! Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404202 Share on other sites More sharing options...
justlukeyou Posted January 8, 2013 Author Share Posted January 8, 2013 Hi, Does anyone have any suggestions please? I have tried using the PHP site http://php.net/manual/en/function.empty.php I think I almost have it but cant get the right configuration. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404325 Share on other sites More sharing options...
DavidAM Posted January 8, 2013 Share Posted January 8, 2013 There are several issues with that code, not the least of which is indentation (which could be the fault of the "WYSIWYG" editor). Here is an outline (using your code) of how you can accomplish this. $query = "SELECT * FROM eventjobs WHERE organiserid = " . intval($_SESSION['userID']) . " LIMIT 10"; /* This assigns the resource (or FALSE) to $result AND tests if the query succeeded NOTE: The query WILL succeed if there are NO ROWS found */ if ($result = mysql_query($query)) { // if (empty($organiserid)) # As you have seen this is not useful // Check to see if there were ZERO rows returned if (mysql_num_rows($result) == 0) { echo 'You have no events. Create an event.'; } else { /* You were only processing the first row. You need a while loop */ // $row = mysql_fetch_array($result); while ($row = mysql_fetch_assoc($result)) { ?> <div class="eventjoblistings"> AND SO FORTH </div> <?php } // END while ($row = } // END if (mysql_num_rows ( } // END if ($result = mysql_query( Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404343 Share on other sites More sharing options...
justlukeyou Posted January 9, 2013 Author Share Posted January 9, 2013 Thanks mate. This worked a treat. Quote Link to comment https://forums.phpfreaks.com/topic/272848-if-empty-echo-else-echo/#findComment-1404501 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.