Jump to content

PHP Guestbook sorting


alen

Recommended Posts

I made this simple guestbook script which stores everything in a file. I would have used a sql database, but I don't have one. Anyway .. I need to sort the entries. The newest entry comes first.

 

You can find my script at;

 

http://anigma.sitees.com/Guestbook/index.phps

http://anigma.sitees.com/Guestbook/lagre.phps (lagre=save)

 

and you can test my script here;

 

http://anigma.sitees.com/Gjestebok/index.php

 

Any idea on what I can do? I know how to sort it with MySQL, but I think it's not quite the same.

Link to comment
https://forums.phpfreaks.com/topic/39326-php-guestbook-sorting/
Share on other sites

I posted this yesterday, may be of use to you.

 

<?php
function setSortVar(&$item, $key, $sortBy)
	{
	$item[0]=$item[$sortBy];
	}

function myArraySort($x, $y)
	{
	if ($x[0] == $y[0])
		{
		return 0;
		}
	else if ($x[0] < $y[0])
		{
		return -1;
		}
	return 1;
	}

$myCollection = array();
$myCollection[0][0]=""; // Sorting Value
$myCollection[0][1]="Die Die Die"; // Name
$myCollection[0][2]="Movie"; // Main Category
$myCollection[0][3]="DVD"; // Sub Category
$myCollection[0][4]="Drama"; // Type of movie

$myCollection[1][0]=""; // Sorting Value
$myCollection[1][1]="Worm Race"; // Name
$myCollection[1][2]="Movie"; // Main Category
$myCollection[1][3]="DVD"; // Sub Category
$myCollection[1][4]="Action"; // Type of movie

$myCollection[2][0]=""; // Sorting Value
$myCollection[2][1]="Ha ha."; // Name
$myCollection[2][2]="Movie"; // Main Category
$myCollection[2][3]="HDDVD"; // Sub Category
$myCollection[2][4]="Comedy"; // Type of movie

$sortBy=1;

//Look for sort vars
if (isset($_GET['sortBy']))
	{
	switch ($_GET['sortBy'])
		{
		case 1:
		$sortBy=1;
		break;
		case 2:
		$sortBy=2;
		break;
		case 3:
		$sortBy=3;
		break;
		case 4:
		$sortBy=4;
		break;
		default:
		$sortBy=1;
		break;
		}
	}

array_walk($myCollection, 'setSortVar', $sortBy);
usort($myCollection, 'myArraySort');
print_r($myCollection);
?>

Link to comment
https://forums.phpfreaks.com/topic/39326-php-guestbook-sorting/#findComment-189982
Share on other sites

1- use file("filepath"); function to read all the entries line by line in an array.

2- now you can loop through all the elements of the array and parse each array element.

3- find out date in the array element.

4- craeate another array and put date as key of the array element and rest of the data as value.

5- use ksort(), krsort() array functions to sort the array.

 

Hope this will help

Link to comment
https://forums.phpfreaks.com/topic/39326-php-guestbook-sorting/#findComment-190078
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.