Jump to content

Adding checkbox when call function


Orasion

Recommended Posts

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?? :confused:

Link to comment
https://forums.phpfreaks.com/topic/251820-adding-checkbox-when-call-function/
Share on other sites

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().

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.