Jump to content

code tidy


matthew9090

Recommended Posts

please could you inspect this code and tell me any errors. It is a dictionary script so i shows words from a database with the definition underneith.

 

here are ones that i can't fix:

  • There are 2 divs ontop of each other which makes some borders thicker than the other
  • Some searches start out as bold word and non-bold descripton(that's meant to happen) but then half way down all the text is bold and the borders are thicker
  • the extend() javascript function does not work

 

other than that everything else works.

 

so please can you help clean up my code and tell me what i need to do to fix some of the things.

 

here's the code:

<style type="text/css">
      div 
      {
      font-family:Arial,Helvetica,sans-serif;
      font-size: 30px;
      border-style:solid;
border-width:1px;
width: 700px;
background-color: #2CFFB5;
padding: 0px;
      }
      div#div:hover {
background-color:#f1f1f1;
cursor:pointer
      }
      .highlight {
      color: red;
      }
</style>
<script type="text/javascript">
function hover() 
{
document.getElementById("div").style.border="thick solid #0000FF";
}

function extend(str)
{
document.getElementById("extend").innerHTML=str;
document.getElementById("extend").style.width="800px";
}
</script>
<?php
error_reporting(E_ALL);
mysql_connect("localhost", "root", "");
mysql_select_db("dictionary");
$words = mysql_query("SELECT * FROM words");
$q = strtolower($_GET['q']);
$query = mysql_query("SELECT * FROM words WHERE word LIKE '%$q%' OR definition LIKE '%$q%'");
while ($row = mysql_fetch_assoc($query))
{
   $tags1 = "<div id=\"div\" onMouseOver='hover()'><b>";
   $tags2 = "</b><br />";
   $tags3 = "<br /><br /></div>";
   $string1 = $row['word'];
   $string2 = $row['definition'];
   if (strlen($string2)>43) 
   { 
      $tags1 .= "<div id='extend' onMouseOver=\"extend(".$string2.")\">" . $tags1;
      $tags3 = $tags3 . "</div>";
      $string2 = substr($string2, 0, 40); 
      $tags3 = "...<br /><br /></div>";
   }
   $string3 = str_ireplace($q, "<span class='highlight'>$q</span>", $string1);
   $string4 = str_ireplace($q, "<span class='highlight'>$q</span>", $string2);
   $string = $tags1 . $string3 . $tags2 . $string4 . $tags3;
   $string = strtolower($string);
   if (strlen($string)>25) 
   { 
      substr($string, 0, 25); 
   }
   echo $string;
}

if (mysql_num_rows($query)==0)
{
   echo "No words found";
}

if ($q=="")
{
   echo "Type a word";
}
?>

 

and the index page:

<html>
   <head>
      <script type="text/javascript" src="scripts.js"></script>
      <style type="text/css">
      p {
      font-family:Arial,Helvetica,sans-serif;
      color: #FF4B2C;
      }
      
      a:link {
      color: #FF4B2C;
      }
      
      a:visited {
      color: #FF4B2C;
      }
      
      html {
         background-color: #2CE0FF;
      }
      
      h1 {
      color: #2C76FF;
      }
      
      #input {
         width: 700px;
         height: 55px;
         font-family: verdana;
         font-size: 35px;
         background: #A4EF9C url(a.png) no-repeat left -15px ;
      }
      #input:hover {
         background-color: #ffffff;
      }
      #input:focus {
         background: #ffffff url(a.png) no-repeat left -11125px ;
      }
      
      h1 {
         font-size: 50px;
      }
      
      div {
      
      }
      </style>
   </head>
   <body>
      <h1>The dictionary</h1>
      <form>
         <input type="text" id="input" onkeyup="showHint(this.value)" /> 
      </form>
      <p>
      <div id="txtHint"></div>
      </p>
      <p>
         <a href="word.php">Make up your word now!</a>
      </p>
   </body>
</html>

  

 

and the ajax script:

function showHint(str)
{
if (str.length==0)
  { 
  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==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}

Link to comment
https://forums.phpfreaks.com/topic/237123-code-tidy/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.