B_CooperA Posted October 4, 2013 Share Posted October 4, 2013 So guys, I have made four functions, or methods, how you would like to call them: ParticipateEvent() will add the userID and eventID into user_events table => User has attended to that event countTotalattendents() will count the total amount of users who are attending on that particular event isAttending() will check if the user has already attended into that event showAllevents() fetches and displays all the events from events table Here are the functions: public function participateEvent() { $query = "INSERT INTO user_events (eventID, userID) VALUES (:event_id, :user_id)"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $_POST['event_id'], ':user_id' => $_SESSION['user_id'] )); if($stmt->rowCount() == 1) { return true; } else { $this->error[] = "Emme pystyneet lisäämään sinua tähän tapahtumaan"; } } public function countTotalattendents() { $query = "SELECT COUNT(userID) AS total_attendents FROM user_events WHERE eventID = :event_id"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $this->row['event_id'] )); if($stmt->rowCount() == 0) { $count = 0; } else { while($this->row = $stmt->fetch(PDO::FETCH_ASSOC)) { $count = $this->row['total_attendents']; } } return $count; } public function isAttending() { $query = "SELECT eventID, userID FROM user_events WHERE eventID = :event_id AND userID = :user_id"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $this->row['event_id'], ':user_id' => $_SESSION['user_id'] )); $this->isAttending = $stmt->rowCount(); return $this->isAttending; } public function showAllEvents() { $query = "SELECT * FROM events LEFT JOIN users ON users.user_id = events.creatoruserid WHERE events.creatoruserid != :user_id AND CURDATE() <= events.event_date ORDER BY events.event_creation_date DESC"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':user_id' => $_SESSION['user_id'] )); $this->rows = $stmt->rowCount(); if($this->rows >= 1) { $i = 1; while($i <= $this->rows) { $this->row = $stmt->fetch(PDO::FETCH_ASSOC); $today = strtotime(date('Y-m-d')); $event_date = strtotime($this->row['event_date']); $this->days = (abs($event_date - $today) / 86400); if ($this->days == 0) { $this->days = " Tänään"; } else if ($this->days == 1) { $this->days = $this->days. " päivä"; } else { $this->days = $this->days. " päivää"; } // Echo the user-event div part echo "<div class='black-show-name'> <a href='profile.php?id=".$this->row['user_id']."'>".$this->row['name']."</a> <div class='tri_angle'></div></div> <div class='user-event-header' > <input type='hidden' value='".$this->row['event_id']."' /> <img src='images/thumbnails/".$this->row['profilepic']."' class='profilesmall'/> <h2><ul class='event-heading'> <li class='event_city' title='Paikkakunta'>".ucfirst($this->row['event_city']). '</li> <li class="event_name" title="Tapahtuma">' .ucfirst($this->row['event_name']). ' </li> <li class="event-date" title="Päivämäärä">'. date("d.m.Y", strtotime($this->row[ 'event_date'])). ' '. date("G:i", strtotime($this->row['event_time'])). ' <small>('.$this->days .')</small></li> <li class="event-created" title="Alkuun"> Osallistujia: '. $this->countTotalattendents(). " </li> <li class='event-attend' title='Näytä lisää'> <button id='show-more' class='medium-button-new'>+</button></li> </ul></h2> </div> // Echo out the event div <div class='event'><p>Osoite: " .ucfirst($this->row['event_address']). "</p> <p>Tapahtuman päivämäärä -ja kellonaika: " .date('d.m.Y', strtotime($this->row['event_date']))." " .$this->row['event_time']. " <p>Tapahtuman osoite - ja kaupunki: " .ucfirst($this->row['event_address']). ", " .ucfirst($this->row['event_city'])."</p> <p>Tapahtuman kuvaus: " .ucfirst($this->row['event_description']). "</p> // If is attending, echo the remove attend button <div class='right_area'>"; if($this->isAttending() == 1) { echo "<form action='".$_SERVER['PHP_SELF']."' method='post'> <input type='hidden' name='event_id' value='".$this->row['event_id']."'/> <input type='submit' name='remove_attend' value='Peru osallistuminen' class='medium-button-remind' /> </form>"; } else { echo "<form action='".$_SERVER['PHP_SELF']."' method='post'> <input type='hidden' name='event_id' value='".$this->row['event_id']."'/> <input type='submit' name='attend_event' value='Osallistu' class='medium-button-attend' /> </form>"; } echo "</div></div>"; $i++; } // No events } else { echo "<p>Ei näytettäviä tapahtumia</p>"; } } The problem is that these functions won't work together. If I remove the counttotalAttendents(), the participate and isattending function are working, but if I keep it, it will count the total amount of attendents, but the "Attend" - button stays there for every event and if i click it it will save the userID but the eventID will always be 0. It's pretty vital to get the count the total users and the isAttending() - function both working. Thanks in advance:) Quote Link to comment Share on other sites More sharing options...
trq Posted October 4, 2013 Share Posted October 4, 2013 You need to explain what "not working" means. Quote Link to comment Share on other sites More sharing options...
B_CooperA Posted October 4, 2013 Author Share Posted October 4, 2013 You need to explain what "not working" means. Well it should echo out the "Remove Attending" button, if user has attended to that event. But it's just echoing the "Attend event" - button and if I press it it will save the value of 0 for the eventID part of the user_events table. If I remove the counttotalAttends() - function, every other functions will work together. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2013 Share Posted October 4, 2013 (edited) I think I see the problem. The functions you posted is retrieving the results from the database and putting them into the $this->rows and $this->row variable and then trying to use the latter variable in other methods. You should no do this, as each query is overwriting the $this->row variable when you get the results. What should do is pass the values that the method (countTotalattendents, and isAttending) needs as arguments. Do not store the database results in $this->row variable unless your class needs to remember the results, which it doesn't. Each method needs separate results from the queries you're running. So save the results from your queries to local variables and pass the data to the countTotalattendents, and isAttending methods as arguments I have modified your methods so it saves results to local variables and not to $this, and passes the event_id to countTotalattendents, and isAttending methods as arguments public function participateEvent() { $query = "INSERT INTO user_events (eventID, userID) VALUES (:event_id, :user_id)"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $_POST['event_id'], ':user_id' => $_SESSION['user_id'] )); if($stmt->rowCount() == 1) { return true; } else { $this->error[] = "Emme pystyneet lisäämään sinua tähän tapahtumaan"; } } public function countTotalattendents($event_id) { $query = "SELECT COUNT(userID) AS total_attendents FROM user_events WHERE eventID = :event_id"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $event_id )); if($stmt->rowCount() == 0) { $count = 0; } else { while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $count = $row['total_attendents']; } } return $count; } public function isAttending($event_id) { $query = "SELECT eventID, userID FROM user_events WHERE eventID = :event_id AND userID = :user_id"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':event_id' => $event_id, ':user_id' => $_SESSION['user_id'] )); $isAttending = $stmt->rowCount(); return $isAttending; } public function showAllEvents() { $query = "SELECT * FROM events LEFT JOIN users ON users.user_id = events.creatoruserid WHERE events.creatoruserid != :user_id AND CURDATE() <= events.event_date ORDER BY events.event_creation_date DESC"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':user_id' => $_SESSION['user_id'] )); $rows = $stmt->rowCount(); if($rows >= 1) { $i = 1; while($i <= $rows) { $row = $stmt->fetch(PDO::FETCH_ASSOC); $today = strtotime(date('Y-m-d')); $event_date = strtotime($row['event_date']); $days = (abs($event_date - $today) / 86400); if ($days == 0) { $days = " Tänään"; } else if ($days == 1) { $days = $days. " päivä"; } else { $days = $days. " päivää"; } // Echo the user-event div part echo "<div class='black-show-name'> <a href='profile.php?id=".$row['user_id']."'>".$row['name']."</a> <div class='tri_angle'></div></div> <div class='user-event-header' > <input type='hidden' value='".$row['event_id']."' /> <img src='images/thumbnails/".$row['profilepic']."' class='profilesmall'/> <h2><ul class='event-heading'> <li class='event_city' title='Paikkakunta'>".ucfirst($row['event_city']). '</li> <li class="event_name" title="Tapahtuma">' .ucfirst($row['event_name']). ' </li> <li class="event-date" title="Päivämäärä">'. date("d.m.Y", strtotime($row[ 'event_date'])). ' '. date("G:i", strtotime($row['event_time'])). ' <small>('.$days .')</small></li> <li class="event-created" title="Alkuun"> Osallistujia: '. $this->countTotalattendents($row['event_id']). " </li> <li class='event-attend' title='Näytä lisää'> <button id='show-more' class='medium-button-new'>+</button></li> </ul></h2> </div> // Echo out the event div <div class='event'><p>Osoite: " .ucfirst($row['event_address']). "</p> <p>Tapahtuman päivämäärä -ja kellonaika: " .date('d.m.Y', strtotime($row['event_date']))." " .$row['event_time']. " <p>Tapahtuman osoite - ja kaupunki: " .ucfirst($row['event_address']). ", " .ucfirst($row['event_city'])."</p> <p>Tapahtuman kuvaus: " .ucfirst($row['event_description']). "</p> // If is attending, echo the remove attend button <div class='right_area'>"; if($this->isAttending($row['event_id']) == 1) { echo "<form action='".$_SERVER['PHP_SELF']."' method='post'> <input type='hidden' name='event_id' value='".$row['event_id']."'/> <input type='submit' name='remove_attend' value='Peru osallistuminen' class='medium-button-remind' /> </form>"; } else { echo "<form action='".$_SERVER['PHP_SELF']."' method='post'> <input type='hidden' name='event_id' value='".$row['event_id']."'/> <input type='submit' name='attend_event' value='Osallistu' class='medium-button-attend' /> </form>"; } echo "</div></div>"; $i++; } // No events } else { echo "<p>Ei näytettäviä tapahtumia</p>"; } Edited October 4, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 4, 2013 Share Posted October 4, 2013 (edited) this is similar to what Ch0cu3r posted above. you are missing the point of what functions/methods do and how, if at all, they interact. i also think you think you must use $this->variable_name for every variable. that's not true. you only use that for class properties that you want to share between methods. the only class properties you should have are the ones that your class needs to share between methods or that need to be public for external access. don't use class properties for every variable you use inside a method. you can use a regular php variable, such as $row, inside of method/function and it will be local only to the method/function. Edited October 4, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
B_CooperA Posted October 4, 2013 Author Share Posted October 4, 2013 (edited) Okay thank you guys, now I know what's the difference of using $this and just a regular variable. So $this only if you want to save the data in it and use it in another methods, otherwise just user a regular variable. I still have one problem and I think it's pretty major to me. I have user profiles in my web app and the address for them is like this: profile.php?id=[an id from the $_GET variable] so it's looking a matching id from the database and echo out the information based on that user id. I've made a function for this, called checkUser() and it looks like this: public function checkUser() { $query = "SELECT user_id FROM users WHERE user_id = :id"; $stmt = $this->connect->prepare($query); $stmt->execute(array( ':id' => $_GET['id'] )); $rows = $stmt->rowCount(); if($rows == 0) { header("location: events.php"); } else { return; } } I also have another file, events.php, which will echo the events declared in showAllevents() - method and I am redirecting the user in to that page if the user id isn't found. There used to be a 404.php there, but I changed it. So the problem is that what ever I'm doing inside of my profile.php page, I always have to redirect it back to the same page whether the function encountered errors or not. Otherwise it will redirect me to events.php. So this makes it very hard to user know what just happened in user experience point of view. If I don't check the id with this function, it will always redirect me to that profile.php page which basically is just a template and you'll get a lot of undefined index - errors there. So I'm asking, what is the best way to do this so that I can use and print possible user messages and errors for my profile.php page? Edited October 4, 2013 by B_CooperA Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2013 Share Posted October 4, 2013 (edited) On way would be to check that $_GET['id'] exists before calling the checkUser() method in profile.php. If no id found in _GET then display error. Example if(isset($_GET['id']) && is_numeric($_GET['id'])) { //call checkUser } else { echo 'Sorry, requested user profile requested doesn\'t exist'; } Or in checkUser method, save an error message to $_SESSION before header(); if($rows == 0) { $_SESSION['error'] = 'Sorry user profile was not found'; session_write_close(); // may need to call this before redirect header("location: events.php"); } Then in event.php check to see if the error exists and display the error message if(isset($_SESSION['error'])) { echo '<div class="error">' . $_SESSION['error'] . '</div>'; unset($_SESSION['error']); // remove error from session to stop it being shown again } Edited October 4, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
B_CooperA Posted October 4, 2013 Author Share Posted October 4, 2013 But will this help for it that it doesn't always redirect me to that profile.php "template" if I want to execute some methods without redirecting? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2013 Share Posted October 4, 2013 Sorry I don't understand that. You saying you don't want the user to be redirected from profile.php if the id doesn't exist? Quote Link to comment Share on other sites More sharing options...
B_CooperA Posted October 4, 2013 Author Share Posted October 4, 2013 Sorry I don't understand that. You saying you don't want the user to be redirected from profile.php if the id doesn't exist? What I'm saying is that if I don't have that checkUser() - function and if I have some functions that will do some things (edit name, edit password) in the profile.php page.. After the function executes, it'll always trying to redirect me to that profile.php page (without the id after it) unless I don't use that checkUser() - function. So I can't use any echos, errors, print_r:s for testing or anything because it'll alway redirects me to that profille.php "template". Quote Link to comment 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.