Results 1 to 2 of 2

Thread: Help - Fetch Data from MySQL using PHP to populate Table
      
   

  1. #1
    Join Date
    Nov 2006
    Location
    Centurion, South Africa
    Posts
    586

    Default Help - Fetch Data from MySQL using PHP to populate Table

    Hi Folks,

    I have a site which allows me to enter data and the form works well

    What I would like to do is the following:

    I have a select box which already has the names of events loaded into it.
    I have the data in MySQL

    I have seen an example at this link which is similar to what I would like to do except that the table will have to have enough rows to populate the same information that was provided in the first link.

    My challenge again is that I can see the twq codes need to be linked somehow and do not know how to go about this.

    I have created two files and each cvan be veiwed at the following links:

    Select Box
    The table

    The select box seems to veiw ok but when selction is made nothing happens.

    If I try veiw the table then the page views blank. When I select a tournament name then there is activity as the text that is there is over written by the blank page. It is just that the table is not appearing as intended.

    It would be great if you could advise.

    The two files are posted below:

    SELECT BOX

    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    showUser(str)
    {
    if (
    str=="")
      {
      
    document.getElementById("txtHint").innerHTML="";
      return;
      } 
    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==&& xmlhttp.status==200)
        {
        
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","MySQLreturntable.php?q="+str,true);
    xmlhttp.send();
    }
    </
    script>
    </
    head>
    <
    body>

    <
    form>
    <
    select name="users" onchange="showUser(this.value)">
    <
    option value="">Select a tournament:</option>
    <
    option value="1">Kempton Park Golf Club</option>
    <
    option value="2">Kyalami Golf Club</option>
    <
    option value="3">St.Francis Links</option>
    <
    option value="4">Gardener Ross Golf Estate</option>
    <
    option value="5">Rustenburg Golf Club</option>
    <
    option value="6">Witbank Golf Course</option>
    <
    option value="7">Ebotse Golf Estate</option>
    <
    option value="8">Umhlali Golf Estate</option>
    <
    option value="9">Durban Country Club</option>
    <
    option value="10">Steenburg Golf Club</option>
    <
    option value="11">Maccauvlei Golf Club</option>
    <
    option value="12">Mt.Edgecombe Country Club</option>
    <
    option value="13">Randpark Golf Club</option>
    <
    option value="14">Oubaai Golf Club</option>
    <
    option value="15">Zwartkop Country Club</option>
    <
    option value="16">ERPM Golf Club</option>
    <
    option value="17">Polokwane Golf Club</option>
    <
    option value="18">Erivale</option>
    <
    option value="19">Royal Cape Golf Club</option>
    <
    option value="20">Paarl Golf Club</option>
    <
    option value="21">Irene Country Club</option>
    <
    option value="22">Stellenbosch Golf Club</option>
    <
    option value="23">Mowbray Golf Club</option>
    <
    option value="24">Bryanston Golf Club</option>
    <
    option value="25">Schoeman Park Golf Club</option>
    <
    option value="26">Nelspruit Golf Club</option>
    <
    option value="27">The Hill</option>
    <
    option value="28">Mossel Bay Golf Club</option>
    <
    option value="29">Wanderers Golf Club</option>
    <
    option value="30">Pecanwood Golf Club</option>

    </
    select>
    </
    form>
    <
    br />
    <
    div id="txtHint"><b>The Results of the Winners will be listed here.</b></div>

    </
    body>
    </
    html
    THE TABLE PAGE

    PHP Code:
    <?php
    $q
    =$_GET["q"];

    $con mysql_connect("localhost""*****_r*****h""********") or
    if (!
    $con)
      {
    die (
    "Hey loser, check your server connection.");
    }
    mysql_select_db("eghfya_Members"$con);
    $sql="SELECT * FROM _Form_Nr_6 WHERE id = '".$q."'";
    $result mysql_query($sql);

    echo 
    "<table border='1'>
    <tr>
    <th>NAME</th>
    <th>SURNAME</th>
    <th>DIVISION</th>
    <th>SCORE</th>
    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['champions_name'] . "</td>";
      echo 
    "<td>" $row['champion_surname'] . "</td>";
      echo 
    "<td>" $row['champion_of _the_day_comes_from:'] . "</td>";
      echo 
    "<td>" $row['champions_gross_score'] . "</td>";
       echo 
    "</tr>";
      }
    echo 
    "</table>";

    mysql_close($con);
    ?>

  2. #2
    Join Date
    Apr 2006
    Location
    SPAIN
    Posts
    182

    Default Re: Help - Fetch Data from MySQL using PHP to populate Table

    Hi,
    Try pasting while loop before you echo out the table. while($row = mysql_fetch_array($result)) then make variables for all the fields: $row['champions_name'] for example $champ_name = $row['champions_name'] then you can echo out the table echo "<td>" $champ_name "</td>";

    There is a jquery plugin: Autocomplete, that is very similar and very easy to implement.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

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