Jump to content

class declared in loop


claro

Recommended Posts

Greetings! I'm practicing oop. I have a problem in declaring classes in loop.

 public function save($sn,$year,$type)
   {
       Connection::conn();
       try{
              $sql = "  SELECT * FROM device.sn
         WHERE  sn_number = :sn
  AND sn_delstat IS NULL";  

  $q = connection::$db->prepare($sql);
          $q->bindParam(':sn', $sn,PDO::PARAM_STR);
  $q->execute();
   $count = $q->rowCount();

          if ($count ==0)
   {
    $sql = "INSERT INTO device.sn (dname_id,sn_year,sn_number)
          VALUES (:dname,:year,:snumber)" ;

 $q = connection::$db->prepare($sql);
               $q->bindParam(':snumber', $sn);
               $q->bindParam(':year', $year);
               $q->bindParam(':dname', $type);
               $q ->execute();
               $count = $q->rowCount();

            if ($count>0)
     {
    echo "<div class='space'></div>";
                  echo '<div class ="success">Successfully added'.'</div>';
     }
     else { echo '<div class ="error">Error in adding.</div>'; }
   }
   else
   {
    echo '<div class ="error">'.$sn.'already exist.</div>';

    }

       }catch (Exception $e){

               print "Error!: ". $e->getMessage();
       }

   }

 

and I call my class this way

foreach ($val as $sn)
 {
                                           if (!empty($sn))
                                           {
                                               $dev = new Sn();
                                               $dev-> save($sn,$year,$device);
                                           }
                                           else
                                           {
                                           echo 'sn requied';
                                           }
 }

 

 

My code is all working, my problem is to display them properly, like when I save 2 items,

 

there are two

 

'<div class ="success">Successfully added'.'</div>

is displayed. How can I make it display in a single div including the errors. Any help will do. Thank you.

Link to comment
https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.