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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.