Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:08:33+00:00 2026-06-03T23:08:33+00:00

I am trying desperately to move towards OOP but just can’t wrap my head

  • 0

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.

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.

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!

DETAILS.PHP

<script type="text/javascript">
$(document).ready(function(){
//When loading page load notes/messages tables and then reload when ajax is done        
$('#note_holder').load('load_notes.php?subcat=<? echo $subcat;?>'); 
    //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=<? echo $subcat;?>');
                            } 
                    );
             }
    });           
});
</script>  

<div style="float:left; margin-left:15px;">
    <form  name="messages1" class="form" id="myforma" method="post" action="#" enctype="multipart/form-data">
        <fieldset style="width:500px; height:400px; overflow:auto; font-size:11px;">
            <legend>Click to View Previous Notes / Messages</legend>            
            <div style="height:350px; overflow:auto;" class="note_holder" id="note_holder">
             <!--This div is being called from the ajax script to load add_notes_ajax.php-->              
            </div>           
        </fieldset>
        <div style="margin-top:20px;"></div>
    </form>    
</div>

<div style=" float:right;">
  <form  name="notes" class="notes" id="notes" method="post" action="#" enctype="multipart/form-data">
    <fieldset style="width:300px; height:400px;">
      <legend>Enter a Note</legend>
      <div style="margin-top:00px;"></div>
      <div>
     <textarea rows="20" cols="20" style="height:300px; width:290px;" name="notes"></textarea>
     <input type="submit" name="notes_submit" id="notes_submit" value="Submit Note" class="button"  />
     <input type="hidden" name="subcat" value= "<?php echo $subcat; ?>" />
      </div>
    </fieldset>
    <div style="margin-top:20px;"></div>
  </form>
</div>

ADD NOTES AJAX.PHP

<?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->prepare('
        INSERT INTO `notes` 
            (date  , sub_cat_id , notes) 
        VALUES 
            (:date , :subcat    , :notes )
            ');
    $stmt->bindValue('subcat',    $subcat);
    $stmt->bindValue('date',    date('Y-m-d H:i:s'));
    $stmt->bindValue('notes',    $notes);
    $stmt->execute();       

echo "This note was added successfully";
exit;

?>

.
LOAD NOTES.PHP

<table width="100%">
  <thead style="text-align:left; ">
    <tr style="font-size:14px; font-weight:bold;"> 
      <!-- <th><input class="check-all" type="checkbox" /></th>-->
      <th>Date</th>
      <th >Contents</th>
      <th>Preview / Print</th>
    </tr>
  </thead>
  <?php while ($messages_row = mysql_fetch_object($messages_res)):?>
  <tr>
    <td><a target="_blank" href="../site_hospital_files/thread.php?question_id=<?php echo $messages_row->question_id;?>"><?php echo substr($messages_row->reply, 0, 20) . '...';?></a></td>
    <td><?php echo date('Y-m-d', strtotime($messages_row->date_added));?></td>
    <td><a href="../site_hospital_files/pdf_messages_notes.php?msg_id=<?php echo $messages_row->question_id;?>&amp;var1=<?php echo $subcat;?>">Create PDF</a></td>
  </tr>
  <?php endwhile;?>
  <?php while($notes_row = $notes_res->fetch(PDO::FETCH_ASSOC)):?>
  <tr>
    <td><?php echo $notes_row[date]; ?></td>
    <td><?php echo substr($notes_row[notes], 0, 50).'...';?></td>
    <td><a href="pdf_messages_notes.php?note_id=<?php  echo $notes_row->sub_cat_id; ?>&var1=<?php echo $subcat;?>">View</a></td>
  </tr>
  <?php endwhile;?>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:08:35+00:00Added an answer on June 3, 2026 at 11:08 pm

    It absolutely is. Given the relational nature of MySQL and other relational databases, it is very easy to define your PHP objects and code representations of the mysql tables. Consider this overly simple class:

    <?php
        class Note {
            private $id
            private $text;
            private $insert_dt;
            private $update_dt;
        }
    ?>
    

    What this allows you to do is better organize and reuse functionality without the need for code duplication or hunting around your codebase. For instance, say I want to start printing out the insert date for every note a particular way across all pages. How would I do this? I could potentially have to change every page on the site.

    If we have properly defined setters and getters, this becomes an exceedingly simply task. I should only have to replace the formatted return string in 1 (very obvious) location:

    <?php
        class Note {
            // ...
            public function getFormattedInsertDate() {
                return date( "M, Y", $this->insert_dt );
            }
        }
    ?>
    

    Admittedly, this all seems very excessive and time consuming on a small scale. I remember building a personal site for myself in college when I had extensive OO experience. I was learning PHP at the time and for the site I tended to use inline code simply for the speed. It worked fine, was very fast and lightweight. I didn’t understand the hullabaloo over web frameworks, as they felt overly “heavy”.

    The issues arise after the fact during maintenance. When you return to code 6 months or years later, you try and figure out where this call was, or why you have to change it in 8 places to fix a bug. These are the feelings caused by bad coupling – cohesion in your codebase.

    Luckily, many frameworks have sprung up over the years to not only support but encourage and enforce this behavior. If you haven’t I would strongly suggest looking into CakePHP or Code Igniter. They are both pretty easy to leaner frameworks that really nail down these concepts well, as well as provide excellent introductory tutorials to walk you through creating a blog website.

    I hope this helps. Let me know if I missed anything and I’ll update this as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am desperately trying to get my head wrapped around how to implement home
I am playing with things here above my head but am desperately trying to
I am new to ASP.NET MVC3 and trying desperately to just get something simple
Right now I'm trying desperately to get @font-face to work in my website. This
I'm trying desperately to design my UI programmatically. But I think I'm in trouble
I've got a Sony network camera (SNC-RZ25N) that I am trying desperately to get
I'm trying, desperately, to get into C++ programming with DirectX. It's been a steep
Im desperately trying to get my listView to open an alert dialog or a
I am desperately trying to use LinqKits PredicateBuilder to allow the user to enter
I am desperately trying to move a document in a document library from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.