Page 1 of 1

no duplicate codes

Posted: Thu Dec 04, 2003 12:52 pm
by grudz
Hello,

is there a way i can not have duplicate recordes shown on a page? I can't do the unique key, because i need them to be duplicate for another reason, but i dont want to show them (the duplicates)


thank you in advance

Posted: Thu Dec 04, 2003 1:04 pm
by Chambrln
I blieve you can use "SELECT DISTINCT" in your query

Here is a page: http://www.mysql.com/doc/en/DISTINCT_optimisation.html

Example

Code: Select all

SELECT DISTINCT * FROM tablename

Posted: Thu Dec 04, 2003 1:15 pm
by grudz
i dont get the t1.a in the link u sent me

Posted: Thu Dec 04, 2003 2:16 pm
by mrvanjohnson
t1.a would be table named "t1" column named "a". Look up JOIN for a better understanding

Posted: Thu Dec 04, 2003 2:32 pm
by grudz
im sorry, but i tried that and it didnt work (or better yet, im not writing it correctly)

table name= forum
column = question_title

now thats what i don't want to duplicate, when question_title is repeated

here's my code:

$query_forum = "SELECT * FROM forum ORDER BY `date` DESC";

can you please help me out........im very new at php coding

Posted: Thu Dec 04, 2003 2:46 pm
by Chambrln
try $query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC";

And if that still doesn't work try pasting your code in here so we can look at it all together.

Posted: Thu Dec 04, 2003 2:51 pm
by grudz
it doesnt........i think this is the part of the code that'll help (or u might want everything ?)

Code: Select all

mysql_select_db($database_cnpa, $cnpa);
$query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC"; 
$query_limit_forum = sprintf("%s LIMIT %d, %d", $query_forum, $startRow_forum, $maxRows_forum);
$forum = mysql_query($query_limit_forum, $cnpa) or die(mysql_error());
$row_forum = mysql_fetch_assoc($forum);

Posted: Thu Dec 04, 2003 2:52 pm
by Chambrln
I gotta run and I'll look at it more in depth when I get back but first thing the * should have a space after DISTINCT

Posted: Thu Dec 04, 2003 2:53 pm
by grudz
k.....

Posted: Thu Dec 04, 2003 2:53 pm
by dull1554
i see no reason that that won't work, can you give us all of your code?

Posted: Thu Dec 04, 2003 2:55 pm
by grudz
here it is

Code: Select all

// *** Logout the current user.
$FF_Logout = $HTTP_SERVER_VARSї'PHP_SELF'] . "?FF_Logoutnow=1";
if (isset($HTTP_GET_VARSї'FF_Logoutnow']) && $HTTP_GET_VARSї'FF_Logoutnow']=="1") {
  session_start();
  session_unregister("MM_Username");
  session_unregister("MM_UserAuthorization");
  $FF_logoutRedirectPage = "content_privee.php";
  // redirect with URL parameters (remove the "FF_Logoutnow" query param).
  if ($FF_logoutRedirectPage == "") $FF_logoutRedirectPage = $HTTP_SERVER_VARSї'PHP_SELF'];
  if (!strpos($FF_logoutRedirectPage, "?") && $HTTP_SERVER_VARSї'QUERY_STRING'] != "") {
    $FF_newQS = "?";
    reset ($HTTP_GET_VARS);
    while (list ($key, $val) = each ($HTTP_GET_VARS)) {
      if($key != "FF_Logoutnow"){
        if (strlen($FF_newQS) > 1) $FF_newQS .= "&";
        $FF_newQS .= $key . "=" . urlencode($val);
      }
    }
    if (strlen($FF_newQS) > 1) $FF_logoutRedirectPage .= $FF_newQS;
  }
  header("Location: $FF_logoutRedirectPage");
  exit;
}

?>
<?php require_once('Connections/cnpa.php'); ?>
<?php
$currentPage = $HTTP_SERVER_VARS&#1111;"PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
&#123;
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) &#123;
    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;
  &#125;
  return $theValue;
&#125;

$editFormAction = $HTTP_SERVER_VARS&#1111;'PHP_SELF'];
if (isset($HTTP_SERVER_VARS&#1111;'QUERY_STRING'])) &#123;
  $editFormAction .= "?" . $HTTP_SERVER_VARS&#1111;'QUERY_STRING'];
&#125;

if ((isset($HTTP_POST_VARS&#1111;"MM_insert"])) && ($HTTP_POST_VARS&#1111;"MM_insert"] == "form1")) &#123;
  $insertSQL = sprintf("INSERT INTO forum (name, `date`, question_title, question) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS&#1111;'name'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'date'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'question_title'], "text"),
                       GetSQLValueString($HTTP_POST_VARS&#1111;'question'], "text"));

  mysql_select_db($database_cnpa, $cnpa);
  $Result1 = mysql_query($insertSQL, $cnpa) or die(mysql_error());

  $insertGoTo = "content_forum.php";
  if (isset($HTTP_SERVER_VARS&#1111;'QUERY_STRING'])) &#123;
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS&#1111;'QUERY_STRING'];
  &#125;
  header(sprintf("Location: %s", $insertGoTo));
&#125;

    $MM_Username=$FF_valUsername;
    session_register("MM_Username");

$maxRows_forum = 5;
$pageNum_forum = 0;
if (isset($HTTP_GET_VARS&#1111;'pageNum_forum'])) &#123;
  $pageNum_forum = $HTTP_GET_VARS&#1111;'pageNum_forum'];
&#125;
$startRow_forum = $pageNum_forum * $maxRows_forum;

mysql_select_db($database_cnpa, $cnpa);
$query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC"; 
$query_limit_forum = sprintf("%s LIMIT %d, %d", $query_forum, $startRow_forum, $maxRows_forum);
$forum = mysql_query($query_limit_forum, $cnpa) or die(mysql_error());
$row_forum = mysql_fetch_assoc($forum);

if (isset($HTTP_GET_VARS&#1111;'totalRows_forum'])) &#123;
  $totalRows_forum = $HTTP_GET_VARS&#1111;'totalRows_forum'];
&#125; else &#123;
  $all_forum = mysql_query($query_forum);
  $totalRows_forum = mysql_num_rows($all_forum);
&#125;
$totalPages_forum = ceil($totalRows_forum/$maxRows_forum)-1;

mysql_select_db($database_cnpa, $cnpa);
$query_privee = "SELECT * FROM privee WHERE name="" . $HTTP_SESSION_VARS&#1111;'MM_Username'] . """;
$privee = mysql_query($query_privee, $cnpa) or die(mysql_error());
$row_privee = mysql_fetch_assoc($privee);
$totalRows_privee = mysql_num_rows($privee);

$maxRows_forum = 5;
$pageNum_forum = 0;
if (isset($HTTP_GET_VARS&#1111;'pageNum_forum'])) &#123;
  $pageNum_forum = $HTTP_GET_VARS&#1111;'pageNum_forum'];
&#125;
$startRow_forum = $pageNum_forum * $maxRows_forum;

mysql_select_db($database_cnpa, $cnpa);
$query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC"; 
$query_limit_forum = sprintf("%s LIMIT %d, %d", $query_forum, $startRow_forum, $maxRows_forum);
$forum = mysql_query($query_limit_forum, $cnpa) or die(mysql_error());
$row_forum = mysql_fetch_assoc($forum);

if (isset($HTTP_GET_VARS&#1111;'totalRows_forum'])) &#123;
  $totalRows_forum = $HTTP_GET_VARS&#1111;'totalRows_forum'];
&#125; else &#123;
  $all_forum = mysql_query($query_forum);
  $totalRows_forum = mysql_num_rows($all_forum);
&#125;
$totalPages_forum = ceil($totalRows_forum/$maxRows_forum)-1;

$queryString_forum = "";
if (!empty($HTTP_SERVER_VARS&#1111;'QUERY_STRING'])) &#123;
  $params = explode("&", $HTTP_SERVER_VARS&#1111;'QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) &#123;
    if (stristr($param, "pageNum_forum") == false && 
        stristr($param, "totalRows_forum") == false) &#123;
      array_push($newParams, $param);
    &#125;
  &#125;
  if (count($newParams) != 0) &#123;
    $queryString_forum = "&" . implode("&", $newParams);
  &#125;
&#125;
$queryString_forum = sprintf("&totalRows_forum=%d%s", $totalRows_forum, $queryString_forum);

$MM_paramName = ""; 

// *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters
// create the list of parameters which should not be maintained
$MM_removeList = "&index=";
if ($MM_paramName != "") $MM_removeList .= "&".strtolower($MM_paramName)."=";
$MM_keepURL="";
$MM_keepForm="";
$MM_keepBoth="";
$MM_keepNone="";
// add the URL parameters to the MM_keepURL string
reset ($HTTP_GET_VARS);
while (list ($key, $val) = each ($HTTP_GET_VARS)) &#123;
	$nextItem = "&".strtolower($key)."=";
	if (!stristr($MM_removeList, $nextItem)) &#123;
		$MM_keepURL .= "&".$key."=".urlencode($val);
	&#125;
&#125;
// add the URL parameters to the MM_keepURL string
if(isset($HTTP_POST_VARS))&#123;
	reset ($HTTP_POST_VARS);
	while (list ($key, $val) = each ($HTTP_POST_VARS)) &#123;
		$nextItem = "&".strtolower($key)."=";
		if (!stristr($MM_removeList, $nextItem)) &#123;
			$MM_keepForm .= "&".$key."=".urlencode($val);
		&#125;
	&#125;
&#125;
// create the Form + URL string and remove the intial '&' from each of the strings
$MM_keepBoth = $MM_keepURL."&".$MM_keepForm;
if (strlen($MM_keepBoth) > 0) $MM_keepBoth = substr($MM_keepBoth, 1);
if (strlen($MM_keepURL) > 0)  $MM_keepURL = substr($MM_keepURL, 1);
if (strlen($MM_keepForm) > 0) $MM_keepForm = substr($MM_keepForm, 1);
?>

Posted: Thu Dec 04, 2003 3:06 pm
by grudz
anything ???? cuz this is really, really important that i have it done right

Posted: Thu Dec 04, 2003 3:36 pm
by grudz
i want to make sure you understand that i dont want to show the duplicates that are in the same column......

Posted: Thu Dec 04, 2003 4:59 pm
by microthick
Create an array.

Before you output the row, see if the id is in that row. If it is, then don't output. Otherwise, output.

Then, if that id wasn't in the array, add it to the array.