Help Needed Urgent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
khanuja.sunpreet
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 9:04 pm

Help Needed Urgent

Post by khanuja.sunpreet »

I need help. :banghead: I am trying to sort records in PHP using array_multisort. The sort as such works just fine. I am trying to implement a table column header sort where clicking on the ID header would sort the records by ID and clicking on Name would sort the records by name. I am currently using session variables to maintain a numeric flag for sorting 1-for sorting on ID and 2-for sorting on Name.

I am initializing the session variable on the page before I want to perform sorting, and I am using the onclick event attribute to set the value of the session variable. The session variable is misbehaving and is not working as intended. Upon refreshing the page, or clicking either the ID link or Name link, the session variable is getting set to value 2, whereas ideally if I click on ID link, it should set to value "1" and sort by ID and not name.

Here is the code snippet that I am using:
Use of session variable:

if (($_SESSION['sortby'] <> 2))
{ array_multisort($array_nid, $array_name, $array_id, $enable);}
elseif ($_SESSION['sortby'] == 2)
{ array_multisort($array_name, $array_nid, $array_id, $enable);}


Definition of session variable:
<tr>
<td><a href="home.php" onclick="<?php $_SESSION['sortby'] = 1;?>">ID[/url]<img src="images/sort_arrow.jpg" width="10px" height="10px"></td>
<td valign="top"><a href="home.php" onclick="<?php $_SESSION['sortby'] = 2;?>">Nugget Name[/url]<img src="images/sort_arrow.jpg" width="10px" height="10px"></td></tr>


Note, the table is for formatting purposes only. Does the onclick even attribute doesn't support this kind of functionality?
Kindly help, urgently.
Thanks a lot.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Help Needed Urgent

Post by kryles »

odd, I always assumed onClick was for javascript exclusively.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Help Needed Urgent

Post by liljester »

You cannot use onClick to set the value of a session variable. PHP is processed on the server, not in the browser so you have no real access to the php variables in the browser. (unless you want to use ajax) I would suggest making it so that when you click on the column the page would submit via $_GET and you would be able to reorder your results and send the new list to the client.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Help Needed Urgent

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
khanuja.sunpreet
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 9:04 pm

Re: Help Needed Urgent

Post by khanuja.sunpreet »

thanks a ton everyone...

and my bad...i'm new to the forum scene.
thanks again. :D
Post Reply