Reply
Don't know how to fix my error $brain = 'fried';
Old 04-23-2007, 01:02 AM Don't know how to fix my error $brain = 'fried';
NullPointer's Avatar
Will Code for Food

Posts: 498
Name: Matt
Location: Irvine, CA
Can someone help me out with my code. Its a bit of a mess.

I've been writing a program to sort elements in an SQL table by first dividing them by a certain key, then cutting the table in half, then sorting by a different key, dividing by half again, sorting again.. etc. There are four different keys. I'm a bit new to PHP, but I know how to do this in java and C++. Pretty much I've been trying to use associative, two deminsional arrays but its not working out to well. I get this error:

Parse error: syntax error, unexpected T_DOUBLE_ARROW on line 113

I've pointed out the place where I get the error. There is much more to the file than I'm going to post (because its a huge file and the rest works for sure). If you see any values you don't see assigned, assume they are valid.

Code:
//Establish link to database
$link = mysql_connect($dbhost, $dbusername, $dbpass) 
	or die("Failed to connect to database.");
print "Connected to database<br>";
//Select the database
$selected = mysql_select_db($dbname, $link) 
	or die("Could not select the database.");

//Get number of rows
$count = mysql_query('SELECT COUNT(host_id) FROM rate_hosts');
//Prepares to sort and divide by 2
$desired = (int) $count/2;
$hosts[$desired - 1];

$results = mysql_query("SELECT * FROM rate_hosts ORDER BY '$first' DECS;");
print 'working';
$i = 0;
//start sorting
while($row = mysql_fetch_array( $result ) and $i < $desired) 
{
	$hosts[$i] = $row[$second] => $row;  //I GET THE ERROR HERE
	$i++;
}

$desired = (int) $desired/2;
if($desired > 1)
{
	krsort($hosts);
	$index = 0;
	foreach ($hosts as $key => $val) 
	{
    	$hosts[$index] = $val;
		$index++;
	}
	$temp[$desired];
	for($j = 0 ; $j < $desired; $j++)
	{
		$temp[$j] = $hosts[$j][$third] => $hosts[$j];
	}
	$hosts = NULL;
	$hosts = $temp;
	$desired = (int) $desired/2;
	if($desired > 1)
	{
		print '.';
		krsort($hosts);
		$index2 = 0;
		foreach($hosts as $key => $val)
		{
			$hosts[$index2] = $val;
			$index++;
		}
		$temp2[$desired];
		for($p = 0 $p < $desired; $p++)
		{
			$temp[$p] = $hosts[$p][$fourth] => $hosts[$p];
		}
		$hosts = NULL;
		$hosts = $temp;
		$desired = (int) $desired/2;
		if($desired > 1)
		{
			print '.';
			krsort($hosts);
			$index3 = 0;
			foreach($hosts as $key => $val)
			{
				$hosts[$index3] = $val;
				$index++;
			}
		}
	}
}
print '.<br>';
$final[3];
for($f = 0; $f < 3; $f++)
{
	if($hosts[$f] != NULL)
	{
		$final[$f] = $hosts[$f];
	}
}
for($counter = 0; $counter < 3; $counter++)
{
	print $final[$counter][1];
}
//open connections are bad.
mysql_close($link);
print "<br>Connection to database closed.";
__________________
http://tinsology.com/ - Under construction
NullPointer is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-23-2007, 01:29 AM Re: Don't know how to fix my error $brain = 'fried';
mtishetsky's Avatar
King Spam Talker

Posts: 1,054
Name: Mike
Location: Mataro, Spain
$hosts[$i] = $row[$second] => $row; //I GET THE ERROR HERE
Please describe what do you expect this construction to do. I don't understand it and I don't consider it a legal php construction.
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 04-23-2007, 02:29 AM Re: Don't know how to fix my error $brain = 'fried';
NullPointer's Avatar
Will Code for Food

Posts: 498
Name: Matt
Location: Irvine, CA
I was trying to contruct the array as key value pairs where the key is the value at index $second (which I defined earlier in the script, and is not the second element of the array btw). When I coded it I was looking at the docs for krsort():

Code:
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
I'll probably just end up writing my own sorting method that well let me specify the key in a more straight forward way.
__________________
http://tinsology.com/ - Under construction
NullPointer is offline
Reply With Quote
View Public Profile
 
Old 04-23-2007, 02:34 AM Re: Don't know how to fix my error $brain = 'fried';
mtishetsky's Avatar
King Spam Talker

Posts: 1,054
Name: Mike
Location: Mataro, Spain
usort()
Manuals are written to be read, not just to consume disk space.
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 04-23-2007, 02:49 AM Re: Don't know how to fix my error $brain = 'fried';
NullPointer's Avatar
Will Code for Food

Posts: 498
Name: Matt
Location: Irvine, CA
Good point. Lets close down the forum and insert a link to cryptic documentation. I figured it out btw (didn't use usort()).
__________________
http://tinsology.com/ - Under construction
NullPointer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Don't know how to fix my error $brain = 'fried';
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14626 seconds with 12 queries