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. 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? 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. 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 ! Link to comment https://forums.phpfreaks.com/topic/274194-class-declared-in-loop/#findComment-1410955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.