onclick display insert form

only1wizard
only1wizard used Ask the Experts™
on
how do i get this insert form to display for the results record id 1 once and record id 2 once instead of looping though all of foreign keys for record 1. this is in refferance to the code for sub query insert form on click.

for example: i have 10 records for record id 1 then the last record for 1 id is 10 that should show the insert form. instead of having all 10 records showing the form.

thanks in advance for your help!

<?php require_once('Connections/Del_Comments.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO ascomments (Id, Memo, `Date`) VALUES (%s, %s, now())",
                       GetSQLValueString($_POST['Id'], "int"),
                       GetSQLValueString($_POST['Memo'], "text"));

  mysql_select_db($database_Del_Comments, $Del_Comments);
  $Result1 = mysql_query($insertSQL, $Del_Comments) or die(mysql_error());


$Cfk = mysql_insert_id();
$UId = '2';

  $insertSQL = sprintf("INSERT INTO asmanystatusupdate (Id, SFk, CFk, CFk0, UIdFk0, UIdFk1, Dates) VALUES (%s, %s, '$Cfk', '$Cfk', '$UId', '$UId', now())",
                       GetSQLValueString($_POST['Id'], "int"),
                       GetSQLValueString($_POST['SFk'], "int"));

  mysql_select_db($database_Del_Comments, $Del_Comments);
  $Result1 = mysql_query($insertSQL, $Del_Comments) or die(mysql_error());
}


mysql_select_db($database_Del_Comments, $Del_Comments);
$query_Recordset1 = "SELECT * FROM ahcomments";
$Recordset1 = mysql_query($query_Recordset1, $Del_Comments) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

</head>

<body>
<?php
//c.sfk={$row['sfk']}

mysql_select_db($database_Del_Comments, $Del_Comments);

$result = sprintf("SELECT a.Id, a.Type, 
a.Dates, 
a.UIdFk as UIdFk, 
b.Id as Did, 
b.comment as Comment, 
b.dates as Day, 
b.sfk as Sfk ,
aes_decrypt(e.ProfileName, '$Ukey') as Name,
substr(i.Path, 4) as Path
FROM 
asstatusupdate as a   
left join asstatusdata as b 
on a.id = b.sfk 
right join ASWebInfo as e
on e.Uidfk = a.uidfk
right join ASManyAlbums as f
on f.UserId=a.uidfk
right join ASAlbums as g
on f.AlbumId=g.Id
right join ASTitle as h
on g.Id=h.AlbumId
right join ASData as i
on h.Id=i.TitleId
where a.uidfk in (select friendid from asfriends where uidfk0='1') and i.DefaultProfilePic='Y' order by dates desc;
");
$statusUpdate = mysql_query($result, $Del_Comments) or die(mysql_error());

$category_id = '';

echo '<table align="center" width="40%" border="3" cellpadding="0" cellspacing="0">'; 
while($row = mysql_fetch_array($statusUpdate)) {
	if ($row['Id'] != $category_id) {
		$category_id = $row['Id'];
	
		// grab table variables	
	    	$Type = $row['Type'];
		$Dates = $row['Dates'];
		$Comment = $row['Comment'];
		$Name = $row['Name'];
		$Path = $row['Path'];
		$FriendId = $row['UIdFk'];
		$SFK = $row['Sfk'];
		
		echo '<tr>';
		echo '	<td align="center">', $Name, '<br><a href="source/source.php?FriendId=',$FriendId,'"><img src="',$Path,'" height="120" width="120" align="middle" border="3" /></a></td>';
		echo '	<td align="center"> ',$Dates,'<br><br>',$Type,'<br/>',$Comment,'</td>'; 
		echo '	<td>',$category_id,'</td>';
		echo '	<td>',$SFK,'</td>';
		echo '</tr>';
	} // end of if

mysql_select_db($database_Del_Comments, $Del_Comments);
	$comments= sprintf("SELECT 
          c.sfk as sfk1,
          c.UIdFk0,
          d.Memo as Memo,
          d.Date as Dates1,
          e.ProfileName,
          substr(i.Path, 4) as Path
     FROM 
     asmanystatusupdate as c
     left join ascomments as d     on d.id = c.cfk
     right join ASWebInfo as e     on e.Uidfk = c.uidfk0
     right join ASManyAlbums as f     on f.UserId=c.uidfk0
     right join ASAlbums as g     on f.AlbumId=g.Id
     right join ASTitle as h     on g.Id=h.AlbumId
     right join ASData as i     on h.Id=i.TitleId
     where c.sfk={$row['Sfk']} AND c.uidfk0 in (select friendid from asfriends where uidfk0='1') and i.DefaultProfilePic='Y'  order by dates desc;
     ");
$statusComments = mysql_query($comments, $Del_Comments) or die(mysql_error());
	while($user_row = mysql_fetch_array($statusComments)){
		// test the query
		//if($row['sfk'] == $user_row['sfk']) 
		{
			
			$Dates1 = $user_row['Dates1'];
			$Memo = $user_row['Memo'];
			$Name1 = $user_row['Name'];
			$Path1 = $user_row['Path'];
			$FriendId1 = $user_row['UIdFk0'];
			$SFK1 = $user_row['sfk1'];
			
			//row and cell for "outer" while		
			 echo '<tr>';
			 echo '<td colspan="4" style="padding-left:1em;">';
				// display table
				echo '<a href="#0'."$SFK1".'" onclick="toggle_visibility(\'foo1'."$SFK1".'\');" ;return false\" style=\"position:relative\">Click here to show records</a>'; // to show comment records
  				echo '<div id="foo1'."$SFK1".'"style="position:relative;display:none">'; // to show comment records
				echo '<table align="center" width="60%" border="3" cellpadding="0" cellspacing="0">'; 
				echo '<tr>';
				echo '	<td align="center">',$Name1,'<br/><a href="source/source.php?FriendId=',$FriendId1,'"><img src="',$Path1,'" height="120" width="120" align="middle" border="3" /></a></td>';
				echo '	<td align="center" width="40%"> ',$Dates1,'<br/><br/><br/>',$Memo,'</td>';
				echo '	<td>' ,$category_id, '</td>'; 
				echo '	<td>',$SFK1,'</td>';
				echo '</tr>';
				echo '</table>';
				echo '</div>';  // for comment results show query
			//end row and cell for outer while
			 //echo '</td>';
			 //echo '</tr>';
				
  echo '<form action="$editFormAction" method="post" name="form1" id="form1">';
  echo '<a href="#'."$SFK1".'" onclick="toggle_visibility(\'foo'."$SFK1".'\');" ;return false\" style=\"position:relative\">Click here to toggle visibility of element #foo</a>';
  echo '<div id="foo'."$SFK1".'"style="position:relative;display:none">';echo '<table align="center">';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">Memo:</td>';
      echo '<td><input type="text" name="Memo" value="" size="32" /></td>';
    echo '</tr>';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">&nbsp;</td>';
     echo ' <td nowrap="nowrap" align="right">&nbsp;</td>';
   echo ' </tr>';
    echo '<tr valign="baseline">';
     echo ' <td nowrap="nowrap" align="right">&nbsp;SFK:' ."$SFK1". '</td>';
     echo ' <td><input type="submit" value="Insert record" /></td>';
   echo ' </tr>';
 echo ' </table>';
  echo '<input type="hidden" name="Id" value="" />';
  echo '<input type="hidden" name="Id" value="" />';
 echo ' <input type="hidden" name="SFk" value="' ."$SFK1". '" />';
 echo ' <input type="hidden" name="MM_insert" value="form1" />';
 echo '</form>';
echo '</div>';
		} // end of if

	} // end of while loop
}// end of main loop
echo '</table>';
?>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
can you provide a link to your page.

Author

Commented:
i have a pdf file available i dont have this on production yet. the hyperlinks labeled part 1 are the ones in question.

if you really need a live page i can make it happen just let me know.

thanks in advance for your help!

Theo Werntz II
Untitled-Document.pdf
i've created a 3rd query that tests for the same id variable and limit 1 desc which will return the last row. i also included a if statement to output the forms data for == 0 or > 0.

SELECT 
c.sfk as sfk1,
c.UIdFk0
FROM 
asmanystatusupdate as c
where c.sfk={$row['Sfk']} AND c.UIdFk0 in (select FriendId from asfriends where UIdFk0='1') order by c.sfk desc limit 1;

Open in new window


if(mysql_num_rows($user_row_comment) == 0) {
				echo '<tr>';
				echo '<td>'; echo '&nbsp;'; echo '</td>';
				echo '<td align="right">';
								
				echo '<a href="#'."$category_id".'" onclick="toggle_visibility(\'foo2'."$category_id".'\');" ;return false" style="position:relative" align="right">Comment </a>';
				echo '</td>';
				echo '</tr>';
				echo '</table>';
	echo '<form action="' ."$editFormAction". '" method="post" name="form1" id="form1">';
 // echo '<tr>';			
   //echo '</tr>';
  echo '<div id="foo2'."$category_id".'"style="position:relative;display:none">';
  echo '<table align="right">';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">Comment:</td>';
      echo '<td><input type="text" name="Memo" value="" size="32" /></td>';
    echo '</tr>';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">&nbsp;</td>';
     echo ' <td nowrap="nowrap" align="right">&nbsp;</td>';
   echo ' </tr>';
    echo '<tr valign="baseline">';
     echo ' <td nowrap="nowrap" align="right">&nbsp</td>'; //;SFK:' ."$category_id". ' testing
     echo ' <td><input type="submit" value="Insert record" id="$category_id"/></td>';
   echo ' </tr>';
 echo ' </table>';
  echo '<input type="hidden" name="Id" value="" />';
  echo '<input type="hidden" name="Id" value="" />';
 echo ' <input type="hidden" name="SFk" value="' ."$category_id". '" />';
 echo ' <input type="hidden" name="MM_insert" value="form1" />';
 echo '</form>';
echo '</div>';
					} //end of else

				if (mysql_num_rows($user_row_comment) > 0) 
				{ 
				echo '<tr>';
				echo '<td>'; echo '&nbsp;'; echo '</td>';
				echo '<td align="right">';
								
				echo '<a href="#'."$SFK1".'" onclick="toggle_visibility(\'foo3'."$SFK1".'\');" ;return false" style="position:relative" align="right">Comment </a>';
				echo '</td>';
				echo '</table>';
	echo '<form action="' ."$editFormAction". '" method="post" name="form1" id="form1">';
  echo '<tr>';			
   echo '</tr>';
  echo '<div id="foo3'."$SFK1".'"style="position:relative;display:none">';
  echo '<table align="right">';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">Comment:</td>';
      echo '<td><input type="text" name="Memo" value="" size="32" /></td>';
    echo '</tr>';
    echo '<tr valign="baseline">';
      echo '<td nowrap="nowrap" align="right">&nbsp;</td>';
     echo ' <td nowrap="nowrap" align="right">&nbsp;</td>';
   echo ' </tr>';
    echo '<tr valign="baseline">';
     echo ' <td nowrap="nowrap" align="right">&nbsp;</td>'; //SFK:' ."$SFK1". '
     echo ' <td><input type="submit" value="Insert record" id="$SFK1"/></td>';
   echo ' </tr>';
 echo ' </table>';
  echo '<input type="hidden" name="Id" value="" />';
  echo '<input type="hidden" name="Id" value="" />';
 echo ' <input type="hidden" name="SFk" value="' ."$SFK1". '" />';
 echo ' <input type="hidden" name="MM_insert" value="form1" />';
 echo '</form>';
echo '</div>';
				} // end of while loop	

Open in new window

Author

Commented:
after some additional coding i thought of using a 3rd query to accomplish what i was trying to acccomplish of which worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial