Orasion Posted November 26, 2011 Share Posted November 26, 2011 Hi all. I try to adding checkbox when I call my function, my Class code is class SQLconn{ function fetchData($table_name, $argument, $linebreak, $end_linebreak){ $result = mysql_query("select * from $table_name where status='$argument'"); while($row=mysql_fetch_row($result)){ echo $linebreak.$row[1]."<input type='checkbox' name='option' id='checkbox' value=$row[0] />".$end_linebreak; //$row[0] is 'job_id' from database } } } and my view code is like this <?php $connect = new SQLconn(config()); $connect->connectDB(); $connect->fetchData("todo", "Undone", "<li>", "</li>"); //this is where the problem is $connect->closeConn(); ?> With that code I succesfully achieve my goal to add checkbox with right 'job_id' value but this is a workaround and not good because if I code it this way my function will be broken if use it somewhere else. My question is, how can I fix my code so I can add checkbox to view page but I can keep my function clean? Really need opinion about this?? Link to comment https://forums.phpfreaks.com/topic/251820-adding-checkbox-when-call-function/ Share on other sites More sharing options...
Laash Posted November 26, 2011 Share Posted November 26, 2011 First of all, use different name and different id for every checkbox. Like this: "<input type='checkbox' name='option'"+$row[0]+"' id='checkbox'"+$row[0]+"' value='"+$row[0]"' />"; Second, put the checkbox's value in quotes, like in the above example. Link to comment https://forums.phpfreaks.com/topic/251820-adding-checkbox-when-call-function/#findComment-1291385 Share on other sites More sharing options...
Orasion Posted November 27, 2011 Author Share Posted November 27, 2011 okay. I'll try that. Thx for your answer Link to comment https://forums.phpfreaks.com/topic/251820-adding-checkbox-when-call-function/#findComment-1291490 Share on other sites More sharing options...
MasterACE14 Posted November 27, 2011 Share Posted November 27, 2011 why not pass the field name to the method as an argument? function fetchData($table_name, $argument, $linebreak, $end_linebreak, $field){ $result = mysql_query("select * from $table_name where status='$argument'"); while($row=mysql_fetch_assoc($result)){ echo $linebreak.$row[1]."<input type='checkbox' name='option' id='checkbox' value=$row[$field] />".$end_linebreak; //$row[$field] is 'job_id' from database } } you just have to change $row[1] to $row['fieldname'] and that'd work great. Also note I changed mysql_fetch_row() to mysql_fetch_assoc(). Link to comment https://forums.phpfreaks.com/topic/251820-adding-checkbox-when-call-function/#findComment-1291497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.