Jump to content

Sorting and filtering this script by form


SanderK

Recommended Posts

Hello Everybody!

 

From all the PHP forums, this seems to be the best one, since I find the words 'freaks' and 'addicted' everywhere, so I give it a shot for the first time ;)

 

Yesterday I've been struggling with a piece of code all day, which got me really frustrated. I'm a bit of a noob yet, but usually Iget quite far with a bit of googling. Not this time...

 

I have a code and I want to be able to sort and filter the code with a form (It's for a portfolio). I know it has a lot to do with XXXX, but I can't get it working. Do I have to use a double array instead? (tried that also, but then I have to find a way to get the paging back...)

 

Can someone help me out a bit maybe? I would be very very graceful!

 

Here is the code I'm using:

 

The index.php

<?php
$page_comments = 2; //number of comments per page

/*  DEZE HIERONDER KAN MISSCHIEN WEG??  */
$page = 0;
$page = (int)$_GET['p']; 
?>

<!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>
<title>Sort/filter form</title>


<style type="text/css">
<!--
div.gbsign{color:#F00;}
.gbname{font-weight:bold;color:#FC0;}
.gbdate{margin-left:30px;color:#F0C;}
p.gbmessage{margin-top:-12px;color:#FCC;}
p.extra_p{margin-top:-12px;color:#0F0;}
p.extra2_p{margin-top:-12px;color:#00F;}
-->
</style>

</head>
<body>



<form method=get style="display: inline;" name=''>
<select name=orderby onChange="">
<option value='relevance'>Relevance</option>
<option value='year'>Year</option>
<option value='title_asc'>Title (A - Z)</option>
<option value='title_desc'>Title (Z - A)</option><BR />
</select>
<input type="checkbox">Film</input>
<input type="checkbox">TV</input>
<input type="checkbox">Fiction</input>
<input type="checkbox">Documentary / Reality</input>
<input type="checkbox">Commercial</input>
<input type="checkbox">In Opdracht</input>
</form>




<?php 
$gbfile = 'gbcontentfile.php';
$fh = @fopen($gbfile, "r");
$fcontent = @fread($fh, filesize($gbfile));
if($fcontent){
$cnt = substr_count($fcontent,'<?php /* ');
$cnt = $cnt/2;
$maxp = 0;
if($cnt>$page_comments)$maxp = (int)($cnt/$page_comments);
preg_match_all("/\<\?php .*? \?\>(.*?)\<\?php .*? \?\>/is", $fcontent, $entries);
$ini = $page*$page_comments;
$end = ($page+1)*$page_comments;
$ovo = array('<1>','<2>','<3>','<4>','<5>','<6>','<7>');

$sovim = array(
"<div class=\"gbsign\">
<p><span class=\"gbname\">",
"</span> <span class=\"gbdate\">",
"</span>
</p>
<p class=\"gbmessage\">",
"</p>
<p class=\"extra_p\">",
"</p>
<p class=\"extra2_p\">",

"</p>
<p class=\"extra2_p\">",

"</p>
<p class=\"extra2_p\">",
"
</p>
</div>
"
);
for($j=$ini;$j<$end;$j++)echo str_replace($ovo,$sovim,$entries[1][$j]);
if($maxp>-1){
	echo '<p>'.strstr($fcontent,'<!--').' Pagina '; $gap = "";
	for($j=0;$j<$maxp+1;$j++){
		if($j==0||$j==$maxp||($j-$page)*($j-$page)<26){
			echo $gap; $gap = "";
			if($j!=$page)echo "- <a href=\"?p=".$j.$si."\">".($j+1)."</a> " ; /*  HIER KOMT NORMAALGESPROKEN (VOOR HET VRAAGTEKEN) DE BESTANDSNAAM VAN DIT DOCUMENT (nu niet ivm index) */
			else echo "- <b>".($j+1)."</b> " ;
		}
		else $gap = "<b>.....</b>"; 
	}
	echo '</p>';
}
}

?>



</body>
</html>

 

This is the 'database' gbcontentfile.php with the info:

<?php /* ARTICLE START */ ?><1>TV<2>Fiction<3>ATitle1<4>1<5>Small Example text<6>THE text<7>extra option<?php /* ARTICLE END */ ?>

<?php /* ARTICLE START */ ?><1>Film<2>Fiction<3>BTitle2<4>3<5>Small Example text<6>THE text<7>extra option<?php /* ARTICLE END */ ?>

<?php /* ARTICLE START */ ?><1>TV<2>Reality<3>CTitle3<4>2<5>Small Example text<6>THE text<7>extra option<?php /* ARTICLE END */ ?>

 

I hope that someone can help me out. I found this already, but I can not get the two combined:

<?PHP
  $data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
    array("firstname" => "James", "lastname" => "Brown", "age" => 31),
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
  );
  ?>

<?PHP

  function orderBy($data, $field)
  {
    $code = "return strnatcmp(\$a['$field'], \$b['$field']);";
    usort($data, create_function('$a,$b', $code));
    return $data;
  }

  $data = orderBy($data, 'age');
  
?>

 

Thanks in advance!!

 

Sander

Link to comment
Share on other sites

...And that's why God created databases.

 

What you are looking to do really should have a RDBS backend.  It's all good and well running that through with a half dozen lines of data, but when you start scaling it up performing sort and reindexing operations on a multidimensional array takes up way more resource than is practical.  Not to mention that using a RDBS like MySQL, Oracle or MS SQL Server you would find the task its self significantly easier.

 

I know it's not the answer you're looking for and I'm sorry about that, but I have never atempted what you are trying to perform simply because, in my opinion, its just not the right way to do things.

Link to comment
Share on other sites

Hi Muddy Funster,

 

Thanks for the reply! Actually... You are right! ;)

 

But of course there is a reason I didn't use MySQL for this: I've never used it before. Gets about time then!

 

I spend the evening researching, and I managed to get a working database which I can put in a table.

 

But this is where I need a little more help:

Now I have the database (printing in a table) and the form, but now I need to combine the two:

 

Php calling and printing database to table:

<?php

$host="****"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("DB bestaat niet");

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width=""><? echo $rows['id_nr']; ?></td>
<td width=""><? echo $rows['titel']; ?></td>
<td width=""><? echo $rows['jaar']; ?></td>
<td width=""><? echo $rows['genre']; ?></td>
<td width=""><? echo $rows['korte_omsch']; ?></td>
</tr>
</table>

<?
// close while loop 
}

// close connection 
mysql_close();
?>

 

And the form:

<form method=get style="display: inline;" name=''>
<select name=orderby onChange="">
<option value='id_nr'>ID#</option>
<option value='year'>Year</option>
<option value='title_asc'>Title (A - Z)</option>
<option value='title_desc'>Title (Z - A)</option><BR />
</select>
<input type="checkbox">Film</input>
<input type="checkbox">TV</input>
<input type="checkbox">Fiction</input>
<input type="checkbox">Documentary / Reality</input>
<input type="checkbox">Commercial</input>
<input type="checkbox">In Opdracht</input>
</form>

 

In these, the checker boxes refer to the data 'film', 'tv', 'fiction' etc. which are '1' (true) or '0' (false). If a checkbox is selected, only the true rows in the category should be printed.

The sorting should be done by the column that is selected in the drop down menu.

 

Any suggestions (and/or articles that could help me) to make BOTH of these (sorting and filtering) work at the same time?

 

Thanks a lot already!

 

Sander

Link to comment
Share on other sites

Ah! Found a part for it already:

 

 $sql="SELECT * FROM $tbl_name ORDER BY column_name";

 

I guess with a little javascript I can influence the 'column_name' part, and toggle it id, name, title, etc.

 

Only difficulty is how to reload the data after the form has been changed. Does it change automatically?

I'll work further on it tomorrow evening!

 

Still have to find a way to filter it. And also to page it to 10 entries per page max. I hope I can combine the three... :s

Link to comment
Share on other sites

Wow, there is a lot info about MySQL! I already found the filter part (both filter and order now):

 

$sql="SELECT * FROM $tbl_name WHERE column_name1='1' ORDER BY column_name2";

 

Now I just need a way to change the two column names by the table, see my previous post. And the paging.

 

Any suggestions on that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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