Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP OOP vs Inline
    text
    copied!<p>I am trying desperately to move towards OOP but just can't wrap my head around when to use it. I get the mechanics but when to use them is just not clicking. I'm curious if my current scenario is ripe for an OOP approach. </p> <p>I have 3 pages. Details.php shows two side by side divs. One where the user can add a note and another where they can see previous notes stored in MySQL. They can add notes and pull notes via AJAX function in Details.php. The javascript function calls add_notes.php to add notes to the database and it calls load_notes.php to load notes on page via Jquery .load() as well as when a new note is submitted to refresh the div.</p> <p>I'm a newbie but I feel in my bones there is a better way to organize this code. I would look into a framework but I am knee deep in this project already so looking for OOP ideas on how to break this up better or validation that I'm doing it in as streamlined a way as possible. All comments are helpful!</p> <p>DETAILS.PHP</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function(){ //When loading page load notes/messages tables and then reload when ajax is done $('#note_holder').load('load_notes.php?subcat=&lt;? echo $subcat;?&gt;'); //onclick handler send message btn $("#notes_submit").click(function(){ $(this).closest('form').submit(function(){ return false; }); var frm = $(this).closest('form'); var data = $(frm).serialize(); if($(frm).valid()){ $.post( "../php/add_notes_ajax.php", data, function(data){ $('#note_holder').load('load_notes.php?subcat=&lt;? echo $subcat;?&gt;'); } ); } }); }); &lt;/script&gt; &lt;div style="float:left; margin-left:15px;"&gt; &lt;form name="messages1" class="form" id="myforma" method="post" action="#" enctype="multipart/form-data"&gt; &lt;fieldset style="width:500px; height:400px; overflow:auto; font-size:11px;"&gt; &lt;legend&gt;Click to View Previous Notes / Messages&lt;/legend&gt; &lt;div style="height:350px; overflow:auto;" class="note_holder" id="note_holder"&gt; &lt;!--This div is being called from the ajax script to load add_notes_ajax.php--&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;div style="margin-top:20px;"&gt;&lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;div style=" float:right;"&gt; &lt;form name="notes" class="notes" id="notes" method="post" action="#" enctype="multipart/form-data"&gt; &lt;fieldset style="width:300px; height:400px;"&gt; &lt;legend&gt;Enter a Note&lt;/legend&gt; &lt;div style="margin-top:00px;"&gt;&lt;/div&gt; &lt;div&gt; &lt;textarea rows="20" cols="20" style="height:300px; width:290px;" name="notes"&gt;&lt;/textarea&gt; &lt;input type="submit" name="notes_submit" id="notes_submit" value="Submit Note" class="button" /&gt; &lt;input type="hidden" name="subcat" value= "&lt;?php echo $subcat; ?&gt;" /&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;div style="margin-top:20px;"&gt;&lt;/div&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>ADD NOTES AJAX.PHP</p> <pre><code>&lt;?php include_once('../bootstrap.php'); include_once('../site_globals/common_functions.php'); include_once('../site_globals/common_queries.php'); include_once('../php/gump.class.php'); page_protect(); error_reporting(0); $firstname = filter($_SESSION['user_name']); $myid = filter($_SESSION['user_id']); // All the variables from the submission form $notes = filter($_POST['notes']); $subcat = filter($_POST['subcat']); //Insert Notes into the database $stmt = $dbh-&gt;prepare(' INSERT INTO `notes` (date , sub_cat_id , notes) VALUES (:date , :subcat , :notes ) '); $stmt-&gt;bindValue('subcat', $subcat); $stmt-&gt;bindValue('date', date('Y-m-d H:i:s')); $stmt-&gt;bindValue('notes', $notes); $stmt-&gt;execute(); echo "This note was added successfully"; exit; ?&gt; </code></pre> <p>. LOAD NOTES.PHP</p> <pre><code>&lt;table width="100%"&gt; &lt;thead style="text-align:left; "&gt; &lt;tr style="font-size:14px; font-weight:bold;"&gt; &lt;!-- &lt;th&gt;&lt;input class="check-all" type="checkbox" /&gt;&lt;/th&gt;--&gt; &lt;th&gt;Date&lt;/th&gt; &lt;th &gt;Contents&lt;/th&gt; &lt;th&gt;Preview / Print&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;?php while ($messages_row = mysql_fetch_object($messages_res)):?&gt; &lt;tr&gt; &lt;td&gt;&lt;a target="_blank" href="../site_hospital_files/thread.php?question_id=&lt;?php echo $messages_row-&gt;question_id;?&gt;"&gt;&lt;?php echo substr($messages_row-&gt;reply, 0, 20) . '...';?&gt;&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo date('Y-m-d', strtotime($messages_row-&gt;date_added));?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="../site_hospital_files/pdf_messages_notes.php?msg_id=&lt;?php echo $messages_row-&gt;question_id;?&gt;&amp;amp;var1=&lt;?php echo $subcat;?&gt;"&gt;Create PDF&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endwhile;?&gt; &lt;?php while($notes_row = $notes_res-&gt;fetch(PDO::FETCH_ASSOC)):?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $notes_row[date]; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo substr($notes_row[notes], 0, 50).'...';?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="pdf_messages_notes.php?note_id=&lt;?php echo $notes_row-&gt;sub_cat_id; ?&gt;&amp;var1=&lt;?php echo $subcat;?&gt;"&gt;View&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endwhile;?&gt; &lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload