php - small social networking for university assignment -


i doing university assignment, making small social networking website. stuck on student search page.

if don't search , shows default 20 students. if want search name, fine.

however if want add friend, create problem. have while loop outing add friend button, when add friend, should output request sent or added particular student student, tried many different know didn't workout.

this how works, bad

        <?php         try{           include('includes/connect.php');            $user_id = $_session['id'];           $found = 'false';            $sql = "select * requests user_id = $user_id";           $stmt_request_status = $connection->prepare($sql);           $stmt_request_status->execute();           if($stmt_request_status->rowcount()) {             $request_result = array();             while($request_row = $stmt_request_status->fetchobject()){               $request_result[] =  $request_row->status;             }           }            if(isset($_post['addfriend'])) {             $request_id = $_post['request_id'];             $sql = "insert requests (user_id, request_id, status) values ('$user_id', :request_id, '0')";             $stmt = $connection->prepare($sql);             $stmt->bindparam(':request_id', $request_id);             $stmt->execute();           }             $sql = "select * users order id desc limit 20";           $result = $connection->query($sql);            $count = "select count(*) users";           $count_result = $connection->query($count);           $total_records = $count_result->fetchcolumn();            if(isset($_get['submit'])) {              $keyword = '%%%' . $_get['keyword'] . '%%%';             $sql = "select * users first_name :keyword or last_name :keyword";              $stmt = $connection->prepare($sql);             $stmt->bindparam(':keyword', $keyword);             $stmt->execute();              if($stmt->rowcount()) {               $found = 'true';             } else {               $msg = 'no results found';             }           }          }           catch(pdoexception $e) {           echo "connection failed: " . $e->getmessage();         }         ?>            <div class="row">           <div class="medium-12 columns">             <form action="<?php echo $_server['php_self']; ?>" method="get">               <div class="row">                 <div class="large-12 columns">                   <div class="row collapse">                     <div class="small-10 columns">                       <input type="text" name="keyword" placeholder="whats new in class?">                     </div>                     <div class="small-2 columns">                       <input type="submit" name="submit" class="button postfix" value="go!">                     </div>                   </div>                 </div>               </div>             </form>             </div>         </div>          <div class="row">           <div class="large-12 columns">             <?php echo isset($msg) ? '<span class="error">' . $msg . '</span>' : '';             ?>                <?php             if($found !== 'true') {               echo "<h4>recent students</h4>";               while($row = $result->fetchobject()){ ?>               <?php echo $row->first_name; ?>                <form action="" method="post" accept-charset="utf-8">                 <input type="hidden" name="request_id" value="<?php echo $row->id; ?>">                 <?php                 if(isset($request_result)){                   foreach ($request_result  $status) {                     if($status == 1) {                       echo '<p class="button secondary tiny">already friends</p>';                     }                     else if ($status == 0) {                       echo '<p class="button secondary tiny">request sent</p>';                     }                   }                 }                 ?>                 <input type="submit" name="addfriend" value="add friend" class="button tiny success">                 </form>               <hr>         <?php   } // while loop         } // end if         ?>           <?php          if($found == 'true') {           echo "<h4>student found</h4>";           while($row = $stmt->fetchobject()){ ?>           <?php echo $row->first_name; ?>         <?php  }  // while loop         } // end if         ?>        </div>     </div>      <hr>     <div class="row">       <div class="large-12 columns">         <p><strong>total students far : <?php echo $total_records; ?> </strong></p>       </div>     </div>      <?php include('includes/footer.php'); ?>       believe messing around, result unwanting      <?php             if($found !== 'true') {               echo "<h4>recent students</h4>";               while($row = $result->fetchobject()){ ?>               <?php echo $row->first_name; ?>                <form action="" method="post" accept-charset="utf-8">                 <input type="hidden" name="request_id" value="<?php echo $row->id; ?>">                 <?php                 if(isset($request_result)){                   foreach ($request_result  $status) {                     if($status == 1) {                       echo '<p class="button secondary tiny">already friends</p>';                     }                     else if ($status == 0) {                       echo '<p class="button secondary tiny">request sent</p>';                     }                   }                 }                 ?>                 <input type="submit" name="addfriend" value="add friend" class="button tiny success">                 </form>               <hr>         <?php   } // while loop         } // end if         ?> 

see if able work this. think separation of task important can focus on each part piece. best way make functions or classes , include them on page compiling. page organized , limit how script being displayed @ 1 time. of these elements can not test because don't have requests table in database , guessing database connection work have (i not connection way in theory should work). bottom line, separate out tasks isolating functions/classes:

<?php // create "global" database class dbconnection     {         private static  $con;         private function __construct()             {             }          public  static  function initialize()             {                 // include connection once here                 // don't know connect.php in relation                 // page, assuming it's in                 // same directory (root)                 include_once(__dir__.'/includes/connect.php');                 // assign static connection here                 if(!isset(self::$con)) {                         self::$con  =   $connection;                     }                 // return either new or current connection                 return self::$con;             }     } // wrap connection in function quick access function connection()     {            return dbconnection::initialize();     } // fetch , set data array function fetch($sql = false,$bind = false)     {         if(empty($sql))             return 0;         try {                 $con    =   connection();                 $query  =   $con->prepare($sql);                  if(!empty($bind) && is_array($bind)) {                         foreach($bind $key => $value) {                                 $array[":{$key}"]   =   $value;                             }                         $query->execute($array);                     }                 else                     $query->execute();                   while($row = $query->fetch(pdo::fetch_assoc)) {                         $result[]   =   $row;                     }                  return (!empty($result))? $result : 0;             }         catch (pdoexception $e) {                 return 0;             }     } // quick write function function write($sql = false,$bind = false)     {         if(empty($sql))             return 0;         try {                 $con    =   connection();                 $query  =   $con->prepare($sql);                  if(!empty($bind) && is_array($bind)) {                         foreach($bind $key => $value) {                                 $array[":{$key}"]   =   $value;                             }                         $query->execute($array);                     }                 else                     $query->execute();                  return true;             }         catch (pdoexception $e) {                 return false;             }     } // quick way run query function get_requests($id = false,$obj = false)     {         $result =   fetch("select * `requests` `user_id` = :0",array($id));          if($result == 0)             return 0;         // return object if desired         return ($obj)? json_decode(json_encode($result)) : $result;     } // quick way write friend function add_friend($request = false)     {         if(empty($request) || empty($_session['id']))             return 0;          return write("insert `requests` (`user_id`, `request_id`, `status`) values (:0, :1, '0')",array($_session['id'],$request));     } // quick way select users function get_users($limit = 20)     {         return fetch("select * `users` order `id` desc limit {$limit}");     } // easy count function function count_users()     {         $count  =   fetch("select count(*) count `users`");         return $count[0]['count'];     } // quick search function function find_users($search = false)     {         $search =   '%%%'.$search.'%%%';         return  fetch("select * `users` `first_name` :0 or `last_name` :1",array($search,$search));     }  session_start(); $_session['id'] =   29; // if add-a-friend request if(isset($_post['addfriend']))     add_friend($_post['request_id']); // if submitting search $search     =   (isset($_get['search']))? find_users($_get['keyword']) : false; // user id $requests   =   (!empty($_session['id']))? get_requests($_session['id']) : false; // count total users $count  =   count_users(); // if not logged in, show footer if(empty($_session['id'])) {         include('includes/footer.php');         exit;     } ?> <div class="row">     <div class="medium-12 columns">         <form action="<?php echo $_server['php_self']; ?>" method="get">             <div class="row">                 <div class="large-12 columns">                     <div class="row collapse">                         <div class="small-10 columns">                             <input type="text" name="keyword" placeholder="whats new in class?">                         </div>                         <div class="small-2 columns">                             <input type="submit" name="search" class="button postfix" value="go!">                         </div>                     </div>                 </div>             </div>         </form>     </div> </div> <div class="row">     <div class="large-12 columns"> <?php if(!isset($_get['search'])) {  ?>      <h4>recent students</h4> <?php   foreach(get_users() $row) { ?>         <?php echo $row['first_name']; ?>         <form action="" method="post" accept-charset="utf-8">             <input type="hidden" name="request_id" value="<?php echo $row['id']; ?>"> <?php           if(isset($request_result)) {                         foreach($request_result  $status) {                                 if($status == 1) { ?>          <p class="button secondary tiny">already friends</p> <?php                               }                                 elseif($status == 0) { ?>          <p class="button secondary tiny">request sent</p> <?php                               }                             }                     } ?>          <input type="submit" name="addfriend" value="add friend" class="button tiny success">         </form>         <hr> <?php       }     } // if searching if(isset($_get['search'])) {         // if not found         if($search == 0) { ?>      <span class="error">friend not found!</span> <?php       }         // if found         else { ?>      <h4><?php echo $setcount = count($search);?> student<?php echo ($setcount > 1)? "s":"";?> found</h4> <?php           foreach($search $row) { ?>      <?php echo $row['first_name']; ?> <?php               }             }      } ?>  </div> </div> <hr> <div class="row">     <div class="large-12 columns">         <p><strong>total students far : <?php echo $count; ?> </strong></p>     </div> </div> <?php include('includes/footer.php'); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -