Jump to content

embaded php into html problem


doug007

Recommended Posts

Hi Guys,

 

I have the below code that returns a checkbox as checked if it matches a certain condition. i have embaded the php into the html opening and closing tags like below, but whenever somthing is checked instead of making a checkbox just check it is aslo echoing this "checked> " next to the checkbox. does anyone please know what i am doing wrong?

echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; 
$query2 ='SELECT
tag_options_id,
tag_id,
tag_name,
user_id
FROM
tags_options';
$result2 = mysql_query($query2);
if (!$result){
echo mysql_error();
}
while ($row2 = mysql_fetch_array($result2)) { 
if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){
//global $checked; 
$checked = 'checked'; 
echo " $checked> </input>"; 
}
} 

 

Link to comment
Share on other sites

Your while loop is returning two positive results (2 rows)  Check your query. Alternatively (which is not ideal) you could get rid of your while loop to give:

 

$row2 = mysql_fetch_array($result2);
if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){
//global $checked; 
$checked = "checked='checked'"; 
echo " $checked> </input>"; 
}// end if

 

That's really not a good way of doing it though.  What is good is the way I replaced $checked='checked'; with $checked="checked='checked'";  That makes it valid xhtml 1.0

 

 

Link to comment
Share on other sites

You included that at the bottom of your script. If you do not want it then remove it, else if it should do something else then modify your echo statement appropriately.

<?php
$checked = 'checked'; 
echo " $checked> </input>";
?>

 

yes i need my while loop there, but what do you mean with modifying my echo statement appropriatley?

Link to comment
Share on other sites

WolfRage made a mistake.  The echo statement is not the problem.  The problem is that the while loop is causing your echo statement to execute twice. The code you end up with in your html is then...

<input name="c_options[$i]" style="border-style: solid; border:0px solid; border-color: #FFFFFF" type="checkbox" border="0" value="tag_desc_id" checked></input> checked></input>

 

check it out in your source.

Link to comment
Share on other sites

WolfRage made a mistake.  The echo statement is not the problem.  The problem is that the while loop is causing your echo statement to execute twice. The code you end up with in your html is then...

<input name="c_options[$i]" style="border-style: solid; border:0px solid; border-color: #FFFFFF" type="checkbox" border="0" value="tag_desc_id" checked></input> checked></input>

 

check it out in your source.

 

this sounds possible, but how do i fix this?  i need while loop to only loop through what is checked and if checked mark each checkbox as checked?

 

cheers

Link to comment
Share on other sites

Hopefully I can redeem my self in Defeated's Eyes.

 <?php
$query2 ='SELECT
tag_options_id,
tag_id,
tag_name,
user_id
FROM
tags_options';
$result2 = mysql_query($query2);
if (!$result){
echo mysql_error();
}
while ($row2 = mysql_fetch_array($result2)) { 
if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){
//global $checked; 
echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; 
$checked = "checked='checked'";  
echo " $checked> </input>"; 
}
} 
?>

Link to comment
Share on other sites

Hopefully I can redeem my self in Defeated's Eyes.

 <?php
$query2 ='SELECT
tag_options_id,
tag_id,
tag_name,
user_id
FROM
tags_options';
$result2 = mysql_query($query2);
if (!$result){
echo mysql_error();
}
while ($row2 = mysql_fetch_array($result2)) { 
if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){
//global $checked; 
echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\""; 
$checked = "checked='checked'";  
echo " $checked> </input>"; 
}
} 
?>

 

thanks WolfRage, this works, but the type type=\"checkbox\" is no longer wokring?

 

Link to comment
Share on other sites

The problem is you are doing more than what you are showing me in code. Until you present the rest of the code I am not a mind reader and can't guess what your code was doing before. Post all of the code and we will straighten it out. k.

Link to comment
Share on other sites

Nice... but I suspect that you will end up with two inputs for each checked checkbox.

 

I think that what you are trying to do would be better achieved with a WHERE statement in your mysql.  The trouble will still be that you have two pieces of data that satisfy your if clause.

 

$tag_desc_id = $row['tag_desc_id'];
$user_info = $user_info[user_id];

$query2 ="SELECT tag_id FROM tags_options
WHERE tag_id='$tag_desc_id' AND user_id='$user_info'";

$result2 = mysql_query($query2) or die(mysql_error());

while ($row2 = mysql_fetch_array($result2)) 
{ 
     if(!empty($row2['tag_id']))
     {
          echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" checked='checked' />";  
     }//end if
     else
     {
        echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" />"; 
     }//end else 
}//end while

 

Ah, Sorry WolfRage.  I stand corrected.  This should work in that case.

 

Scratch that.  It won't work.  Like WolfRage said... need more code.

 

or just try adding

 

else
{
//global $checked; 
echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\"";  
echo "> </input>"; 
}// end else

after your if statement

Link to comment
Share on other sites

Nice... but I suspect that you will end up with two inputs for each checked checkbox.

 

I think that what you are trying to do would be better achieved with a WHERE statement in your mysql.  The trouble will still be that you have two pieces of data that satisfy your if clause.

 

$tag_desc_id = $row['tag_desc_id'];
$user_info = $user_info[user_id];

$query2 ="SELECT tag_id FROM tags_options
WHERE tag_id='$tag_desc_id' AND user_id='$user_info'";

$result2 = mysql_query($query2) or die(mysql_error());

while ($row2 = mysql_fetch_array($result2)) 
{ 
     if(!empty($row2['tag_id']))
     {
          echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" checked='checked' />";  
     }//end if
     else
     {
        echo "<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$tag_desc_id."\" />"; 
     }//end else 
}//end while

 

Ah, Sorry WolfRage.  I stand corrected.  This should work in that case.

 

Scratch that.  It won't work.  Like WolfRage said... need more code.

 

or just try adding

 

else
{
//global $checked; 
echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\"";  
echo "> </input>"; 
}// end else

after your if statement

 

same problem as above.

 

here is my entire code, its little long, but really would appriciate if anyone cared to look at it.

 

function do_checkboxes($userid){
global $profile_info,  $setting, $smarty_object, $user_info;;

if(isset($_POST['set_options'])){
  
if(!$_POST['c_options']==""){
  foreach($_POST['c_options'] as $key=>$value) {
     
       $tag_name=mysql_real_escape_string($_POST['tag_name'][$key]);
  $id=mysql_real_escape_string($value);
		  		
	$delete = "DELETE FROM tags_options where user_id = '".$user_info[user_id]."' and tag_id <> '".$id."'";
        $deleteSQL = mysql_query($delete) or die(mysql_error());
	//echo  $deleteSQL.'<br />';
}

foreach($_POST['c_options'] as $key=>$value) {
  		$tag_name=mysql_real_escape_string($_POST['tag_name'][$key]);
   $id=mysql_real_escape_string($value);

	$oaction = "INSERT INTO tags_options (tag_id, tag_name, user_id) 
            		VALUES('$id', '".$tag_name."', '".$user_info[user_id]."')";
//	echo $oaction.'<br />';

        $optionsSQL = mysql_query($oaction) or die(mysql_error());

	}
	if($optionsSQL){
		echo '<font color=red>Your options have been saved</font>';
	}	
}
}

$query ="SELECT 
			tag_desc_id, 
			tag_name,
			parent_id
	FROM 
			tags_description 
	ORDER BY tag_name ASC";




$result = mysql_query($query);

if (!$result){
	echo mysql_error();
}
elseif(mysql_num_rows($result)<=0){
	//echo "No entries";
}else{

echo "<table  border=\"0\"  cellspacing=\"0\" cellpadding=\"10\">";
echo "<form method=\"post\">";

$i=0;

while ($row = mysql_fetch_array($result)) {

$i++;
$incr = ($incr == 3) ? 1 : $incr + 1; 

if($incr == 1) echo "<tr>";

echo "<td class=\"formcheckbox2\">";
$query2 ='SELECT 
				tag_options_id, 
				tag_id,
				tag_name,
				user_id
		FROM 
			tags_options';
		$result2 = mysql_query($query2);
		if (!$result2){
				echo mysql_error();
			}

echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\"";
$checked = '';  
										while ($row2 = mysql_fetch_array($result2)) {
										if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id]){
											//global $checked;
											$checked = 'checked="checked"';

					echo " $checked> </input>";

				}


			}
			echo"<input type=\"hidden\" name=\"tag_name[$i]\" value=\"".$row['tag_name']."\"></input>";


				echo $row['tag_name'];	
		echo "</td>";
if($incr == 3) echo "</tr>";			


}

echo "<tr>";
echo "<td>

		<input type=\"hidden\" name=\"set_options\" value=\"set_options\">
		<input name=\"GO\" type=\"submit\" id=\"GO\" value=\"Save Changes\" class=\"button\">";
echo"</td>";
echo "</tr>";
echo "</form>";
echo "</table>";

	}
}/*

 

Link to comment
Share on other sites

aha! Try this.  There seems to be an extra curly bracket at the end.... but perhaps it's not extra.

All I have done is put the echo statement outside the if statement and structured the code a bit so that I could see what was going on.  If it doesn't work I'll get to it later.

 


function do_checkboxes($userid){
global $profile_info,  $setting, $smarty_object, $user_info;;

	if(isset($_POST['set_options']))
{
  
		if(!$_POST['c_options']=="")
	{
  			foreach($_POST['c_options'] as $key=>$value) 
		{
    			$tag_name=mysql_real_escape_string($_POST['tag_name'][$key]);
			$id=mysql_real_escape_string($value);
			$delete = "DELETE FROM tags_options where user_id = '".$user_info[user_id]."' and tag_id <> '".$id."'";
        		$deleteSQL = mysql_query($delete) or die(mysql_error());	
			//echo  $deleteSQL.'<br />';
		}//end foreach

			foreach($_POST['c_options'] as $key=>$value) 
		{
  				$tag_name=mysql_real_escape_string($_POST['tag_name'][$key]);
			$id=mysql_real_escape_string($value);
			$oaction = "INSERT INTO tags_options (tag_id, tag_name, user_id) 
            	VALUES('$id', '".$tag_name."', '".$user_info[user_id]."')";
			echo $oaction.'<br />';
			$optionsSQL = mysql_query($oaction) or die(mysql_error());
		}//end foreach


		if($optionsSQL)
		{
			echo '<font color=red>Your options have been saved</font>';
		}//end if

	}//end if no c_options

}//end if set_options

$query ="SELECT tag_desc_id, tag_name,parent_id	
FROM  tags_description 
ORDER BY tag_name ASC";

$result = mysql_query($query);

if (!$result)
{
echo mysql_error();
}//end if

elseif(mysql_num_rows($result)<=0)
{
//echo "No entries";
}//end elseif
else
{
echo "<table  border=\"0\"  cellspacing=\"0\" cellpadding=\"10\">";
echo "<form method=\"post\">";
$i=0;
while ($row = mysql_fetch_array($result)) 
{
	$i++;
	$incr = ($incr == 3) ? 1 : $incr + 1; 
	if($incr == 1) echo "<tr>";
	echo "<td class=\"formcheckbox2\">";

	$query2 ='SELECT tag_options_id, tag_id,tag_name,user_id
	FROM tags_options';
	$result2 = mysql_query($query2);

	if (!$result2)
	{
		echo mysql_error();
	}//end if

	echo"<input name=\"c_options[$i]\" style=\"border-style: solid; border:0px solid; border-color: #FFFFFF\" type=\"checkbox\" border=\"0\" value=\"".$row['tag_desc_id']."\"";

	$checked = '';  

	while ($row2 = mysql_fetch_array($result2)) 
	{
		if($row2['tag_id'] == $row['tag_desc_id'] && $row2['user_id'] == $user_info[user_id])
		{
			//global $checked;
			$checked = 'checked="checked"';
		}//end if

		echo " $checked> </input>";

	}//end while

	echo"<input type=\"hidden\" name=\"tag_name[$i]\" value=\"".$row['tag_name']."\"></input>";
	echo $row['tag_name'];
	echo "</td>";

	if($incr == 3) echo "</tr>";

}//end while

echo "<tr>";
echo "<td>
<input type=\"hidden\" name=\"set_options\" value=\"set_options\">
<input name=\"GO\" type=\"submit\" id=\"GO\" value=\"Save Changes\" class=\"button\">";
echo"</td>";
echo "</tr>";
echo "</form>";
echo "</table>";

}//end else


// What's this curly bracket for?
}/*

Link to comment
Share on other sites

Ok the problem with placing the echo statement outside the if statement now is that it echos this:

 

> > > > > > > > > > > > > > > > > > > checked="checked"> checked="checked"> next to each checkbox that is checked, but does not check the actual checkbox.

 

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.