Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts

    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.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    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.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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'
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    kthx.

    Regards,

    LC.

  • #12
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Do I create the session variable in my sort.php or my main page?

    Regards,

    LC.

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    Last edited by tangoforce; 09-19-2012 at 01:22 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.

  • #15
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •