PDA

View Full Version : recordsets to display in random order



Perspective
12-30-2007, 10:34 PM
Hello,

I am trying to figure out how to have my recordsets in Dreamweaver CS3 (created with Mysql and php) display in a random order each time the page is requested. Basically I do not want the same companies information to show up first each time. Below is a portion of the code I am using. i have tried ORDER BY RAND () and some other things but continue to get an error each time.

Is there someone that can show me what to do and where to place it?

Thank you so much!

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

$maxRows_rsvendors = 10;
$pageNum_rsvendors = 0;
if (isset($_GET['pageNum_rsvendors'])) {
$pageNum_rsvendors = $_GET['pageNum_rsvendors'];
}
$startRow_rsvendors = $pageNum_rsvendors * $maxRows_rsvendors;

$varzip_rsvendors = "1";
if (isset($_POST['Zip'])) {
$varzip_rsvendors = $_POST['Zip'];
}
$varcategory_rsvendors = "1";
if (isset($_POST['Category'])) {
$varcategory_rsvendors = $_POST['Category'];
}
mysql_select_db($database_connect, $connect);
$query_rsvendors = sprintf("SELECT commercial.Company_Name, commercial.Street, commercial.City, commercial.`State`, commercial.Zip, commercial.Phone, commercial.Website, commercial.Email, commercial.Category, commercial.Photo, commercial.About, commercial.Contact, commercial.General_liability_insurance, commercial.GLI_amount, commercial.Workers_comp, commercial.WC_amount, commercial.Accept_credit_cards, commercial.Years_in_business FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s ", GetSQLValueString($varzip_rsvendors, "text"),GetSQLValueString($varcategory_rsvendors, "text"));
$query_limit_rsvendors = sprintf("%s LIMIT %d, %d", $query_rsvendors, $startRow_rsvendors, $maxRows_rsvendors);
$rsvendors = mysql_query($query_limit_rsvendors, $connect) or die(mysql_error());
$row_rsvendors = mysql_fetch_assoc($rsvendors);

if (isset($_GET['totalRows_rsvendors'])) {
$totalRows_rsvendors = $_GET['totalRows_rsvendors'];
} else {
$all_rsvendors = mysql_query($query_rsvendors);
$totalRows_rsvendors = mysql_num_rows($all_rsvendors);
}
$totalPages_rsvendors = ceil($totalRows_rsvendors/$maxRows_rsvendors)-1;
?><!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>
<link href="vol.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>

Leafy
01-02-2008, 09:06 AM
Dear god that query string is ugly!
Yours looks like this:


SELECT commercial.Company_Name, commercial.Street, commercial.City, commercial.`State`, commercial.Zip, commercial.Phone, commercial.Website, commercial.Email, commercial.Category, commercial.Photo, commercial.About, commercial.Contact, commercial.General_liability_insurance, commercial.GLI_amount, commercial.Workers_comp, commercial.WC_amount, commercial.Accept_credit_cards, commercial.Years_in_business FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s "

When it could look like this:


SELECT * FROM commercial WHERE ((`Zip`='%s')AND(`Category`='%s'))


Please change it.

You can randomize arrays using shuffle($array). Read up more about PHP functions and data types by using PHP.net's built-in documentation by going to http://www.php.net/FUNCTION.

BLiZZaRD
01-02-2008, 10:25 AM
Dear God you are a wee bit pushy.

Please change that.

Perspective
01-04-2008, 02:40 AM
Thanks for the tips Leafy, I did get it figured out.

Did not intend to be pushy Blizzard, just asking for help and am new at this.

BLiZZaRD
01-04-2008, 03:36 AM
Did not intend to be pushy Blizzard, just asking for help and am new at this.

That wasn't aimed at you ;)