Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2012, 06:09 PM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Help writing a link

Hello,

I need some help writing a link in php.

I am trying to write table headers as links in php but can't seem to get it to work out.

Here is my code:
PHP Code:
echo "<table id='members_table'>";
  echo 
"<tr>";
  echo 
"<th>Members No.</th>";
  echo 
"<th><a href='#' onmouseup='showUser(ASC, members_no)'>Username</a></th>";
  echo 
"<th>Join Date</th>"
The variables are not being passed correctly. My ajax function is not receiving any values.

Can anyone help me encode this url properly? I've enclosed the values in the onmouseup event in single and double quotes but it still isn't working. I also saved them values into variables and tried to pass those in but with no luck.

Thanks in advance.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 08:16 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 581
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
where are you getting ASC and members_no from?

if there not variable names you probably need to put them in quotes
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 09-17-2012, 08:50 PM   PM User | #3
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I hard code them into the links in the php script to go into the ajax function showUser(). It then passes the variables through the url of the specified action script, to be used to decide what column to sort by, and whether it be ascending or descending.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 09:11 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by LearningCoder View Post
PHP Code:
echo "<table id='members_table'>";
  echo 
"<tr>";
  echo 
"<th>Members No.</th>";
  echo 
"<th><a href='#' onmouseup='showUser(ASC, members_no)'>Username</a></th>";
  echo 
"<th>Join Date</th>"
The variables are not being passed correctly. My ajax function is not receiving any values.
What do you mean by "My ajax function is not receiving any values." ?

If you mean its not receiving the 5 lines you're echoing out, then no it won't. For an ajax reply it must be all on one single line so you'll need to str_replace any \n characters.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-17-2012, 11:02 PM   PM User | #5
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
This is my code up to now:
Code:
<div id="order_links_div">
	  <a href="#" name="DEFAULT" onmouseup="showUser('DEFAULT')">DEFAULT</a>
   </div>
   
   <div id='members_div'>
      <table id="header_table">
	  <tr>
	     <th>Members No.</th>
	     <th><a href="#" onmouseup="showUser('username')">Username</a></th>
	     <th><a href="#" onmouseup="showUser('join_date')">Join Date</th>
	  </tr>
	  </table>
	  <table id="results_table">
	  
	  </table>
   </div>
When either of those links within the table are clicked, my ajax function is called which takes the parameter value in, so it can send it to the sort.php script which will eventually order by column name.

I changed my php script to look like this:
PHP Code:
<?php
require("connectdb.php");

if(!empty(
$_GET['o']) && $_GET['o'] != 'DEFAULT'){

$col $_GET['o'];

$sql mysql_query("SELECT * FROM members ORDER BY {$col}");
   
   if(!
$sql){
     die(
"error with query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
else if(
$_GET['o'] == 'DEFAULT') {

$sql mysql_query("SELECT * FROM members");

   if(!
$sql){
      die(
"error with default query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
?>
It is nearly working as intended. If I click these links, they change to the correct order, but I'm having issues with changing it to order by descending if the column has already been clicked once.

So I want to click the username header anchor for example, then it orders the data by username, and as you know it is done ascending by default. I need to retain the state of the link so that if it is clicked again, it will order by descending. I tried using a static variable to retain the value but it doesn't seem to be doing so. Can I only use static variables within a function?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 11:33 PM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
You've two ways of doing it:

Store the asc / desc in your javascript so that the function can make use of it

Use sessions in php to do the same thing.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-18-2012, 10:35 AM   PM User | #7
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
So I could create 2 variables within the javascript function? for instance:

Code:
function showUser(col)
 {
 
 var asc = "ASC";
 var desc = "DESC";
 
 if (col=="")
   {
   document.getElementById("content").innerHTML=xmlhttp.responseText;
   } 
 if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
     {
     document.getElementById("results_table").innerHTML = xmlhttp.responseText;
     }
   }
 xmlhttp.open("GET","sort.php?col="+col+"&ord="+asc,true);
 xmlhttp.send();
 }
Then pass them into the request url like so:
Code:
xmlhttp.open("GET","sort.php?col="+col+"&ord="+asc,true);
Is that right? I'm still not sure how you could track which order the list is currently in?

Do static variables work the same way as they do in PHP?

Regards,

LC.

Last edited by LearningCoder; 09-18-2012 at 10:43 AM..
LearningCoder is offline   Reply With Quote
Old 09-18-2012, 12:36 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
LC I'm no JS expert but no not like that.

You'd want variables called the same as your column names and keep either asc or desc in them. When you make the next call from your function you then invert their values to hold the opposite - EG if its asc then it becomes desc.

This is a PHP forum and I'm not good with JS so I can't really give you any code.

In php something like this..

PHP Code:
$Column $_GET['col'];

//If its sent, use it otherwise default is asc
$Direction = (isset($_GET['ord']))? $_GET['ord']: 'asc'
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-18-2012, 03:07 PM   PM User | #9
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I still do not understand how the function retains the information...

In php, we use static variables inside functions which retain their information each time that it is called. But how do I implement that because I tried to set one like this:
Code:
var order = 'ASC';

function showUser(){

   if(order == 'ASC'){
      order = 'DESC';
   }
}
I only pass the column name to this function now with the onclick event of the anchor tag. I have created a global javascript variable so I thought this would work but it doesn't seem to be...

But if my memory serves me correctly once the function ends, the values are also lost. I would like to do this with php and not rely so much on the javascript. The more I can do with the php is better for me.

P.S - Apologies for the JS questions over in this section but I'm not sure if it's a JS or a PHP solution...

Regards,

LC.

Last edited by LearningCoder; 09-18-2012 at 03:14 PM..
LearningCoder is offline   Reply With Quote
Old 09-18-2012, 04:15 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
LC,

In post 4 I asked you a direct question which you answered with an indirect reply in the form of code and more questions. In my next post I suggested you use php to retain the information in sessions. You then come back bleating on about javascript again and again I've given you yet more information on how to do it in php.

To date, I've given you more than enough information to use a ternary operator and session to achieve this and you continue to ask in a php forum about how to solve this in javascript.

Why? - You claim you're not sure if this is a JS or PHP solution yet I've told you how to do it and you're still asking for a solution in JS while saying you're not even sure if JS .. oh why am I bothering
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 09-18-2012, 04:18 PM   PM User | #11
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
kthx.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-19-2012, 12:17 AM   PM User | #12
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Do I create the session variable in my sort.php or my main page?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-19-2012, 01:17 AM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Where ever you initially grab the data from the database would be the best place.

I see you're using sort.php for the ajax but does the main page also use ajax to get the initial data or is that printed into the page by php? - Where ever you get that first initial data from is the best place to do it.

I won't be about much tomorrow but keep me updated, I'll tune in when I'm back.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 09-19-2012 at 01:22 AM..
tangoforce is offline   Reply With Quote
Old 09-19-2012, 01:26 AM   PM User | #14
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I was using sort.php within a require statement so that the data would initially display, but I took that out.

Here is my current php:
PHP Code:
<?php
session_start
();
require(
"connectdb.php");


if(!empty(
$_GET['col']) && $_GET['col'] != 'DEFAULT'){


$column $_GET['col'];//holds value which column was clicked.

$sql mysql_query("SELECT * FROM members ORDER BY {$column} ");
   
   if(!
$sql){
     die(
"error with query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
else if(
$_GET['col'] == 'DEFAULT') {

$sql mysql_query("SELECT * FROM members");

   if(!
$sql){
      die(
"error with default query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
else {

$sql mysql_query("SELECT * FROM members");

   if(!
$sql){
      die(
"error with onload query");
   }
   
   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
?>
Thanks for the reply.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-19-2012, 08:36 PM   PM User | #15
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Sorry to bombard with the questions but I really cannot for the life of me figure out how to switch the values properly on each script call.

So, I have my table and links here like so:
Code:
<div id="order_links_div">
	  <a href="#" name="DEFAULT" onmouseup="showUser('DEFAULT')">DEFAULT</a>
   </div>

<div id='members_div'>
      <table id="header_table">
	  <tr>
	     <th>Members No.</th>
	     <th><a href="#" onmouseup="showUser('username')">Username</a></th>
	     <th><a href="#" onmouseup="showUser('join_date')">Join Date</th>
	  </tr>
      </table>

      <table id="results_table">
          <?php require("sort.php"); ?>
      </table>
</div>
It takes the column name and is passed as a parameter to the javascript function which calls the sort.php script and passes in this value along in the url everytime it is clicked and the query in the sort.php orders them by database table name...

So there I can switch the columns no problem and they order correctly. I also 'require' the sort.php in my html file so that something is displayed if nothing has been clicked yet, as you can see in the above code.

But I'm not sure at what point in my sort.php script do I create the session. Here it is:
PHP Code:
<?php
require("connectdb.php");

$_SESSION['order'] = "ASC";
echo 
$_SESSION['order'];


if(!empty(
$_GET['col']) && $_GET['col'] != 'DEFAULT'){


$column $_GET['col'];//holds value which column was clicked, passed in by javascript..

$sql mysql_query("SELECT * FROM members ORDER BY {$column} ");
   
   if(!
$sql){
     die(
"error with query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
else if(!empty(
$_GET['col']) && $_GET['col'] == 'DEFAULT') {

$sql mysql_query("SELECT * FROM members");

   if(!
$sql){
      die(
"error with default query");
   }

   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
else {

$sql mysql_query("SELECT * FROM members");
   
$_GET['col'] = 'undefined';

   if(!
$sql){
      die(
"error with onload query");
   }
   
   while (
$row mysql_fetch_array($sql)){
      echo 
"<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
   }

}
?>
Do I create it at the very top of the script or within the first if statement (seeing as though we know the user has clicked something...)?

I would really appreciate some help with this. Been racking my brains and reading sites etc for days and cannot figure out how to change the order depending on the last order..

I thought about using a for loop to keep track of how many times the sort.php script had been accessed, but couldn't figure out how to build it.

Kind regards,

LC.

Last edited by LearningCoder; 09-19-2012 at 08:40 PM..
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.