FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP and Javascript Issue handling data from a mysql query





welshsteve
Hi everyone. It's been quite a while since I did any php programming, and am very rusty (I was never great anyway Laughing) but I am trying to re-use a script I've used before, but with slightly more data to handle.

However, the below is not working. It's basically a list of results taken from a mysql database. The sql syntax is correct and run in phpmyadmin displays 4 records. There is javascript used to show/hide table IDs for individual scores for each match. It is this that is not working. It seems to be embedding each match within another.

In other words, when the form is submitted, it shows only one match (instead of the 4 records it should be showing). When I click the icon to display the individual scores, it expands these fine, but then shows the second record in the results, and so on through the records. The page this can be seen at is http://www.sdpl-online.co.uk/w_res.php

Code:


      <form name="weekselect" action="w_res.php" method="post">
      <p>Please select a week: <select name="selectweek">
         <option value="1"> Week 1 - 25th October</option>
         <option value="2"> Week 2 - 1st November</option>
         <option value="3"> Week 3 - 8th November</option>
         <option value="4"> Week 4 - 15th November</option>
         <option value="5"> Week 5 - 22nd November</option>
         <option value="6"> Week 6 - 29th November</option>
         <option value="7"> Week 7 - 6th December</option>
         <option value="8"> Week 8 - 31st January</option>
         <option value="9"> Week 9 - 7th February</option>
         <option value="10"> Week 10 - 14th February</option>
         <option value="11"> Week 11 - 21st February</option>
         <option value="12"> Week 12 - 28th February</option>
         <option value="13"> Week 13 - 6th March</option>
         <option value="14"> Week 14 - 13th March</option>
      </select>
      <input type="submit" value="Show Results" /></p>
      </form>
<?php
   $con = mysql_connect("SITE","USER","PASS");
   if (!$con)
     {
     die('Could not connect: ' . mysql_error() . 'database connection problem');
     }
   mysql_select_db("DB", $con);
$week = mysql_real_escape_string(stripslashes($_POST["selectweek"]));

//ADDED THIS BIT
if (!empty($week)) {
    //do the query
    //display results
$sql = "SELECT * FROM w_res_1 WHERE week = ".$week;
//echo $sql;
$result = mysql_query($sql);

//added
if ($week == 1) {
$date = "25th October 2011";
}
elseif ($week == 2) {
$date = "1st November 2011";
}
elseif ($week == 3) {
$date = "8th November 2011";
}
elseif ($week == 4) {
$date = "15th November 2011";
}
elseif ($week == 5) {
$date = "22nd November 2011";
}
elseif ($week == 6) {
$date = "29th November 2011";
}
elseif ($week == 7) {
$date = "6th December 2011";
}
elseif ($week == 8) {
$date = "31st January 2012";
}
elseif ($week == 9) {
$date = "7th February 2012";
}
elseif ($week == 10) {
$date = "14th February 2011";
}
elseif ($week == 11) {
$date = "21st February 2012";
}
elseif ($week == 12) {
$date = "28th February 2012";
}
elseif ($week == 13) {
$date = "6th March 2012";
}
else $date = "13th March 2012";
//end added code

if (!$result)
echo mysql_error();

echo "<table class='fixtures' id='week" . $week . "' style='display:block;'>";
echo "<tr><th colspan='6'>WEEK" . $week . " - " . $date . "</th></tr>";
echo "<tr><td colspan='6'>Click <span class='red'>i</span> for frame results next to each match</td></tr>";
echo "<tr><th class='sub' colspan='6'>DIVISION 1</th></tr>";


while($row = mysql_fetch_array($result))
  {
      echo "<tr><td class='teaml'>" . $row["ht"] . "<td class='score'>" . $row["hs"] . "</td><td class='score'>-</td><td class='score'>" . $row["as"] . "</td><td class='teamr'>" . $row["at"] . "</td><td class='score'><a href='javascript:toggleLayer(\"d" . $row["division"] . "-"  . $row["week"] . "-" . $row["match"] ."\");'><img src='images/info.gif' style='border:0;' alt='Info' /></a></td></tr>";
      echo "<tr><td colspan='6'><table id='d" .$row["division"]. "-" .$row["week"]. "-" .$row["match"]. "' style='margin-left:5px;display:none;'>";
         echo "<tr><td class='score'>" . $row["hp1"] . "</td><td class='score'>" . $row["hs1"] . "</td><td class='score'>-</td><td class='score'>" . $row["as1"] . "</td><td class='score'>" . $row["ap1"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp2"] . "</td><td class='score'>" . $row["hs2"] . "</td><td class='score'>-</td><td class='score'>" . $row["as2"] . "</td><td class='score'>" . $row["ap2"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp3"] . "</td><td class='score'>" . $row["hs3"] . "</td><td class='score'>-</td><td class='score'>" . $row["as3"] . "</td><td class='score'>" . $row["ap3"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp4"] . "</td><td class='score'>" . $row["hs4"] . "</td><td class='score'>-</td><td class='score'>" . $row["as4"] . "</td><td class='score'>" . $row["ap4"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp5"] . "</td><td class='score'>" . $row["hs5"] . "</td><td class='score'>-</td><td class='score'>" . $row["as5"] . "</td><td class='score'>" . $row["ap5"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp6"] . "</td><td class='score'>" . $row["hs6"] . "</td><td class='score'>-</td><td class='score'>" . $row["as6"] . "</td><td class='score'>" . $row["ap6"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp7"] . "</td><td class='score'>" . $row["hs7"] . "</td><td class='score'>-</td><td class='score'>" . $row["as7"] . "</td><td class='score'>" . $row["ap7"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp8"] . "</td><td class='score'>" . $row["hs8"] . "</td><td class='score'>-</td><td class='score'>" . $row["as8"] . "</td><td class='score'>" . $row["ap8"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp9"] . "</td><td class='score'>" . $row["hs9"] . "</td><td class='score'>-</td><td class='score'>" . $row["as9"] . "</td><td class='score'>" . $row["ap9"] . "</td><td class='score'>&nbsp;</td></tr>";
         echo "<tr><td class='score'>" . $row["hp10"] . "</td><td class='score'>" . $row["hs10"] . "</td><td class='score'>-</td><td class='score'>" . $row["as10"] . "</td><td class='score'>" . $row["ap10"] . "</td><td class='score'>&nbsp;</td></tr>";
      echo "<tr><td colspan='6'><hr /></td></tr>";
  }
echo "</table>";
}
else echo "No week selected";

mysql_close($con);
?>


Can anyone help? I know it's probably something stupid I've missed out, but I have this very same script working on a smaller scale elsewhere.

Many thanks in advance
jmraker
It's a table thing. I see a <table> tag in the loop but no </table> in the loop (and also the </td></tr>)
the 2nd table is 1 level deeper inside the DOM and inside the last <td> of the hidden table and the 3rd one is hidden deeper, etc

Visual example:
Code:
<table rec1>
  <tr>
   <td>
     <table rec2>
      <tr>
       <td>
         <table rec3>
            ...


add a
Code:
echo '</table></td></tr>';

at the end of your loop and it might work better
Related topics
Question about forms on websites?
Using Javascript to Encrypt Data then POST to PHP
Help with mysql query
Java tutorials
MySQL Query Cache
A good place to learn html/php/javascript/etc....
mysql query based on 2 fields using 3 different tables
Inserting PHP multidimensional arrays into a mySQL database
Frihost PHP Account
Change password and forgot password script
Semicolon in SQL
Problem with code that handles arrays and database updates
Noobie's code
Interview: Derek Liu, Gaia Online Anime Community
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.