Jump to content

generate comments by a dynamic id


xtiancjs

Recommended Posts

Hi am still having a little trouble with viewing my comments from the DB. I have 2 tables brokers and comments, I have managed I think to link the primary id of one table(brokers)to a foreign key of the other table(comments). when a user adds a comment from the form it stores in the DB and is assigned an id that is the same as the brokers primary id. The problem is arising from viewing the comments, ALL the comments are still being viewed on each page, I now have a WHERE statement in my recordset and was hoping that would fix it . you can view my example at
[a href=\"http://slazo.com/brokerrate/pages/profile.php\" target=\"_blank\"]http://slazo.com/brokerrate/pages/profile.php[/a] here is all the code (sorry for the huge chunk of code-not sure what would be needed): The code is dreamweaver 8 code:

<?php require_once('../../../Connections/broker.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO comments (broker_id, comment) VALUES (%s, %s)",
GetSQLValueString($_POST['broker_id'], "int"),
GetSQLValueString($_POST['comment'], "text"));

mysql_select_db($database_broker, $broker);
$Result1 = mysql_query($insertSQL, $broker) or die(mysql_error());
}

$maxRows_brokers = 1;
$pageNum_brokers = 0;
if (isset($_GET['pageNum_brokers'])) {
$pageNum_brokers = $_GET['pageNum_brokers'];
}
$startRow_brokers = $pageNum_brokers * $maxRows_brokers;

mysql_select_db($database_broker, $broker);
$query_brokers = "SELECT * FROM brokers";
$query_limit_brokers = sprintf("%s LIMIT %d, %d", $query_brokers, $startRow_brokers, $maxRows_brokers);
$brokers = mysql_query($query_limit_brokers, $broker) or die(mysql_error());
$row_brokers = mysql_fetch_assoc($brokers);

if (isset($_GET['totalRows_brokers'])) {
$totalRows_brokers = $_GET['totalRows_brokers'];
} else {
$all_brokers = mysql_query($query_brokers);
$totalRows_brokers = mysql_num_rows($all_brokers);
}
$totalPages_brokers = ceil($totalRows_brokers/$maxRows_brokers)-1;

$maxRows_comments = 3;
$pageNum_comments = 0;
if (isset($_GET['pageNum_comments'])) {
$pageNum_comments = $_GET['pageNum_comments'];
}
$startRow_comments = $pageNum_comments * $maxRows_comments;

mysql_select_db($database_broker, $broker);
$query_comments = "SELECT * FROM comments, brokers WHERE comments.broker_id = brokers.broker_id";
$query_limit_comments = sprintf("%s LIMIT %d, %d", $query_comments, $startRow_comments, $maxRows_comments);
$comments = mysql_query($query_limit_comments, $broker) or die(mysql_error());
$row_comments = mysql_fetch_assoc($comments);

if (isset($_GET['totalRows_comments'])) {
$totalRows_comments = $_GET['totalRows_comments'];
} else {
$all_comments = mysql_query($query_comments);
$totalRows_comments = mysql_num_rows($all_comments);
}
$totalPages_comments = ceil($totalRows_comments/$maxRows_comments)-1;

$queryString_brokers = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_brokers") == false &&
stristr($param, "totalRows_brokers") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_brokers = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_brokers = sprintf("&totalRows_brokers=%d%s", $totalRows_brokers, $queryString_brokers);
?><!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=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<p>Broker Profile</p>
<table width="100%" border="0">
<tr>
<td colspan="2"><?php echo $row_brokers['info']; ?></td>
</tr>
<tr>
<td width="75%">
<table border="0" width="50%" align="left">
<tr>
<td width="23%" align="center"><?php if ($pageNum_brokers > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_brokers=%d%s", $currentPage, 0, $queryString_brokers); ?>">First</a>
<?php } // Show if not first page ?>
</td>
<td width="31%" align="center"><?php if ($pageNum_brokers > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_brokers=%d%s", $currentPage, max(0, $pageNum_brokers - 1), $queryString_brokers); ?>">Previous</a>
<?php } // Show if not first page ?>
</td>
<td width="23%" align="center"><?php if ($pageNum_brokers < $totalPages_brokers) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_brokers=%d%s", $currentPage, min($totalPages_brokers, $pageNum_brokers + 1), $queryString_brokers); ?>">Next</a>
<?php } // Show if not last page ?>
</td>
<td width="23%" align="center"><?php if ($pageNum_brokers < $totalPages_brokers) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_brokers=%d%s", $currentPage, $totalPages_brokers, $queryString_brokers); ?>">Last</a>
<?php } // Show if not last page ?>
</td>
</tr>
</table></td>
<td width="25%"> </td>
</tr>
</table>
<p>Comments</p>
<?php do { ?>
<table width="100%" border="0">
<tr>
<td colspan="2"><?php echo $row_comments['comment']; ?></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<?php } while ($row_comments = mysql_fetch_assoc($comments)); ?><p>Add a comment:</p>
<p> </p>

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Comment:</td>
<td><textarea name="comment" cols="32"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insert record"></td>
</tr>
</table>
<input name="broker_id" type="hidden" value="<?php echo $row_brokers['broker_id']; ?>">
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</html>
<?php
mysql_free_result($brokers);

mysql_free_result($comments);
?>



Thanks for any help!!!!
xtian
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.