Page 1 of 1

help with error Warning: ksort() expects parameter 1 to be

Posted: Mon Nov 07, 2005 5:48 am
by paulng
I was wondering if anyone could help, a while ago I posted a question about ranking fields by asceding order as well as displaying them by random and someone provided me with the code below which worked well :

Code: Select all

$sql_query = "SELECT * FROM $tablevar WHERE FIND_IN_SET('download',DisplayOn) AND StartDate <= '$currdate' AND EndDate >= '$currdate' ORDER BY RAND()"; 
$rs_data = mysql_query($sql_query) or die ("could not execute query: " . mysql_error()); 

while($data = mysql_fetch_assoc($rs_data)) 
{    

$arr_data[$data['DisplayOrder']] = $data; 
} 

ksort($arr_data); 

foreach($arr_data as $item)

until I changed my table structure by adding a new field into my table called "displayOn" this field is causing an error on the page if there is not value in the field. Here is the error
Warning: ksort() expects parameter 1 to be array, null given in /usr/local/apache.....
Warning: Invalid argument supplied for foreach() .....


can anyone plaese help? much appreciated!

paulng.

Posted: Mon Nov 07, 2005 5:57 am
by Chris Corbyn

Code: Select all

$arr_data = array(); //Decl;are the array before you try and stack onto it

while($data = mysql_fetch_assoc($rs_data))
{   

$arr_data[$data['DisplayOrder']] = $data;
}

Posted: Mon Nov 07, 2005 6:29 am
by paulng
I have done that already, when I declare the array the error message is not showing at the same time it's not displaying data from the db. the problem is with one particular field that has two set value without a default value. so unless one of them is selected otherwise the page does not display any data.

table structure:

Code: Select all

CREATE TABLE `table_name` (
  `Id` int(11) NOT NULL auto_increment,
  `DisplayOn` set('download','oneClick') NOT NULL default '',
  `Title` varchar(50) NOT NULL default '',
  `Image` varchar(50) NOT NULL default '',
  `Description` text NOT NULL,
  `DisplayOrder` enum('1','2','3','4','5','6','7','8','9','10') NOT NULL default '1',
  `StartDate` datetime default '0000-00-00 00:00:00',
  `EndDate` datetime default '0000-00-00 00:00:00',
  UNIQUE KEY `id` (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
the field that was added which is creating problems is the DisplayOn

Code: Select all

$arr_data = array();

//sql statement for selecting the entire mod_en table
$sql_query = "SELECT * FROM $tablename WHERE FIND_IN_SET('download',DisplayOn) AND StartDate <= '$currdate' AND EndDate >= '$currdate' ORDER BY RAND()";
$rs_data = mysql_query($sql_query) or die ("could not execute query: " . mysql_error());

while($data = mysql_fetch_assoc($rs_data))
{   
$arr_data[$data['DisplayOrder']] = $data;
}
ksort($arr_data);
foreach($arr_data as $item)
Thanks in advance for your valuable help!

Posted: Mon Nov 07, 2005 6:51 am
by Chris Corbyn
try echoing something in your while loop.... looks like the query results are empty.

Posted: Mon Nov 07, 2005 7:22 am
by paulng
Thanks! I've manage to get it works it was my query

$sql_query = "SELECT * FROM $table WHERE StartDate <= '$currdate' AND EndDate >= '$currdate' ORDER BY RAND()";

instead! :)

Cheers!

Posted: Mon Nov 07, 2005 7:30 am
by yum-jelly
if a SET column type is empty (has no value) it will always return * 0 * not '' or NULL unless (STR,LIST) have a NULL value in which case it would return NULL. I think that is the reason why your result resource loop building for the ksort function does not work!

yj