claro Posted February 8, 2013 Share Posted February 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/ Share on other sites More sharing options...
KevinM1 Posted February 8, 2013 Share Posted February 8, 2013 Why are you trying to create a new object in each iteration of the loop? And why are you overwriting that object in each iteration of the loop? Quote Link to comment https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/#findComment-1410951 Share on other sites More sharing options...
claro Posted February 8, 2013 Author Share Posted February 8, 2013 I haven't thought of that. It doesn't display any error so I thought it's okay. What do you mean by overwriting objects? thank you for the response. Quote Link to comment https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/#findComment-1410953 Share on other sites More sharing options...
claro Posted February 8, 2013 Author Share Posted February 8, 2013 seems like that is the answer. thank you ! Quote Link to comment https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/#findComment-1410955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.