php - Improving while coding -


i need check if monthly contract exist , if signed. improve code because seems heavy

it's time switch pdo , maybe can rewrite things speed script

basically have table 2 columns 1st column have date reference, 2013/jan, 2013/fev ... , 2nd column display check if document exist , have signature.

here code. there way improve?

// query db // contract $contract = array(); $query1 = mysql_query("select * contracts                        ic_id='28'"); while ($row = mysql_fetch_assoc($query1)) {     $contract[] = $row['month'];           }  $signature = array(); $query1 = mysql_query("select * contracts                        ic_id='28'"); while ($row = mysql_fetch_assoc($query1)) {     $signature[] = $row['sign'];           }  // start , end date  $startyear =  '2012'; $startmonth = '12';  $endyear =  '2013'; $endmonth = '12';  $startdate = strtotime("$startyear/$startmonth/01"); $enddate   = strtotime("$endyear/$endmonth/01");  $currentdate = $enddate; 

results table

<tbody>     <?php         while ($currentdate >= $startdate) {         $foo = date('y-m',$currentdate);            $currentdate = strtotime( date('y/m/01/',$currentdate).' -1 month');     ?>            <tr class="grade">         <td>                <?php   echo $foo; ?>         </td>          <td>         <?php           if (in_array($foo, $contract)) {          echo "<img src='images/ok.png';         } else {         echo "<img src='images/ko.png'/>";            }          if (in_array($foo, $signature)) {          echo "<img src='imagens/ok.png'/>";         } else {         echo "<img src='imagens/sign.png'/>";            }          ?>         </td>     </tr>      <?php     $i++;     }     ?>                     </tbody> 

connect pdo.

 <?php  $contract = array();  $signature = array();  $db = new pdo('mysql:host=localhost;dbname=<somedb>', '<username>', 'password');  $st = $db->prepare("select month ,sign contracts   ic_id=?");  $st->execute(array('28'));   foreach($st $row)  {    $contract[] = $row['month'];     $signature[] = $row['sign'];  }  ?> 

why use variables there contant information? example in start , end date. know mysql_* depracated should move pdo asap. naming variables $var1, $var2 bad convention.

check these links:

  1. zend conventions
  2. pear standards

as wrote above

$startyear =  '2012'; $startmonth = '12';  $endyear =  '2013'; $endmonth = '12';  $startdate = strtotime("$startyear/$startmonth/01"); $enddate   = strtotime("$endyear/$endmonth/01"); 

these variables seem constant use constant string.

$startdate = strtotime("2012/12/01"); $enddate   = strtotime("2013/12/01"); 

moreover, select columns in query use 2, i've changed query.

this part of code

<?php       if (in_array($foo, $contract)) {      echo "<img src='images/ok.png';     } else {     echo "<img src='images/ko.png'/>";        }      if (in_array($foo, $signature)) {      echo "<img src='imagens/ok.png'/>";     } else {     echo "<img src='imagens/sign.png'/>";        }      ?> 

can changed to

<?php       echo "<img src='images/".(in_array($foo, $contract) ? "ok" : "ko").".png";     echo "<img src='imagens/".(in_array($foo, $signature) ? "ok" : "ko").".png"; ?> 

less code :)

echo has shortcut syntax, can follow opening tag equals sign. can change this:

<?php   echo $foo; ?> 

to this

<?=$foo?> 

the last tip use pre incrementation because faster. if don't need post incrementaction use pre!

change $i++ ++$i;


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -