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

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

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.