Recently found OOP PHP. Want Help re-coding Component.

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
portablejim
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Jul 12, 2007 1:21 am
Contact:

Recently found OOP PHP. Want Help re-coding Component.

Post by portablejim » Fri Dec 14, 2007 11:10 pm

I recently coded a search facility for a database. I then wanted to integrate it into Joomla! (w/o using the wrapper), so I went on investigating how to make it into a plugin.
I found this page.
I discovered I needed to make it a component.
I discovered that I needed to learn OOP PHP, so I did (I read over the documents that the links linked to in regard to OOP).
I found a simple helloworld component and put my code instead of the hello world text.

Now I think I need to recode my component to take up less CPU power.

Could someone please give me some ideas as to how to make my code to Joomla OOP PHP.

Here is my code (yes I only have one file):

Code: Select all

<?php
/**
* @package SearchSermons
* @version 1.0
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* Joomla! is free software and parts of it may contain or be derived from the
* GNU General Public License or other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


// Make a MySQL Connection, Log in and Select the Database
mysql_connect("127.0.0.1:3306/", "dpcsermons", "dpcsermons") or die(mysql_error("Could Not Log in"));
mysql_select_db("dpcsermons") or die(mysql_error("Could not select the database. Check the server."));

/* Start of the function "formfield". This function creates forms, with or without the values from a certain field in a database, giving one option for each
unique field name.
$field = name of the field to pull fom the database
$type = the type of form to create
$options = the options for the form (e.g size="10" maxlength="40").
$includevalues = a boolean value to put in the values from the database (like you would for a drop down menu) or to leave the values out (like you would for
a text box).
*/
function formfield($field,$type,$options,$includevalues)
{
   // If $includevalues is =1, get the information from the table 'sermons' and group the data by $field. This results in the unique names being put into $result
   if($includevalues)
   {
      $query = "SELECT * FROM sermons GROUP BY $field ";
      $result = mysql_query($query) or die(mysql_error());
   }
   //Next, make up the form, including the database values of they are wanted.
?>
   <<?php echo $type;?> name="<?php echo $field;?>" id="f<?php echo $field;?>" <?php echo $options;?>>
   <?php if($includevalues)
   {?>
      <option selected ></option>
      <?php while($row = mysql_fetch_array($result))
      {?>
         <option><?php echo $row[$field]?></option>
         <br />
<?php      }
   }?>
   </select>
<?php } /* End of the function */

function modmeeting($meating){
   $meating = str_replace("Early Morning Evening", "All 3", $meating); //Combined
   $meating = str_replace("Early Morning", "Early Church & Morning Church", $meating);
   $meating = str_replace("Early Evening", "Early Church & Evening Church", $meating);
   $meating = str_replace("Morning Evening", "Morning Church & Evening Church", $meating);
   $meating = str_replace("Early", "Early Church", $meating);
   $meating = str_replace("Morning", "Morning Church", $meating);
   $meating = str_replace("Evening", "Evening Church", $meating);
   return $meating;
}
?>
<script src="components/com_searchsermons/sermonsjs.js"></script><!-- External Javascript File -->

   <p>This is a form to search the databse for sermons.</p>
   <!-- Create a form that displays the information on the same page and uses the URL bar to populate the form.  -->
   <form action="index.php?option=com_searchsermons" method="post" id="1" name="form1">
      <input type="hidden" name="option" value="com_searchsermons" />
      <input type="hidden" name="Itemid" value="<?php echo $_POST['Itemid']; ?>" ><?php echo $_POST['Itemid']; ?></input>
   
      <!-- Create a normal text input box for our keywords.  -->
      <p>Keyword: <input type="text" size="30" maxlength="100" name="lockword" id="flockword" /></p>
      <!-- Create a place to hide and show the extra fields.  -->
      <p style="padding-left: 10px;">Advanced - <a href="javascript: void(0)" onclick="showadvanced()">Show</a> / <a href="javascript: void(0)" onclick="hideadvanced()">Hide</a></p>
      <!-- The extra fields, all but the date created with out function set earlier.  -->
      <div id="advancedopt" style="display: none;"> <!-- style="display: none;" -->
         <!-- Make a dropdown box with a blank option, plus an option for each of the months of the year (from an array)-->
         <p>Date:<select name="month">
            <option></option>
<?php
            $datemonth = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November' , 12 => 'December');
            $dmnum = 1;
            while($datemonth[$dmnum])
            {?>
               <option value="<?php echo $dmnum;?>"><?php echo $datemonth[$dmnum];?> </option>
               
<?php            $dmnum++;
            }?>

         </select>
         <!-- Make a dropdown box with a blank option, plus an option for the numbers from the year of the first sermon to the year of the latest sermon -->
<?php         $qyeara = mysql_fetch_array(mysql_query("SELECT * FROM `sermons` ORDER BY `date` ASC LIMIT 1"));
            $qyeard = mysql_fetch_array(mysql_query("SELECT * FROM `sermons` ORDER BY `date` DESC LIMIT 1"));
            $yearmn = $qyeara['date'];
            $yearmx = $qyeard['date'];
            $yearmax = substr($yearmx, 0, 4);
            $yearmin = substr($yearmn, 0, 4);
?>
         <select name="year">
            <option></option>
<?php
            $dateday = $yearmin;
            while($dateday <= $yearmax)
            {?>
               <option><?php echo $dateday;?></option>      
<?php            $dateday++;
            }?>
         </select></p>
      
<?php /* The fields created with the function 'formfield'*/ ?>
         <p>Title: <?php formfield("title", "input", "size="10" maxlength="40"","0");?></p>
         <p>Speaker: <?php formfield("speaker","select","","1");?></p>
         <p>Title of Series: <?php formfield("seriestitle", "input", "size="10" maxlength="40"","0");?></p>
         <p>Passage: <?php formfield("passage", "input", "size="10" maxlength="40"","0");?></p>
         
         <p>Meeting: 
         <select name="meeting" id="meeting">
            <option></option>
            <option value="Early Morning Evening">Combined</option>
            <option>Early</option>
            <option>Morning</option>
            <option>Evening</option>
            <option>Tuesday</option>
            <option>Special</option>                  
         </select></p>
<!-- End of the extra fields div.  -->
</div>
<!-- To submit the info, why not a submit button? -->
<input type="submit" value="search" id="fsearch" />
<!-- Some people might want to start over  -->
<input type="reset" value="Reset" id="freset" />
</form>
<?php
   //Get all the values from the form and put them into variables
   $lockword = $_POST['lockword'];
   $month = $_POST['month'];
   $year = $_POST['year'];
   $title = $_POST['title'];
   $speaker = $_POST['speaker'];
   $series_title = $_POST['seriestitle'];
   $passage = $_POST['passage'];
   $meeting = $_POST['meeting'];
   
   //If the variables are blank (because of the forms being left blank), make them = * for easy database searching.
   if($lockword==""){$lockword = "";}
   if($month==""){$month = "%";}
   if($year==""){$year = "%";}
   if($title==""){$title = "";}//else{$title = "%".title."%";}
   if($speaker==""){$speaker = "";}
   if($series_title==""){$series_title = "";}
   if($passage==""){$passage = "";}
   if($meeting==""){$meeting = "";}
   //If every field in the form was left blank, (and so now every variable = *), then do not even search for the result. If something has been filled out, then
   //search for the result and display it.
   if(($lockword =="   ") && ($month =="%") && ($year =="%") && ($speaker =="") && ($series_title =="") && ($passage =="") && ($meeting ==""))
   {?><div>You have not put in any search terms. Please choose something to search for.</div><?php }
   else
   {
      //Make the keyword into an array of words
      $lockword = explode(" ",$lockword);
      if($month < 10 && $month != "%"){$month = "0".$month;}
      $searchdate = $year."-".$month."-%";
      // Start of the MySQL query that is assigned to the variable $stg.
      $stg = "SELECT * FROM sermons WHERE ( date like '$searchdate' AND title like '%$title%' AND speaker like '%$speaker%' AND seriestitle like '%$series_title%' AND passage like '%$passage%' AND meeting like '%$meeting%' ) AND (";
      //Set $n = 1 so that we can add in " OR", the secont time in the following loop.
      $n = 1;
      // A loop that adds a string $stg changing the field value ($value) each time around for each word in $lockword, putting " OR " in between each pass.
      //eg. first time round it would say "title like (word 1)", second time round it would say "title like (word 2)"
      foreach($lockword as $num => $value)
      {
         if($n > 1)
         {
            $stg = $stg." AND ";
         }
         //String
         $stg = $stg."( title like '%$value%' OR speaker like '%$value%' OR seriestitle like '%$value%' OR passage like '%$value%' )";
         $n++;//Adds 1 to n so that the if statment is executed.
      }
      $stg = $stg.")";
      echo $stg;
      //Search the relevent fields of the database for the keywords, using 'OR' so that only one of the fields has to match
      $result = mysql_query($stg);
      //Spit out the results
      while($row = mysql_fetch_array($result))
      {
         
         ?><h4><a href="<?php echo $row['outlineurl'] ?>"><?php echo $row['title'] ?></a></h4>
         <p><strong>Series: </strong><?php echo $row['seriestitle'] ?><br />
         <strong>Passage: </strong><?php echo $row['passage'] ?><br />
         <strong>Date: </strong><?php echo date("D, F j, Y", strtotime($row['date'])) ?><br />
         <strong>Meeting: </strong><?php echo modmeeting($row['meeting']) ?></p>
<?php   }
   }
?>
http://portablejim.uni.cc
I wish Windoze had apt. Will the next Windoze have sudo?

joomdev
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Wed Oct 17, 2007 10:40 pm

Re: Recently found OOP PHP. Want Help re-coding Component.

Post by joomdev » Tue Dec 18, 2007 6:12 pm

and you are asking this in Joomla101 forum ? maybe try some other subforum. I tried a question here, no reply.


Post Reply